4 var gutil = require('gulp-util');
5 var prettyTime = require('pretty-hrtime');
6 var chalk = require('chalk');
7 var semver = require('semver');
8 var archy = require('archy');
9 var Liftoff = require('liftoff');
10 var tildify = require('tildify');
11 var interpret = require('interpret');
12 var v8flags = require('v8flags');
13 var completion = require('../lib/completion');
14 var argv = require('minimist')(process.argv.slice(2));
15 var taskTree = require('../lib/taskTree');
17 // Set env var for ORIGINAL cwd
18 // before anything touches it
19 process.env.INIT_CWD = process.cwd();
21 var cli = new Liftoff({
23 completions: completion,
24 extensions: interpret.jsVariants,
30 process.once('exit', function(code) {
31 if (code === 0 && failed) {
36 // Parse those args m8
37 var cliPackage = require('../package');
38 var versionFlag = argv.v || argv.version;
39 var tasksFlag = argv.T || argv.tasks;
41 var toRun = tasks.length ? tasks : ['default'];
43 // This is a hold-over until we have a better logging system
45 var simpleTasksFlag = argv['tasks-simple'];
46 var shouldLog = !argv.silent && !simpleTasksFlag;
49 gutil.log = function() {};
52 cli.on('require', function(name) {
53 gutil.log('Requiring external module', chalk.magenta(name));
56 cli.on('requireFail', function(name) {
57 gutil.log(chalk.red('Failed to load external module'), chalk.magenta(name));
60 cli.on('respawn', function(flags, child) {
61 var nodeFlags = chalk.magenta(flags.join(', '));
62 var pid = chalk.magenta(child.pid);
63 gutil.log('Node flags detected:', nodeFlags);
64 gutil.log('Respawned to PID:', pid);
69 configPath: argv.gulpfile,
70 require: argv.require,
71 completion: argv.completion,
75 function handleArguments(env) {
76 if (versionFlag && tasks.length === 0) {
77 gutil.log('CLI version', cliPackage.version);
78 if (env.modulePackage && typeof env.modulePackage.version !== 'undefined') {
79 gutil.log('Local version', env.modulePackage.version);
84 if (!env.modulePath) {
86 chalk.red('Local gulp not found in'),
87 chalk.magenta(tildify(env.cwd))
89 gutil.log(chalk.red('Try running: npm install gulp'));
93 if (!env.configPath) {
94 gutil.log(chalk.red('No gulpfile found'));
98 // Check for semver difference between cli and local installation
99 if (semver.gt(cliPackage.version, env.modulePackage.version)) {
100 gutil.log(chalk.red('Warning: gulp version mismatch:'));
101 gutil.log(chalk.red('Global gulp is', cliPackage.version));
102 gutil.log(chalk.red('Local gulp is', env.modulePackage.version));
105 // Chdir before requiring gulpfile to make sure
106 // we let them chdir as needed
107 if (process.cwd() !== env.cwd) {
108 process.chdir(env.cwd);
110 'Working directory changed to',
111 chalk.magenta(tildify(env.cwd))
115 // This is what actually loads up the gulpfile
116 require(env.configPath);
117 gutil.log('Using gulpfile', chalk.magenta(tildify(env.configPath)));
119 var gulpInst = require(env.modulePath);
122 process.nextTick(function() {
123 if (simpleTasksFlag) {
124 return logTasksSimple(env, gulpInst);
127 return logTasks(env, gulpInst);
129 gulpInst.start.apply(gulpInst, toRun);
133 function logTasks(env, localGulp) {
134 var tree = taskTree(localGulp.tasks);
135 tree.label = 'Tasks for ' + chalk.magenta(tildify(env.configPath));
138 .forEach(function(v) {
139 if (v.trim().length === 0) {
146 function logTasksSimple(env, localGulp) {
147 console.log(Object.keys(localGulp.tasks)
152 // Format orchestrator errors
153 function formatError(e) {
159 if (typeof e.err.showStack === 'boolean') {
160 return e.err.toString();
168 // Unknown (string, number, etc.)
169 return new Error(String(e.err)).stack;
172 // Wire up logging events
173 function logEvents(gulpInst) {
175 // Total hack due to poor error management in orchestrator
176 gulpInst.on('err', function() {
180 gulpInst.on('task_start', function(e) {
182 // so when 5 tasks start at once it only logs one time with all 5
183 gutil.log('Starting', '\'' + chalk.cyan(e.task) + '\'...');
186 gulpInst.on('task_stop', function(e) {
187 var time = prettyTime(e.hrDuration);
189 'Finished', '\'' + chalk.cyan(e.task) + '\'',
190 'after', chalk.magenta(time)
194 gulpInst.on('task_err', function(e) {
195 var msg = formatError(e);
196 var time = prettyTime(e.hrDuration);
198 '\'' + chalk.cyan(e.task) + '\'',
199 chalk.red('errored after'),
205 gulpInst.on('task_not_found', function(err) {
207 chalk.red('Task \'' + err.task + '\' is not in your gulpfile')
209 gutil.log('Please check the documentation for proper gulpfile formatting');