UNPKG

4.72 kBJavaScriptView Raw
1'use strict';
2
3var fs = require('fs');
4var path = require('path');
5var log = require('gulplog');
6var chalk = require('chalk');
7var yargs = require('yargs');
8var Liftoff = require('liftoff');
9var tildify = require('tildify');
10var interpret = require('interpret');
11var v8flags = require('v8flags');
12var findRange = require('semver-greatest-satisfied-range');
13var exit = require('./lib/shared/exit');
14var cliOptions = require('./lib/shared/cliOptions');
15var completion = require('./lib/shared/completion');
16var verifyDeps = require('./lib/shared/verifyDependencies');
17var cliVersion = require('./package.json').version;
18var getBlacklist = require('./lib/shared/getBlacklist');
19var toConsole = require('./lib/shared/log/toConsole');
20
21var loadConfigFiles = require('./lib/shared/config/load-files');
22var mergeConfigToCliFlags = require('./lib/shared/config/cli-flags');
23var mergeConfigToEnvFlags = require('./lib/shared/config/env-flags');
24
25// Logging functions
26var logVerify = require('./lib/shared/log/verify');
27var logBlacklistError = require('./lib/shared/log/blacklistError');
28
29// Get supported ranges
30var ranges = fs.readdirSync(__dirname + '/lib/versioned/');
31
32// Set env var for ORIGINAL cwd
33// before anything touches it
34process.env.INIT_CWD = process.cwd();
35
36var cli = new Liftoff({
37 name: 'gulp',
38 completions: completion,
39 extensions: interpret.jsVariants,
40 v8flags: v8flags,
41 configFiles: {
42 '.gulp': {
43 home: {
44 path: '~',
45 extensions: interpret.extensions,
46 },
47 cwd: {
48 path: '.',
49 extensions: interpret.extensions,
50 },
51 },
52 },
53});
54
55var usage =
56 '\n' + chalk.bold('Usage:') +
57 ' gulp ' + chalk.blue('[options]') + ' tasks';
58
59var parser = yargs.usage(usage, cliOptions);
60var opts = parser.argv;
61
62// Set up event listeners for logging temporarily.
63toConsole(log, opts);
64
65cli.on('require', function(name) {
66 log.info('Requiring external module', chalk.magenta(name));
67});
68
69cli.on('requireFail', function(name) {
70 log.error(chalk.red('Failed to load external module'), chalk.magenta(name));
71});
72
73cli.on('respawn', function(flags, child) {
74 var nodeFlags = chalk.magenta(flags.join(', '));
75 var pid = chalk.magenta(child.pid);
76 log.info('Node flags detected:', nodeFlags);
77 log.info('Respawned to PID:', pid);
78});
79
80function run() {
81 cli.launch({
82 cwd: opts.cwd,
83 configPath: opts.gulpfile,
84 require: opts.require,
85 completion: opts.completion,
86 }, handleArguments);
87}
88
89module.exports = run;
90
91// The actual logic
92function handleArguments(env) {
93 var cfgLoadOrder = ['home', 'cwd'];
94 var cfg = loadConfigFiles(env.configFiles['.gulp'], cfgLoadOrder);
95 opts = mergeConfigToCliFlags(opts, cfg);
96 env = mergeConfigToEnvFlags(env, cfg);
97
98 // This translates the --continue flag in gulp
99 // To the settle env variable for undertaker
100 // We use the process.env so the user's gulpfile
101 // Can know about the flag
102 if (opts.continue) {
103 process.env.UNDERTAKER_SETTLE = 'true';
104 }
105
106 // Set up event listeners for logging again after configuring.
107 toConsole(log, opts);
108
109 if (opts.help) {
110 console.log(parser.help());
111 exit(0);
112 }
113
114 if (opts.version) {
115 log.info('CLI version', cliVersion);
116 if (env.modulePackage && typeof env.modulePackage.version !== 'undefined') {
117 log.info('Local version', env.modulePackage.version);
118 }
119 exit(0);
120 }
121
122 if (opts.verify) {
123 var pkgPath = opts.verify !== true ? opts.verify : 'package.json';
124 if (path.resolve(pkgPath) !== path.normalize(pkgPath)) {
125 pkgPath = path.join(env.cwd, pkgPath);
126 }
127 log.info('Verifying plugins in ' + pkgPath);
128 return getBlacklist(function(err, blacklist) {
129 if (err) {
130 return logBlacklistError(err);
131 }
132
133 var blacklisted = verifyDeps(require(pkgPath), blacklist);
134
135 logVerify(blacklisted);
136 });
137 }
138
139 if (!env.modulePath) {
140 log.error(
141 chalk.red('Local gulp not found in'),
142 chalk.magenta(tildify(env.cwd))
143 );
144 log.error(chalk.red('Try running: npm install gulp'));
145 exit(1);
146 }
147
148 if (!env.configPath) {
149 log.error(chalk.red('No gulpfile found'));
150 exit(1);
151 }
152
153 // Chdir before requiring gulpfile to make sure
154 // we let them chdir as needed
155 if (process.cwd() !== env.cwd) {
156 process.chdir(env.cwd);
157 log.info(
158 'Working directory changed to',
159 chalk.magenta(tildify(env.cwd))
160 );
161 }
162
163 // Find the correct CLI version to run
164 var range = findRange(env.modulePackage.version, ranges);
165
166 if (!range) {
167 return log.error(
168 chalk.red('Unsupported gulp version', env.modulePackage.version)
169 );
170 }
171
172 // Load and execute the CLI version
173 require(path.join(__dirname, '/lib/versioned/', range, '/'))(opts, env, cfg);
174}