utils.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 minimist = require( 'minimist' );
  15. const utils = {
  16. /**
  17. * Parses command line arguments and returns them as a user-friendly hash.
  18. *
  19. * @returns {Object} options
  20. * @returns {String} [options.config] Path to the bundle configuration file.
  21. */
  22. parseArguments() {
  23. const options = minimist( process.argv.slice( 2 ), {
  24. string: [
  25. 'config'
  26. ]
  27. } );
  28. return options;
  29. },
  30. /**
  31. * Render content for entry file which needs to be passed as main file to Rollup.
  32. *
  33. * @param {Object} data
  34. * @param {String} [data.moduleName] Name of the editor class exposed in bundle. e.g. MyCKEditor.
  35. * @param {String} [data.creator] Path to the editor creator as ClassicEditor, StandardEditor etc.
  36. * @param {Array<String>} [data.features] List of paths or names to features which need to be included in bundle.
  37. * @returns {string}
  38. */
  39. renderEntryFileContent( data ) {
  40. const creatorName = path.basename( data.creator, '.js' );
  41. const creatorPath = path.relative( './tmp', data.creator );
  42. let featureNames = [];
  43. let template = `'use strict';\n\n`;
  44. // Imports babel helpers.
  45. template += `import '../node_modules/regenerator-runtime/runtime.js';\n`;
  46. // Imports editor creator
  47. template += `import ${ creatorName } from '${ creatorPath }';\n`;
  48. // Imports editor features.
  49. for ( let feature of data.features ) {
  50. const featureName = path.basename( feature, '.js' );
  51. const featurePath = path.relative( './tmp', feature );
  52. template += `import ${ featureName } from '${ featurePath }';\n`;
  53. featureNames.push( featureName );
  54. }
  55. // Class definition.
  56. template += `\nexport default class ${ data.moduleName } extends ${ creatorName } {
  57. static create( element, config = {} ) {
  58. if ( !config.features ) {
  59. config.features = [];
  60. }
  61. config.features = [ ...config.features, ${ featureNames.join( ', ' ) } ];
  62. return ${ creatorName }.create( element, config );
  63. }
  64. }
  65. `;
  66. return template;
  67. },
  68. /**
  69. * Save files from stream in specific destination and add `.min` suffix to the name.
  70. *
  71. * @param {Stream} stream
  72. * @param {String} destination path
  73. * @returns {Stream}
  74. */
  75. saveFileFromStreamAsMinified( stream, destination ) {
  76. return stream
  77. .pipe( gulpRename( {
  78. suffix: '.min'
  79. } ) )
  80. .pipe( gulp.dest( destination ) );
  81. },
  82. /**
  83. * Copy specified file to specified destination.
  84. *
  85. * @param {String} from file path
  86. * @param {String} to copied file destination
  87. * @returns {Promise}
  88. */
  89. copyFile( from, to ) {
  90. return new Promise( ( resolve ) => {
  91. gulp.src( from )
  92. .pipe( gulp.dest( to ) )
  93. .on( 'finish', resolve );
  94. } );
  95. },
  96. /**
  97. * Get size of the file.
  98. *
  99. * @param {String} path path to the file
  100. * @returns {Number} size size in bytes
  101. */
  102. getFileSize( path ) {
  103. return fs.statSync( path ).size;
  104. },
  105. /**
  106. * Get human readable gzipped size of the file.
  107. *
  108. * @param {String} path path to the file
  109. * @returns {Number} size size in bytes
  110. */
  111. getGzippedFileSize( path ) {
  112. return gzipSize.sync( fs.readFileSync( path ) );
  113. },
  114. /**
  115. * Get normal and gzipped size of every passed file in specified directory.
  116. *
  117. * @param {Array<String>} files
  118. * @param {String} [rootDir='']
  119. * @returns {Array<Object>} List with file size data
  120. */
  121. getFilesSizeStats( files, rootDir = '' ) {
  122. return files.map( ( file ) => {
  123. const filePath = path.join( rootDir, file );
  124. return {
  125. name: path.basename( filePath ),
  126. size: utils.getFileSize( filePath ),
  127. gzippedSize: utils.getGzippedFileSize( filePath )
  128. };
  129. } );
  130. },
  131. /**
  132. * Print on console list of files with their size stats.
  133. *
  134. * Title:
  135. * file.js: 1 MB (gzipped: 400 kB)
  136. * file.css 500 kB (gzipped: 100 kB)
  137. *
  138. * @param {String} title
  139. * @param {Array<Object>} filesStats
  140. */
  141. showFilesSummary( title, filesStats ) {
  142. const label = gutil.colors.underline( title );
  143. const filesSummary = filesStats.map( ( file ) => {
  144. return `${ file.name }: ${ prettyBytes( file.size ) } (gzipped: ${ prettyBytes( file.gzippedSize ) })`;
  145. } ).join( '\n' );
  146. gutil.log( gutil.colors.green( `\n${ label }:\n${ filesSummary }` ) );
  147. }
  148. };
  149. // Assign properties from top level utils.
  150. module.exports = Object.assign( utils, mainUtils );