8
0

utils.js 4.9 KB

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