8
0

tasks.js 2.8 KB

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