1 | #!/usr/bin/env node
|
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 v8flags = require('v8flags');
|
12 | var completion = require('../util/completion');
|
13 | var argv = require('minimist')(process.argv.slice(2));
|
14 | var taskTree = require('../util/task-tree');
|
15 |
|
16 |
|
17 |
|
18 | process.env.INIT_CWD = process.cwd();
|
19 |
|
20 | var cli = new Liftoff({
|
21 | name: 'pomy',
|
22 | completions: completion,
|
23 | v8flags: v8flags,
|
24 | processTitle: 'pomy',
|
25 | moduleName: 'pomy',
|
26 | configName: 'pomy',
|
27 | extensions: {
|
28 | '.json': null
|
29 | }
|
30 | });
|
31 |
|
32 |
|
33 | var failed = false;
|
34 | process.once('exit', function(code) {
|
35 | if (code === 0 && failed) {
|
36 | process.exit(1);
|
37 | }
|
38 | });
|
39 |
|
40 |
|
41 | var cliPackage = require('../package');
|
42 | var versionFlag = argv.v || argv.version;
|
43 | var tasksFlag = argv.T || argv.tasks;
|
44 | var tasks = argv._;
|
45 | var toRun = tasks.length ? tasks : ['default'];
|
46 |
|
47 |
|
48 |
|
49 | var simpleTasksFlag = argv['tasks-simple'];
|
50 | var shouldLog = !argv.silent && !simpleTasksFlag;
|
51 |
|
52 | if (!shouldLog) {
|
53 | gutil.log = function() {};
|
54 | }
|
55 |
|
56 | cli.on('require', function(name) {
|
57 | gutil.log('Requiring external module', chalk.magenta(name));
|
58 | });
|
59 |
|
60 | cli.on('requireFail', function(name) {
|
61 | gutil.log(chalk.red('Failed to load external module'), chalk.magenta(name));
|
62 | });
|
63 |
|
64 | cli.on('respawn', function(flags, child) {
|
65 | var nodeFlags = chalk.magenta(flags.join(', '));
|
66 | var pid = chalk.magenta(child.pid);
|
67 | gutil.log('Node flags detected:', nodeFlags);
|
68 | gutil.log('Respawned to PID:', pid);
|
69 | });
|
70 |
|
71 | cli.launch({
|
72 | cwd: argv.cwd,
|
73 | configPath: argv.configPath,
|
74 | require: argv.require,
|
75 | completion: argv.completion,
|
76 | }, handleArguments);
|
77 |
|
78 |
|
79 | function handleArguments(env) {
|
80 |
|
81 | if (versionFlag && tasks.length === 0) {
|
82 | gutil.log('CLI version', cliPackage.version);
|
83 | if (env.modulePackage && typeof env.modulePackage.version !== 'undefined') {
|
84 | gutil.log('Local version', env.modulePackage.version);
|
85 | }
|
86 | process.exit(0);
|
87 | }
|
88 |
|
89 | var archetype = false;
|
90 | if (tasks.length === 1 && tasks[0].indexOf('archetype:') === 0) {
|
91 | archetype = true;
|
92 | }
|
93 |
|
94 | if (!env.modulePath) {
|
95 | gutil.log(
|
96 | chalk.red('Local pomy not found in'),
|
97 | chalk.magenta(tildify(env.cwd))
|
98 | );
|
99 | gutil.log(chalk.red('Try running: npm install pomy'));
|
100 | process.exit(1);
|
101 | }
|
102 |
|
103 | if (!archetype && !env.configPath) {
|
104 | gutil.log(chalk.red('No pomy.json found'));
|
105 | process.exit(1);
|
106 | }
|
107 |
|
108 |
|
109 | if (semver.gt(cliPackage.version, env.modulePackage.version)) {
|
110 | gutil.log(chalk.red('Warning: pomy version mismatch:'));
|
111 | gutil.log(chalk.red('Global pomy is', cliPackage.version));
|
112 | gutil.log(chalk.red('Local pomy is', env.modulePackage.version));
|
113 | }
|
114 |
|
115 |
|
116 |
|
117 | if (process.cwd() !== env.cwd) {
|
118 | process.chdir(env.cwd);
|
119 | gutil.log(
|
120 | 'Working directory changed to',
|
121 | chalk.magenta(tildify(env.cwd))
|
122 | );
|
123 | }
|
124 |
|
125 |
|
126 |
|
127 | if (!archetype) {
|
128 | gutil.log('Using pomy.json', chalk.magenta(tildify(env.configPath)));
|
129 | }
|
130 |
|
131 | var pomyInst = require(env.modulePath);
|
132 | logEvents(pomyInst);
|
133 |
|
134 | process.nextTick(function() {
|
135 | if (simpleTasksFlag) {
|
136 | return logTasksSimple(env, pomyInst);
|
137 | }
|
138 | if (tasksFlag) {
|
139 | return logTasks(env, pomyInst);
|
140 | }
|
141 |
|
142 | if (tasks.length > 1) {
|
143 | switch (tasks[0]) {
|
144 | case 'install':
|
145 | case 'uninstall':
|
146 | toRun = ['bower:' + tasks[0]];
|
147 | break;
|
148 | case 'bower:install':
|
149 | case 'bower:uninstall':
|
150 | toRun = [tasks[0]];
|
151 | case 'update':
|
152 | case 'bower:update':
|
153 | toRun = tasks.slice(0, 1);
|
154 | break;
|
155 | default:
|
156 | break;
|
157 | }
|
158 | }
|
159 | pomyInst.start.apply(pomyInst, toRun);
|
160 | });
|
161 | }
|
162 |
|
163 | function logTasks(env, localGulp) {
|
164 | var tree = taskTree(localGulp.tasks);
|
165 | tree.label = 'Tasks for ' + chalk.magenta(tildify(env.configPath));
|
166 | archy(tree)
|
167 | .split('\n')
|
168 | .forEach(function(v) {
|
169 | if (v.trim().length === 0) {
|
170 | return;
|
171 | }
|
172 | gutil.log(v);
|
173 | });
|
174 | }
|
175 |
|
176 | function logTasksSimple(env, localGulp) {
|
177 | console.log(Object.keys(localGulp.tasks)
|
178 | .join('\n')
|
179 | .trim());
|
180 | }
|
181 |
|
182 |
|
183 | function formatError(e) {
|
184 | if (!e.err) {
|
185 | return e.message;
|
186 | }
|
187 |
|
188 |
|
189 | if (typeof e.err.showStack === 'boolean') {
|
190 | return e.err.toString();
|
191 | }
|
192 |
|
193 |
|
194 | if (e.err.stack) {
|
195 | return e.err.stack;
|
196 | }
|
197 |
|
198 |
|
199 | return new Error(String(e.err)).stack;
|
200 | }
|
201 |
|
202 |
|
203 | function logEvents(pomyInst) {
|
204 |
|
205 |
|
206 | pomyInst.on('err', function() {
|
207 | failed = true;
|
208 | });
|
209 |
|
210 | pomyInst.on('task_start', function(e) {
|
211 |
|
212 |
|
213 | gutil.log('Starting', '\'' + chalk.cyan(e.task) + '\'...');
|
214 | });
|
215 |
|
216 | pomyInst.on('task_stop', function(e) {
|
217 | var time = prettyTime(e.hrDuration);
|
218 | gutil.log(
|
219 | 'Finished', '\'' + chalk.cyan(e.task) + '\'',
|
220 | 'after', chalk.magenta(time)
|
221 | );
|
222 | });
|
223 |
|
224 | pomyInst.on('task_err', function(e) {
|
225 | var msg = formatError(e);
|
226 | var time = prettyTime(e.hrDuration);
|
227 | gutil.log(
|
228 | '\'' + chalk.cyan(e.task) + '\'',
|
229 | chalk.red('errored after'),
|
230 | chalk.magenta(time)
|
231 | );
|
232 | gutil.log(msg);
|
233 | });
|
234 |
|
235 | pomyInst.on('task_not_found', function(err) {
|
236 | gutil.log(
|
237 | chalk.red('Task \'' + err.task + '\' is not in your gulpfile')
|
238 | );
|
239 | gutil.log('Please check the documentation for proper gulpfile formatting');
|
240 | process.exit(1);
|
241 | });
|
242 | } |
\ | No newline at end of file |