Skip to content

Commit

Permalink
Update: Improve file structure & add semver version matching
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Dec 21, 2017
1 parent 360381e commit dfc5713
Show file tree
Hide file tree
Showing 21 changed files with 117 additions and 88 deletions.
96 changes: 17 additions & 79 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node

'use strict';
var fs = require('fs');
var path = require('path');
var gutil = require('gulp-util');
var chalk = require('chalk');
Expand All @@ -10,16 +11,20 @@ var Liftoff = require('liftoff');
var tildify = require('tildify');
var interpret = require('interpret');
var v8flags = require('v8flags');
var exit = require('./lib/exit');
var cliOptions = require('./lib/cliOptions');
var completion = require('./lib/completion');
var verifyDeps = require('./lib/verifyDependencies');
var findRange = require('semver-greatest-satisfied-range');
var exit = require('./lib/shared/exit');
var cliOptions = require('./lib/shared/cliOptions');
var completion = require('./lib/shared/completion');
var verifyDeps = require('./lib/shared/verifyDependencies');
var cliVersion = require('./package.json').version;
var getBlacklist = require('./lib/getBlacklist');
var getBlacklist = require('./lib/shared/getBlacklist');

// Logging functions
var logVerify = require('./lib/log/verify');
var logBlacklistError = require('./lib/log/blacklistError');
var logVerify = require('./lib/shared/log/verify');
var logBlacklistError = require('./lib/shared/log/blacklistError');

// Get supported ranges
var ranges = fs.readdirSync(__dirname + '/lib/versioned/');

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

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

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

if (semver.satisfies(env.modulePackage.version, '^3')) {
return gulp3(env);
}

if (semver.satisfies(env.modulePackage.version, '^4')) {
return gulp4(env);
}
}

function gulp3(env) {
var logTasks = require('./lib/3/logTasks');
var logEvents = require('./lib/3/logEvents');
var logTasksSimple = require('./lib/3/logTasksSimple');

// This is what actually loads up the gulpfile
require(env.configPath);
gutil.log('Using gulpfile', chalk.magenta(tildify(env.configPath)));

var gulpInst = require(env.modulePath);
logEvents(gulpInst);
// Find the correct CLI version to run
var range = findRange(env.modulePackage.version, ranges);

process.nextTick(function() {
if (opts.tasksSimple) {
return logTasksSimple(env, gulpInst);
}
if (opts.tasks) {
return logTasks(env, gulpInst);
}
gulpInst.start.apply(gulpInst, toRun);
});
}

function gulp4(env) {
var logTasks = require('./lib/log/tasks');
var logEvents = require('./lib/log/events');
var logTasksSimple = require('./lib/log/tasksSimple');

var gulpInst = require(env.modulePath);
logEvents(gulpInst);

// This is what actually loads up the gulpfile
require(env.configPath);

process.nextTick(function() {
if (opts.tasksSimple) {
return logTasksSimple(gulpInst.tree());
}
if (opts.tasks) {
var tree = {
label: 'Tasks for ' + chalk.magenta(tildify(env.configPath)),
nodes: gulpInst.tree({ deep: true }),
};
return logTasks(tree);
}
if (opts.tasksJson) {
return console.log(
JSON.stringify(gulpInst.tree({ deep: true }), null, 2)
);
}
try {
gutil.log('Using gulpfile', chalk.magenta(tildify(env.configPath)));
// TODO: do we care about the error/result from calling this?
gulpInst.parallel(toRun)();
} catch (err) {
gutil.log(chalk.red(err.message));
gutil.log(
'Please check the documentation for proper gulpfile formatting'
);
exit(1);
}
});
// Load and execute the CLI version
require(path.join(__dirname, '/lib/versioned/', range, '/'))(opts, env);
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
var chalk = require('chalk');
var gutil = require('gulp-util');

var formatError = require('../formatError');
var exit = require('../exit');

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

module.exports = logBlacklistError;
6 changes: 4 additions & 2 deletions lib/log/verify.js → lib/shared/log/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
var chalk = require('chalk');
var gutil = require('gulp-util');

var exit = require('../exit');

function logVerify(blacklisted) {
var pluginNames = Object.keys(blacklisted);

if (!pluginNames.length) {
gutil.log(
chalk.green('There are no blacklisted plugins in this project')
);
process.exit(0);
exit(0);
}

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

process.exit(1);
exit(1);
}

module.exports = logVerify;
File renamed without changes.
File renamed without changes.
34 changes: 34 additions & 0 deletions lib/versioned/^3.7.0/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

var chalk = require('chalk');
var gutil = require('gulp-util');
var tildify = require('tildify');

var logTasks = require('./log/tasks');
var logEvents = require('./log/events');
var logTasksSimple = require('./log/tasksSimple');

function execute(opts, env) {

var tasks = opts._;
var toRun = tasks.length ? tasks : ['default'];

// This is what actually loads up the gulpfile
require(env.configPath);
gutil.log('Using gulpfile', chalk.magenta(tildify(env.configPath)));

var gulpInst = require(env.modulePath);
logEvents(gulpInst);

process.nextTick(function() {
if (opts.tasksSimple) {
return logTasksSimple(env, gulpInst);
}
if (opts.tasks) {
return logTasks(env, gulpInst);
}
gulpInst.start.apply(gulpInst, toRun);
});
}

module.exports = execute;
4 changes: 2 additions & 2 deletions lib/3/logEvents.js → lib/versioned/^3.7.0/log/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ var chalk = require('chalk');
var gutil = require('gulp-util');
var prettyTime = require('pretty-hrtime');

var exit = require('../exit');
var formatError = require('./formatError');
var exit = require('../../../shared/exit');
var formatError = require('../formatError');

// Wire up logging events
function logEvents(gulpInst) {
Expand Down
2 changes: 1 addition & 1 deletion lib/3/logTasks.js → lib/versioned/^3.7.0/log/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var chalk = require('chalk');
var gutil = require('gulp-util');
var tildify = require('tildify');

var taskTree = require('./taskTree');
var taskTree = require('../taskTree');

function logTasks(env, localGulp) {
var tree = taskTree(localGulp.tasks);
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
54 changes: 54 additions & 0 deletions lib/versioned/^4.0.0-alpha.1/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

var chalk = require('chalk');
var gutil = require('gulp-util');
var tildify = require('tildify');

var exit = require('../../shared/exit');

var logTasks = require('./log/tasks');
var logEvents = require('./log/events');
var logTasksSimple = require('./log/tasksSimple');

function execute(opts, env) {

var tasks = opts._;
var toRun = tasks.length ? tasks : ['default'];

var gulpInst = require(env.modulePath);
logEvents(gulpInst);

// This is what actually loads up the gulpfile
require(env.configPath);

process.nextTick(function() {
if (opts.tasksSimple) {
return logTasksSimple(gulpInst.tree());
}
if (opts.tasks) {
var tree = {
label: 'Tasks for ' + chalk.magenta(tildify(env.configPath)),
nodes: gulpInst.tree({ deep: true }),
};
return logTasks(tree);
}
if (opts.tasksJson) {
return console.log(
JSON.stringify(gulpInst.tree({ deep: true }), null, 2)
);
}
try {
gutil.log('Using gulpfile', chalk.magenta(tildify(env.configPath)));
// TODO: do we care about the error/result from calling this?
gulpInst.parallel(toRun)();
} catch (err) {
gutil.log(chalk.red(err.message));
gutil.log(
'Please check the documentation for proper gulpfile formatting'
);
exit(1);
}
});
}

module.exports = execute;
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"nomnom": "^1.8.0",
"pretty-hrtime": "^1.0.0",
"semver": "^5.0.0",
"semver-greatest-satisfied-range": "^1.0.0",
"tildify": "^1.0.0",
"v8flags": "^2.0.9",
"wreck": "^5.0.1"
Expand Down
2 changes: 1 addition & 1 deletion test/taskTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var lab = exports.lab = require('lab').script();
var code = require('code');

var taskTree = require('../lib/3/taskTree');
var taskTree = require('../lib/versioned/^3.7.0/taskTree');

lab.experiment('taskTree()', function() {

Expand Down

0 comments on commit dfc5713

Please sign in to comment.