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