tasks.js 2.7 KB

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