Bez popisu

gulp 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/usr/bin/env node
  2. 'use strict';
  3. var gutil = require('gulp-util');
  4. var prettyTime = require('pretty-hrtime');
  5. var chalk = require('chalk');
  6. var semver = require('semver');
  7. var archy = require('archy');
  8. var Liftoff = require('liftoff');
  9. var tildify = require('tildify');
  10. var interpret = require('interpret');
  11. var v8flags = require('v8flags');
  12. var completion = require('../lib/completion');
  13. var argv = require('minimist')(process.argv.slice(2));
  14. var taskTree = require('../lib/taskTree');
  15. // Set env var for ORIGINAL cwd
  16. // before anything touches it
  17. process.env.INIT_CWD = process.cwd();
  18. var cli = new Liftoff({
  19. name: 'gulp',
  20. completions: completion,
  21. extensions: interpret.jsVariants,
  22. v8flags: v8flags,
  23. });
  24. // Exit with 0 or 1
  25. var failed = false;
  26. process.once('exit', function(code) {
  27. if (code === 0 && failed) {
  28. process.exit(1);
  29. }
  30. });
  31. // Parse those args m8
  32. var cliPackage = require('../package');
  33. var versionFlag = argv.v || argv.version;
  34. var tasksFlag = argv.T || argv.tasks;
  35. var tasks = argv._;
  36. var toRun = tasks.length ? tasks : ['default'];
  37. // This is a hold-over until we have a better logging system
  38. // with log levels
  39. var simpleTasksFlag = argv['tasks-simple'];
  40. var shouldLog = !argv.silent && !simpleTasksFlag;
  41. if (!shouldLog) {
  42. gutil.log = function() {};
  43. }
  44. cli.on('require', function(name) {
  45. gutil.log('Requiring external module', chalk.magenta(name));
  46. });
  47. cli.on('requireFail', function(name) {
  48. gutil.log(chalk.red('Failed to load external module'), chalk.magenta(name));
  49. });
  50. cli.on('respawn', function(flags, child) {
  51. var nodeFlags = chalk.magenta(flags.join(', '));
  52. var pid = chalk.magenta(child.pid);
  53. gutil.log('Node flags detected:', nodeFlags);
  54. gutil.log('Respawned to PID:', pid);
  55. });
  56. cli.launch({
  57. cwd: argv.cwd,
  58. configPath: argv.gulpfile,
  59. require: argv.require,
  60. completion: argv.completion,
  61. }, handleArguments);
  62. // The actual logic
  63. function handleArguments(env) {
  64. if (versionFlag && tasks.length === 0) {
  65. gutil.log('CLI version', cliPackage.version);
  66. if (env.modulePackage && typeof env.modulePackage.version !== 'undefined') {
  67. gutil.log('Local version', env.modulePackage.version);
  68. }
  69. process.exit(0);
  70. }
  71. if (!env.modulePath) {
  72. gutil.log(
  73. chalk.red('Local gulp not found in'),
  74. chalk.magenta(tildify(env.cwd))
  75. );
  76. gutil.log(chalk.red('Try running: npm install gulp'));
  77. process.exit(1);
  78. }
  79. if (!env.configPath) {
  80. gutil.log(chalk.red('No gulpfile found'));
  81. process.exit(1);
  82. }
  83. // Check for semver difference between cli and local installation
  84. if (semver.gt(cliPackage.version, env.modulePackage.version)) {
  85. gutil.log(chalk.red('Warning: gulp version mismatch:'));
  86. gutil.log(chalk.red('Global gulp is', cliPackage.version));
  87. gutil.log(chalk.red('Local gulp is', env.modulePackage.version));
  88. }
  89. // Chdir before requiring gulpfile to make sure
  90. // we let them chdir as needed
  91. if (process.cwd() !== env.cwd) {
  92. process.chdir(env.cwd);
  93. gutil.log(
  94. 'Working directory changed to',
  95. chalk.magenta(tildify(env.cwd))
  96. );
  97. }
  98. // This is what actually loads up the gulpfile
  99. require(env.configPath);
  100. gutil.log('Using gulpfile', chalk.magenta(tildify(env.configPath)));
  101. var gulpInst = require(env.modulePath);
  102. logEvents(gulpInst);
  103. process.nextTick(function() {
  104. if (simpleTasksFlag) {
  105. return logTasksSimple(env, gulpInst);
  106. }
  107. if (tasksFlag) {
  108. return logTasks(env, gulpInst);
  109. }
  110. gulpInst.start.apply(gulpInst, toRun);
  111. });
  112. }
  113. function logTasks(env, localGulp) {
  114. var tree = taskTree(localGulp.tasks);
  115. tree.label = 'Tasks for ' + chalk.magenta(tildify(env.configPath));
  116. archy(tree)
  117. .split('\n')
  118. .forEach(function(v) {
  119. if (v.trim().length === 0) {
  120. return;
  121. }
  122. gutil.log(v);
  123. });
  124. }
  125. function logTasksSimple(env, localGulp) {
  126. console.log(Object.keys(localGulp.tasks)
  127. .join('\n')
  128. .trim());
  129. }
  130. // Format orchestrator errors
  131. function formatError(e) {
  132. if (!e.err) {
  133. return e.message;
  134. }
  135. // PluginError
  136. if (typeof e.err.showStack === 'boolean') {
  137. return e.err.toString();
  138. }
  139. // Normal error
  140. if (e.err.stack) {
  141. return e.err.stack;
  142. }
  143. // Unknown (string, number, etc.)
  144. return new Error(String(e.err)).stack;
  145. }
  146. // Wire up logging events
  147. function logEvents(gulpInst) {
  148. // Total hack due to poor error management in orchestrator
  149. gulpInst.on('err', function() {
  150. failed = true;
  151. });
  152. gulpInst.on('task_start', function(e) {
  153. // TODO: batch these
  154. // so when 5 tasks start at once it only logs one time with all 5
  155. gutil.log('Starting', '\'' + chalk.cyan(e.task) + '\'...');
  156. });
  157. gulpInst.on('task_stop', function(e) {
  158. var time = prettyTime(e.hrDuration);
  159. gutil.log(
  160. 'Finished', '\'' + chalk.cyan(e.task) + '\'',
  161. 'after', chalk.magenta(time)
  162. );
  163. });
  164. gulpInst.on('task_err', function(e) {
  165. var msg = formatError(e);
  166. var time = prettyTime(e.hrDuration);
  167. gutil.log(
  168. '\'' + chalk.cyan(e.task) + '\'',
  169. chalk.red('errored after'),
  170. chalk.magenta(time)
  171. );
  172. gutil.log(msg);
  173. });
  174. gulpInst.on('task_not_found', function(err) {
  175. gutil.log(
  176. chalk.red('Task \'' + err.task + '\' is not in your gulpfile')
  177. );
  178. gutil.log('Please check the documentation for proper gulpfile formatting');
  179. process.exit(1);
  180. });
  181. }