Skip to content

Commit dfc5713

Browse files
committed
Update: Improve file structure & add semver version matching
1 parent 360381e commit dfc5713

21 files changed

+117
-88
lines changed

index.js

Lines changed: 17 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env node
22

33
'use strict';
4+
var fs = require('fs');
45
var path = require('path');
56
var gutil = require('gulp-util');
67
var chalk = require('chalk');
@@ -10,16 +11,20 @@ var Liftoff = require('liftoff');
1011
var tildify = require('tildify');
1112
var interpret = require('interpret');
1213
var v8flags = require('v8flags');
13-
var exit = require('./lib/exit');
14-
var cliOptions = require('./lib/cliOptions');
15-
var completion = require('./lib/completion');
16-
var verifyDeps = require('./lib/verifyDependencies');
14+
var findRange = require('semver-greatest-satisfied-range');
15+
var exit = require('./lib/shared/exit');
16+
var cliOptions = require('./lib/shared/cliOptions');
17+
var completion = require('./lib/shared/completion');
18+
var verifyDeps = require('./lib/shared/verifyDependencies');
1719
var cliVersion = require('./package.json').version;
18-
var getBlacklist = require('./lib/getBlacklist');
20+
var getBlacklist = require('./lib/shared/getBlacklist');
1921

2022
// Logging functions
21-
var logVerify = require('./lib/log/verify');
22-
var logBlacklistError = require('./lib/log/blacklistError');
23+
var logVerify = require('./lib/shared/log/verify');
24+
var logBlacklistError = require('./lib/shared/log/blacklistError');
25+
26+
// Get supported ranges
27+
var ranges = fs.readdirSync(__dirname + '/lib/versioned/');
2328

2429
// Set env var for ORIGINAL cwd
2530
// before anything touches it
@@ -36,8 +41,6 @@ var opts = nomnom
3641
.script('gulp')
3742
.options(cliOptions)
3843
.parse();
39-
var tasks = opts._;
40-
var toRun = tasks.length ? tasks : ['default'];
4144

4245
// This is a hold-over until we have a better logging system
4346
// with log levels
@@ -115,6 +118,7 @@ function handleArguments(env) {
115118
}
116119

117120
// Check for semver difference between cli and local installation
121+
// TODO: remove when we support all
118122
if (semver.gt(cliVersion, env.modulePackage.version)) {
119123
gutil.log(chalk.red('Warning: gulp version mismatch:'));
120124
gutil.log(chalk.red('Global gulp is', cliVersion));
@@ -131,75 +135,9 @@ function handleArguments(env) {
131135
);
132136
}
133137

134-
if (semver.satisfies(env.modulePackage.version, '^3')) {
135-
return gulp3(env);
136-
}
137-
138-
if (semver.satisfies(env.modulePackage.version, '^4')) {
139-
return gulp4(env);
140-
}
141-
}
142-
143-
function gulp3(env) {
144-
var logTasks = require('./lib/3/logTasks');
145-
var logEvents = require('./lib/3/logEvents');
146-
var logTasksSimple = require('./lib/3/logTasksSimple');
147-
148-
// This is what actually loads up the gulpfile
149-
require(env.configPath);
150-
gutil.log('Using gulpfile', chalk.magenta(tildify(env.configPath)));
151-
152-
var gulpInst = require(env.modulePath);
153-
logEvents(gulpInst);
138+
// Find the correct CLI version to run
139+
var range = findRange(env.modulePackage.version, ranges);
154140

155-
process.nextTick(function() {
156-
if (opts.tasksSimple) {
157-
return logTasksSimple(env, gulpInst);
158-
}
159-
if (opts.tasks) {
160-
return logTasks(env, gulpInst);
161-
}
162-
gulpInst.start.apply(gulpInst, toRun);
163-
});
164-
}
165-
166-
function gulp4(env) {
167-
var logTasks = require('./lib/log/tasks');
168-
var logEvents = require('./lib/log/events');
169-
var logTasksSimple = require('./lib/log/tasksSimple');
170-
171-
var gulpInst = require(env.modulePath);
172-
logEvents(gulpInst);
173-
174-
// This is what actually loads up the gulpfile
175-
require(env.configPath);
176-
177-
process.nextTick(function() {
178-
if (opts.tasksSimple) {
179-
return logTasksSimple(gulpInst.tree());
180-
}
181-
if (opts.tasks) {
182-
var tree = {
183-
label: 'Tasks for ' + chalk.magenta(tildify(env.configPath)),
184-
nodes: gulpInst.tree({ deep: true }),
185-
};
186-
return logTasks(tree);
187-
}
188-
if (opts.tasksJson) {
189-
return console.log(
190-
JSON.stringify(gulpInst.tree({ deep: true }), null, 2)
191-
);
192-
}
193-
try {
194-
gutil.log('Using gulpfile', chalk.magenta(tildify(env.configPath)));
195-
// TODO: do we care about the error/result from calling this?
196-
gulpInst.parallel(toRun)();
197-
} catch (err) {
198-
gutil.log(chalk.red(err.message));
199-
gutil.log(
200-
'Please check the documentation for proper gulpfile formatting'
201-
);
202-
exit(1);
203-
}
204-
});
141+
// Load and execute the CLI version
142+
require(path.join(__dirname, '/lib/versioned/', range, '/'))(opts, env);
205143
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

lib/log/blacklistError.js renamed to lib/shared/log/blacklistError.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
var chalk = require('chalk');
44
var gutil = require('gulp-util');
55

6-
var formatError = require('../formatError');
6+
var exit = require('../exit');
77

88
function logBlacklistError(err) {
99
gutil.log(chalk.red('Error: failed to retrieve plugins black-list'));
10-
gutil.log(formatError(err));
11-
process.exit(1);
10+
gutil.log(err.message); // Avoid duplicating for each version
11+
exit(1);
1212
}
1313

1414
module.exports = logBlacklistError;

lib/log/verify.js renamed to lib/shared/log/verify.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
var chalk = require('chalk');
44
var gutil = require('gulp-util');
55

6+
var exit = require('../exit');
7+
68
function logVerify(blacklisted) {
79
var pluginNames = Object.keys(blacklisted);
810

911
if (!pluginNames.length) {
1012
gutil.log(
1113
chalk.green('There are no blacklisted plugins in this project')
1214
);
13-
process.exit(0);
15+
exit(0);
1416
}
1517

1618
gutil.log(chalk.red('Blacklisted plugins found in this project:'));
@@ -20,7 +22,7 @@ function logVerify(blacklisted) {
2022
gutil.log(chalk.bgRed(pluginName) + ': ' + reason);
2123
});
2224

23-
process.exit(1);
25+
exit(1);
2426
}
2527

2628
module.exports = logVerify;
File renamed without changes.
File renamed without changes.

lib/versioned/^3.7.0/index.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
var chalk = require('chalk');
4+
var gutil = require('gulp-util');
5+
var tildify = require('tildify');
6+
7+
var logTasks = require('./log/tasks');
8+
var logEvents = require('./log/events');
9+
var logTasksSimple = require('./log/tasksSimple');
10+
11+
function execute(opts, env) {
12+
13+
var tasks = opts._;
14+
var toRun = tasks.length ? tasks : ['default'];
15+
16+
// This is what actually loads up the gulpfile
17+
require(env.configPath);
18+
gutil.log('Using gulpfile', chalk.magenta(tildify(env.configPath)));
19+
20+
var gulpInst = require(env.modulePath);
21+
logEvents(gulpInst);
22+
23+
process.nextTick(function() {
24+
if (opts.tasksSimple) {
25+
return logTasksSimple(env, gulpInst);
26+
}
27+
if (opts.tasks) {
28+
return logTasks(env, gulpInst);
29+
}
30+
gulpInst.start.apply(gulpInst, toRun);
31+
});
32+
}
33+
34+
module.exports = execute;

0 commit comments

Comments
 (0)