tasks.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 gulp = require( 'gulp' );
  7. const minimist = require( 'minimist' );
  8. const path = require( 'path' );
  9. const merge = require( 'merge-stream' );
  10. const log = require( '../../utils/log' );
  11. const ckeditor5Dirs = require( '../../utils/ckeditor5-dirs' );
  12. /**
  13. * Run task over `ckeditor5-*` repositories.
  14. *
  15. * Example:
  16. *
  17. * gulp exec --task task-name
  18. *
  19. * Example of running task just for one repository:
  20. *
  21. * gulp exec --task task-name --repository ckeditor5-utils
  22. *
  23. * Example of running task including root `ckeditor5` package
  24. *
  25. * gulp exec --task task-name --include-root
  26. *
  27. * @param {Object} config Task runner configuration.
  28. * @returns {Stream} Stream with processed files.
  29. */
  30. module.exports = ( config ) => {
  31. const ckeditor5Path = process.cwd();
  32. const packageJSON = require( '../../../package.json' );
  33. const tasks = {
  34. execOnRepositories() {
  35. // Omit `gulp exec` part of arguments
  36. const params = minimist( process.argv.slice( 3 ), {
  37. stopEarly: false,
  38. } );
  39. let task;
  40. try {
  41. if ( params.task ) {
  42. task = require( `./functions/${ params.task }` );
  43. } else {
  44. throw new Error( 'Missing task parameter: --task task-name' );
  45. }
  46. } catch ( err ) {
  47. log.err( err );
  48. return;
  49. }
  50. return execute( task, ckeditor5Path, packageJSON, config.WORKSPACE_DIR, params );
  51. },
  52. register() {
  53. gulp.task( 'exec', tasks.execOnRepositories );
  54. }
  55. };
  56. return tasks;
  57. };
  58. /**
  59. * Execute given task with provided options and command-line parameters.
  60. *
  61. * @param {Function} execTask Task to use on each dependency.
  62. * @param {String} ckeditor5Path Path to main CKEditor5 repository.
  63. * @param {Object} packageJSON Parsed `package.json` file from CKEditor 5 repository.
  64. * @param {String} workspaceRoot Relative path to workspace root.
  65. * @param {Object} params Parameters provided to the task via command-line.
  66. * @returns {Stream} Merged stream of processed files.
  67. */
  68. function execute( execTask, ckeditor5Path, packageJSON, workspaceRoot, params ) {
  69. const workspacePath = path.join( ckeditor5Path, workspaceRoot );
  70. const mergedStream = merge();
  71. const specificRepository = params.repository;
  72. const includeRoot = !!params[ 'include-root' ];
  73. let devDirectories = ckeditor5Dirs.getDevDirectories( workspacePath, packageJSON, ckeditor5Path, includeRoot );
  74. if ( specificRepository ) {
  75. devDirectories = devDirectories.filter( ( dir ) => {
  76. return dir.repositoryURL === `ckeditor/${ specificRepository }`;
  77. } );
  78. }
  79. for ( const dir of devDirectories ) {
  80. try {
  81. log.out( `Executing task on ${ dir.repositoryURL }...` );
  82. const result = execTask( dir.repositoryPath, params );
  83. if ( result ) {
  84. mergedStream.add( result );
  85. }
  86. } catch ( err ) {
  87. log.err( err );
  88. }
  89. }
  90. return mergedStream;
  91. }