8
0

utils.js 2.5 KB

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