8
0

utils.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. const fs = require( 'fs' );
  7. const path = require( 'path' );
  8. const gulp = require( 'gulp' );
  9. const gulpRename = require( 'gulp-rename' );
  10. const gutil = require( 'gulp-util' );
  11. const prettyBytes = require( 'pretty-bytes' );
  12. const gzipSize = require( 'gzip-size' );
  13. const mainUtils = require( '../utils' );
  14. const utils = {
  15. /**
  16. * Save files from stream in specific destination and add `.min` suffix to the name.
  17. *
  18. * @param {Stream} stream
  19. * @param {String} destination path
  20. * @returns {Stream}
  21. */
  22. saveFileFromStreamAsMinified( stream, destination ) {
  23. return stream
  24. .pipe( gulpRename( {
  25. suffix: '.min'
  26. } ) )
  27. .pipe( gulp.dest( destination ) );
  28. },
  29. /**
  30. * Copy specified file to specified destination.
  31. *
  32. * @param {String} from file path
  33. * @param {String} to copied file destination
  34. * @returns {Promise}
  35. */
  36. copyFile( from, to ) {
  37. return new Promise( ( resolve ) => {
  38. gulp.src( from )
  39. .pipe( gulp.dest( to ) )
  40. .on( 'finish', resolve );
  41. } );
  42. },
  43. /**
  44. * Get size of the file.
  45. *
  46. * @param {String} path path to the file
  47. * @returns {Number} size size in bytes
  48. */
  49. getFileSize( path ) {
  50. return fs.statSync( path ).size;
  51. },
  52. /**
  53. * Get human readable gzipped size of the file.
  54. *
  55. * @param {String} path path to the file
  56. * @returns {Number} size size in bytes
  57. */
  58. getGzippedFileSize( path ) {
  59. return gzipSize.sync( fs.readFileSync( path ) );
  60. },
  61. /**
  62. * Get normal and gzipped size of every passed file in specified directory.
  63. *
  64. * @param {Array<String>} files
  65. * @param {String} [rootDir='']
  66. * @returns {Array<Object>} List with file size data
  67. */
  68. getFilesSizeStats( files, rootDir = '' ) {
  69. return files.map( ( file ) => {
  70. const filePath = path.join( rootDir, file );
  71. return {
  72. name: path.basename( filePath ),
  73. size: utils.getFileSize( filePath ),
  74. gzippedSize: utils.getGzippedFileSize( filePath )
  75. };
  76. } );
  77. },
  78. /**
  79. * Print on console list of files with their size stats.
  80. *
  81. * Title:
  82. * file.js: 1 MB (gzipped: 400 kB)
  83. * file.css 500 kB (gzipped: 100 kB)
  84. *
  85. * @param {String} title
  86. * @param {Array<Object>} filesStats
  87. */
  88. showFilesSummary( title, filesStats ) {
  89. const label = gutil.colors.underline( title );
  90. const filesSummary = filesStats.map( ( file ) => {
  91. return `${ file.name }: ${ prettyBytes( file.size ) } (gzipped: ${ prettyBytes( file.gzippedSize ) })`;
  92. } ).join( '\n' );
  93. gutil.log( gutil.colors.green( `\n${ label }:\n${ filesSummary }` ) );
  94. }
  95. };
  96. // Assign properties from top level utils.
  97. module.exports = Object.assign( utils, mainUtils );