build.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* jshint node: true, esnext: true */
  2. 'use strict';
  3. const KNOWN_OPTIONS = {
  4. build: {
  5. string: [
  6. 'formats'
  7. ],
  8. boolean: [
  9. 'watch'
  10. ],
  11. default: {
  12. formats: 'amd',
  13. watch: false
  14. }
  15. }
  16. };
  17. const fs = require( 'fs' );
  18. const path = require( 'path' );
  19. const gulp = require( 'gulp' );
  20. const del = require( 'del' );
  21. const merge = require( 'merge-stream' );
  22. const gulpMirror = require( 'gulp-mirror' );
  23. const gutil = require( 'gulp-util' );
  24. const minimist = require( 'minimist' );
  25. const utils = require( './utils' );
  26. const options = minimist( process.argv.slice( 2 ), KNOWN_OPTIONS[ process.argv[ 2 ] ] );
  27. module.exports = ( config ) => {
  28. const distDir = path.join( config.ROOT_DIR, config.DIST_DIR );
  29. const tasks = {
  30. /**
  31. * Removes the dist directory.
  32. */
  33. clean() {
  34. return del( distDir );
  35. },
  36. src: {
  37. /**
  38. * Returns a stream of all source files.
  39. *
  40. * @param {Boolean} [watch] Whether the files should be watched.
  41. * @returns {Stream}
  42. */
  43. all( watch ) {
  44. return merge( tasks.src.main( watch ), tasks.src.ckeditor5( watch ), tasks.src.modules( watch ) );
  45. },
  46. /**
  47. * Returns a stream with just the main file (`ckeditor5/ckeditor.js`).
  48. *
  49. * @param {Boolean} [watch] Whether to watch the files.
  50. * @returns {Stream}
  51. */
  52. main( watch ) {
  53. const glob = path.join( config.ROOT_DIR, 'ckeditor.js' );
  54. return gulp.src( glob )
  55. .pipe( watch ? utils.watch( glob ) : utils.noop() );
  56. },
  57. /**
  58. * Returns a stream of all source files from CKEditor 5.
  59. *
  60. * @param {Boolean} [watch] Whether to watch the files.
  61. * @returns {Stream}
  62. */
  63. ckeditor5( watch ) {
  64. const glob = path.join( config.ROOT_DIR, 'src', '**', '*.js' );
  65. return gulp.src( glob )
  66. .pipe( watch ? utils.watch( glob ) : utils.noop() )
  67. .pipe( utils.wrapCKEditor5Module() );
  68. },
  69. /**
  70. * Returns a stream of all source files from CKEditor 5 dependencies.
  71. *
  72. * @param {Boolean} [watch] Whether to watch the files.
  73. * @returns {Stream}
  74. */
  75. modules( watch ) {
  76. // Find all CKEditor5 package directories. Resolve symlinks so we watch real directories
  77. // in order to workaround https://github.com/paulmillr/chokidar/issues/419.
  78. const dirs = fs.readdirSync( path.join( config.ROOT_DIR, 'node_modules' ) )
  79. // Look for ckeditor5-* directories.
  80. .filter( ( fileName ) => fileName.indexOf( 'ckeditor5-' ) === 0 )
  81. // Resolve symlinks and keep only directories.
  82. .map( ( fileName ) => {
  83. let filePath = path.join( config.ROOT_DIR, 'node_modules', fileName );
  84. let stat = fs.lstatSync( filePath );
  85. if ( stat.isSymbolicLink() ) {
  86. filePath = fs.realpathSync( filePath );
  87. stat = fs.lstatSync( filePath );
  88. }
  89. if ( stat.isDirectory() ) {
  90. return filePath;
  91. }
  92. // Filter...
  93. return false;
  94. } )
  95. // ...those out.
  96. .filter( ( filePath ) => filePath );
  97. const streams = dirs.map( ( dirPath ) => {
  98. const glob = path.join( dirPath, 'src', '**', '*.js' );
  99. // Use parent as a base so we get paths starting with 'ckeditor5-*/src/*' in the stream.
  100. const baseDir = path.parse( dirPath ).dir;
  101. const opts = { base: baseDir };
  102. return gulp.src( glob, opts )
  103. .pipe( watch ? utils.watch( glob, opts ) : utils.noop() );
  104. } );
  105. return merge.apply( null, streams )
  106. .pipe( utils.unpackModules() );
  107. }
  108. }
  109. };
  110. gulp.task( 'build:clean', tasks.clean );
  111. gulp.task( 'build', [ 'build:clean' ], () => {
  112. const formats = options.formats.split( ',' );
  113. const codeStream = tasks.src.all( options.watch )
  114. .on( 'data', ( file ) => {
  115. gutil.log( `Processing '${ gutil.colors.cyan( file.path ) }'...` );
  116. } );
  117. const formatPipes = formats.reduce( utils.addFormat( distDir ), [] );
  118. return codeStream
  119. .pipe( gulpMirror.apply( null, formatPipes ) );
  120. } );
  121. return tasks;
  122. };