8
0

utils.js 2.0 KB

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