utils.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 filesize = require( 'filesize' );
  12. const mainUtils = require( '../utils' );
  13. const utils = {
  14. /**
  15. * Save files from stream in specific destination and add `.min` suffix to the name.
  16. *
  17. * @param {Stream} stream
  18. * @param {String} destination path
  19. * @returns {Stream}
  20. */
  21. saveFileFromStreamAsMinified( stream, destination ) {
  22. return stream
  23. .pipe( gulpRename( {
  24. suffix: '.min'
  25. } ) )
  26. .pipe( gulp.dest( destination ) );
  27. },
  28. /**
  29. * Get human readable size of the file.
  30. *
  31. * @param {String} path path to the file
  32. */
  33. getFileSize( path ) {
  34. return filesize( fs.statSync( path ).size );
  35. },
  36. /**
  37. * Log on console size of every passed file in specified directory.
  38. *
  39. * utils.logFileSize( [ 'ckeditor.min.js', 'ckeditor.min.css' ], 'path/to/dir' );
  40. *
  41. * ckeditor.min.js: 192.43 KB
  42. * ckeditor.min.css: 5.38 KB
  43. *
  44. * @param {String} [rootDir='']
  45. * @param {Array<String>} files
  46. */
  47. logFilesSize( files, rootDir = '' ) {
  48. files = files.map( ( file ) => {
  49. let filePath = path.join( rootDir, file );
  50. let name = path.basename( filePath );
  51. let size = utils.getFileSize( filePath );
  52. return `${name}: ${size}`;
  53. } );
  54. gutil.log( gutil.colors.green( `\n${ files.join( '\n' ) }` ) );
  55. },
  56. /**
  57. * Copy specified file to specified destination.
  58. *
  59. * @param {String} from file path
  60. * @param {String} to copied file destination
  61. * @return {Stream}
  62. */
  63. copyFile( from, to ) {
  64. return gulp.src( from ).pipe( gulp.dest( to ) );
  65. }
  66. };
  67. // Assign properties from top level utils.
  68. module.exports = Object.assign( utils, mainUtils );