8
0

tasks.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 gutil = require( 'gulp-util' );
  8. const minimist = require( 'minimist' );
  9. const path = require( 'path' );
  10. const merge = require( 'merge-stream' );
  11. const log = require( '../../lib/log' );
  12. const tools = require( '../../lib/tools' );
  13. const git = require( '../../lib/git' );
  14. module.exports = ( config ) => {
  15. const ckeditor5Path = process.cwd();
  16. const packageJSON = require( '../../../package.json' );
  17. // Configure logging.
  18. log.configure(
  19. ( msg ) => gutil.log( msg ),
  20. ( msg ) => gutil.log( gutil.colors.red( msg ) )
  21. );
  22. const tasks = {
  23. execOnRepositories() {
  24. // Omit `gulp exec` part of arguments
  25. const options = minimist( process.argv.slice( 3 ), {
  26. alias: { t: 'task' },
  27. stopEarly: false
  28. } );
  29. let execTask;
  30. try {
  31. execTask = require( `./functions/${ options.task }` );
  32. }
  33. catch ( error ) {
  34. log.err( error );
  35. }
  36. if ( execTask ) {
  37. return exec( execTask, ckeditor5Path, packageJSON, config.WORKSPACE_DIR, options );
  38. }
  39. },
  40. register() {
  41. gulp.task( 'exec', tasks.execOnRepositories );
  42. }
  43. };
  44. return tasks;
  45. };
  46. /**
  47. * @param {Function} execTask Task to use on each dependency.
  48. * @param {String} ckeditor5Path Path to main CKEditor5 repository.
  49. * @param {Object} packageJSON Parsed package.json file from CKEditor5 repository.
  50. * @param {String} workspaceRoot Relative path to workspace root.
  51. * @param {Boolean} dryRun
  52. */
  53. function exec( execTask, ckeditor5Path, packageJSON, workspaceRoot, params ) {
  54. const workspaceAbsolutePath = path.join( ckeditor5Path, workspaceRoot );
  55. // Get all CKEditor dependencies from package.json.
  56. const dependencies = tools.getCKEditorDependencies( packageJSON.dependencies );
  57. const streams = merge();
  58. if ( dependencies ) {
  59. const directories = tools.getCKE5Directories( workspaceAbsolutePath );
  60. if ( directories.length ) {
  61. for ( let dependency in dependencies ) {
  62. const repositoryURL = dependencies[ dependency ];
  63. const urlInfo = git.parseRepositoryUrl( repositoryURL );
  64. const repositoryAbsolutePath = path.join( ckeditor5Path, 'node_modules', dependency );
  65. // Check if repository's directory already exists.
  66. if ( directories.indexOf( urlInfo.name ) > -1 ) {
  67. try {
  68. log.out( `Executing task on ${ repositoryURL }...` );
  69. streams.add( execTask( repositoryAbsolutePath, params ) );
  70. } catch ( error ) {
  71. log.err( error );
  72. }
  73. }
  74. }
  75. } else {
  76. log.out( 'No CKEditor5 plugins in development mode.' );
  77. }
  78. } else {
  79. log.out( 'No CKEditor5 dependencies found in package.json file.' );
  80. }
  81. return streams;
  82. }