index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. 'use strict';
  2. var fs = require('fs');
  3. var log = require('gulplog');
  4. var stdout = require('mute-stdout');
  5. var ansi = require('../../shared/ansi');
  6. var exit = require('../../shared/exit');
  7. var tildify = require('../../shared/tildify');
  8. var logTasks = require('../../shared/log/tasks');
  9. var logEvents = require('../^4.0.0/log/events');
  10. var logSyncTask = require('../^4.0.0/log/sync-task');
  11. var logTasksSimple = require('../^4.0.0/log/tasks-simple');
  12. var registerExports = require('../../shared/register-exports');
  13. var copyTree = require('../../shared/log/copy-tree');
  14. var requireOrImport = require('../../shared/require-or-import');
  15. function execute(opts, env, config) {
  16. var tasks = opts._;
  17. var toRun = tasks.length ? tasks : ['default'];
  18. if (opts.tasksSimple || opts.tasks || opts.tasksJson) {
  19. // Mute stdout if we are listing tasks
  20. stdout.mute();
  21. }
  22. var gulpInst = require(env.modulePath);
  23. logEvents(gulpInst);
  24. logSyncTask(gulpInst, opts);
  25. // This is what actually loads up the gulpfile
  26. requireOrImport(env.configPath, function(err, exported) {
  27. // Before import(), if require() failed we got an unhandled exception on the module level.
  28. // So console.error() & exit() were added here to mimic the old behavior as close as possible.
  29. if (err) {
  30. console.error(err);
  31. exit(1);
  32. }
  33. registerExports(gulpInst, exported);
  34. // Always unmute stdout after gulpfile is required
  35. stdout.unmute();
  36. var tree;
  37. if (opts.tasksSimple) {
  38. return logTasksSimple(gulpInst.tree());
  39. }
  40. if (opts.tasks) {
  41. tree = {};
  42. if (config.description && typeof config.description === 'string') {
  43. tree.label = config.description;
  44. } else {
  45. tree.label = 'Tasks for ' + ansi.magenta(tildify(env.configPath));
  46. }
  47. tree.nodes = gulpInst.tree({ deep: true });
  48. return logTasks(tree, opts, function(taskname) {
  49. return gulpInst.task(taskname);
  50. });
  51. }
  52. if (opts.tasksJson) {
  53. tree = {};
  54. if (config.description && typeof config.description === 'string') {
  55. tree.label = config.description;
  56. } else {
  57. tree.label = 'Tasks for ' + tildify(env.configPath);
  58. }
  59. tree.nodes = gulpInst.tree({ deep: true });
  60. var output = JSON.stringify(copyTree(tree, opts));
  61. if (typeof opts.tasksJson === 'boolean' && opts.tasksJson) {
  62. return console.log(output);
  63. }
  64. return fs.writeFileSync(opts.tasksJson, output, 'utf-8');
  65. }
  66. try {
  67. log.info('Using gulpfile', ansi.magenta(tildify(env.configPath)));
  68. var runMethod = opts.series ? 'series' : 'parallel';
  69. gulpInst[runMethod](toRun)(function(err) {
  70. if (err) {
  71. exit(1);
  72. }
  73. });
  74. } catch (err) {
  75. log.error(ansi.red(err.message));
  76. log.error('To list available tasks, try running: gulp --tasks');
  77. exit(1);
  78. }
  79. });
  80. }
  81. module.exports = execute;