|
1 | 1 | #!/usr/bin/env node |
2 | 2 |
|
3 | | -'use strict'; |
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'); |
16 | | - |
17 | | -// Set env var for ORIGINAL cwd |
18 | | -// before anything touches it |
19 | | -process.env.INIT_CWD = process.cwd(); |
20 | | - |
21 | | -var cli = new Liftoff({ |
22 | | - name: 'gulp', |
23 | | - completions: completion, |
24 | | - extensions: interpret.jsVariants, |
25 | | - v8flags: v8flags, |
26 | | -}); |
27 | | - |
28 | | -// Exit with 0 or 1 |
29 | | -var failed = false; |
30 | | -process.once('exit', function(code) { |
31 | | - if (code === 0 && failed) { |
32 | | - process.exit(1); |
33 | | - } |
34 | | -}); |
35 | | - |
36 | | -// Parse those args m8 |
37 | | -var cliPackage = require('../package'); |
38 | | -var versionFlag = argv.v || argv.version; |
39 | | -var tasksFlag = argv.T || argv.tasks; |
40 | | -var tasks = argv._; |
41 | | -var toRun = tasks.length ? tasks : ['default']; |
42 | | - |
43 | | -// This is a hold-over until we have a better logging system |
44 | | -// with log levels |
45 | | -var simpleTasksFlag = argv['tasks-simple']; |
46 | | -var shouldLog = !argv.silent && !simpleTasksFlag; |
47 | | - |
48 | | -if (!shouldLog) { |
49 | | - gutil.log = function() {}; |
50 | | -} |
51 | | - |
52 | | -cli.on('require', function(name) { |
53 | | - gutil.log('Requiring external module', chalk.magenta(name)); |
54 | | -}); |
55 | | - |
56 | | -cli.on('requireFail', function(name) { |
57 | | - gutil.log(chalk.red('Failed to load external module'), chalk.magenta(name)); |
58 | | -}); |
59 | | - |
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); |
65 | | -}); |
66 | | - |
67 | | -cli.launch({ |
68 | | - cwd: argv.cwd, |
69 | | - configPath: argv.gulpfile, |
70 | | - require: argv.require, |
71 | | - completion: argv.completion, |
72 | | -}, handleArguments); |
73 | | - |
74 | | -// The actual logic |
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); |
80 | | - } |
81 | | - process.exit(0); |
82 | | - } |
83 | | - |
84 | | - if (!env.modulePath) { |
85 | | - gutil.log( |
86 | | - chalk.red('Local gulp not found in'), |
87 | | - chalk.magenta(tildify(env.cwd)) |
88 | | - ); |
89 | | - gutil.log(chalk.red('Try running: npm install gulp')); |
90 | | - process.exit(1); |
91 | | - } |
92 | | - |
93 | | - if (!env.configPath) { |
94 | | - gutil.log(chalk.red('No gulpfile found')); |
95 | | - process.exit(1); |
96 | | - } |
97 | | - |
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)); |
103 | | - } |
104 | | - |
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); |
109 | | - gutil.log( |
110 | | - 'Working directory changed to', |
111 | | - chalk.magenta(tildify(env.cwd)) |
112 | | - ); |
113 | | - } |
114 | | - |
115 | | - // This is what actually loads up the gulpfile |
116 | | - require(env.configPath); |
117 | | - gutil.log('Using gulpfile', chalk.magenta(tildify(env.configPath))); |
118 | | - |
119 | | - var gulpInst = require(env.modulePath); |
120 | | - logEvents(gulpInst); |
121 | | - |
122 | | - process.nextTick(function() { |
123 | | - if (simpleTasksFlag) { |
124 | | - return logTasksSimple(env, gulpInst); |
125 | | - } |
126 | | - if (tasksFlag) { |
127 | | - return logTasks(env, gulpInst); |
128 | | - } |
129 | | - gulpInst.start.apply(gulpInst, toRun); |
130 | | - }); |
131 | | -} |
132 | | - |
133 | | -function logTasks(env, localGulp) { |
134 | | - var tree = taskTree(localGulp.tasks); |
135 | | - tree.label = 'Tasks for ' + chalk.magenta(tildify(env.configPath)); |
136 | | - archy(tree) |
137 | | - .split('\n') |
138 | | - .forEach(function(v) { |
139 | | - if (v.trim().length === 0) { |
140 | | - return; |
141 | | - } |
142 | | - gutil.log(v); |
143 | | - }); |
144 | | -} |
145 | | - |
146 | | -function logTasksSimple(env, localGulp) { |
147 | | - console.log(Object.keys(localGulp.tasks) |
148 | | - .join('\n') |
149 | | - .trim()); |
150 | | -} |
151 | | - |
152 | | -// Format orchestrator errors |
153 | | -function formatError(e) { |
154 | | - if (!e.err) { |
155 | | - return e.message; |
156 | | - } |
157 | | - |
158 | | - // PluginError |
159 | | - if (typeof e.err.showStack === 'boolean') { |
160 | | - return e.err.toString(); |
161 | | - } |
162 | | - |
163 | | - // Normal error |
164 | | - if (e.err.stack) { |
165 | | - return e.err.stack; |
166 | | - } |
167 | | - |
168 | | - // Unknown (string, number, etc.) |
169 | | - return new Error(String(e.err)).stack; |
170 | | -} |
171 | | - |
172 | | -// Wire up logging events |
173 | | -function logEvents(gulpInst) { |
174 | | - |
175 | | - // Total hack due to poor error management in orchestrator |
176 | | - gulpInst.on('err', function() { |
177 | | - failed = true; |
178 | | - }); |
179 | | - |
180 | | - gulpInst.on('task_start', function(e) { |
181 | | - // TODO: batch these |
182 | | - // so when 5 tasks start at once it only logs one time with all 5 |
183 | | - gutil.log('Starting', '\'' + chalk.cyan(e.task) + '\'...'); |
184 | | - }); |
185 | | - |
186 | | - gulpInst.on('task_stop', function(e) { |
187 | | - var time = prettyTime(e.hrDuration); |
188 | | - gutil.log( |
189 | | - 'Finished', '\'' + chalk.cyan(e.task) + '\'', |
190 | | - 'after', chalk.magenta(time) |
191 | | - ); |
192 | | - }); |
193 | | - |
194 | | - gulpInst.on('task_err', function(e) { |
195 | | - var msg = formatError(e); |
196 | | - var time = prettyTime(e.hrDuration); |
197 | | - gutil.log( |
198 | | - '\'' + chalk.cyan(e.task) + '\'', |
199 | | - chalk.red('errored after'), |
200 | | - chalk.magenta(time) |
201 | | - ); |
202 | | - gutil.log(msg); |
203 | | - }); |
204 | | - |
205 | | - gulpInst.on('task_not_found', function(err) { |
206 | | - gutil.log( |
207 | | - chalk.red('Task \'' + err.task + '\' is not in your gulpfile') |
208 | | - ); |
209 | | - gutil.log('Please check the documentation for proper gulpfile formatting'); |
210 | | - process.exit(1); |
211 | | - }); |
212 | | -} |
| 3 | +require('gulp-cli')(); |
0 commit comments