utils.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* jshint node: true, esnext: true */
  2. 'use strict';
  3. const path = require( 'path' );
  4. const gulp = require( 'gulp' );
  5. const rename = require( 'gulp-rename' );
  6. const babel = require( 'gulp-babel' );
  7. const gulpWatch = require( 'gulp-watch' );
  8. const gulpPlumber = require( 'gulp-plumber' );
  9. const gutil = require( 'gulp-util' );
  10. const multipipe = require( 'multipipe' );
  11. const stream = require( 'stream' );
  12. const sep = path.sep;
  13. const utils = {
  14. src( root, pattern, watch ) {
  15. const srcDir = path.join( root, pattern );
  16. let stream = gulp.src( srcDir );
  17. if ( watch ) {
  18. stream = stream
  19. // Let's use plumber only when watching. In other cases we should fail quickly and loudly.
  20. .pipe( gulpPlumber() )
  21. .pipe( gulpWatch( srcDir ) );
  22. }
  23. return stream;
  24. },
  25. dist( distDir, format ) {
  26. const destDir = path.join( distDir, format );
  27. return gulp.dest( destDir );
  28. },
  29. transpile( format ) {
  30. const babelModuleTranspilers = {
  31. amd: 'amd',
  32. cjs: 'commonjs'
  33. };
  34. const babelModuleTranspiler = babelModuleTranspilers[ format ];
  35. if ( !babelModuleTranspiler ) {
  36. throw new Error( `Incorrect format: ${ format }` );
  37. }
  38. return new stream.PassThrough( { objectMode: true } )
  39. .pipe( utils.pickVersionedFile( format ) )
  40. .pipe( babel( {
  41. plugins: [ `transform-es2015-modules-${ babelModuleTranspiler }` ]
  42. } ) );
  43. },
  44. addFormat( distDir ) {
  45. return ( pipes, format ) => {
  46. const conversionPipes = [];
  47. if ( format != 'esnext' ) {
  48. conversionPipes.push( utils.transpile( format ) );
  49. }
  50. conversionPipes.push(
  51. utils.dist( distDir, format )
  52. .on( 'data', ( file ) => {
  53. gutil.log( `Finished writing '${ gutil.colors.cyan( file.path ) }'` );
  54. } )
  55. );
  56. pipes.push( multipipe.apply( null, conversionPipes ) );
  57. return pipes;
  58. };
  59. },
  60. pickVersionedFile( format ) {
  61. return rename( ( path ) => {
  62. const regexp = new RegExp( `__${ format }$` );
  63. path.basename = path.basename.replace( regexp, '' );
  64. } );
  65. },
  66. /**
  67. * Move files out of `node_modules/ckeditor5-xxx/src/*` directories to `ckeditor5-xxx/*`.
  68. *
  69. * @returns {Stream}
  70. */
  71. unpackModules( modulePathPattern ) {
  72. return rename( ( filePath ) => {
  73. filePath.dirname = filePath.dirname.replace( modulePathPattern, `${ sep }$1${ sep }` );
  74. // Remove now empty src/ dirs.
  75. if ( !filePath.extname && filePath.basename == 'src' ) {
  76. filePath.basename = '';
  77. }
  78. } );
  79. },
  80. wrapCKEditor5Module() {
  81. return rename( ( filePath ) => {
  82. filePath.dirname = path.join( filePath.dirname, 'ckeditor5' );
  83. } );
  84. }
  85. };
  86. module.exports = utils;