Skip to content

Commit

Permalink
Scaffold: Import jscs from gulp & fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Dec 21, 2017
1 parent 86bcf42 commit 76d6ecf
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 74 deletions.
48 changes: 48 additions & 0 deletions .jscsrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"disallowKeywords": ["with"],
"disallowKeywordsOnNewLine": ["else"],
"disallowMixedSpacesAndTabs": true,
"disallowMultipleVarDecl": "exceptUndefined",
"disallowNewlineBeforeBlockStatements": true,
"disallowQuotedKeysInObjects": true,
"disallowSpaceAfterObjectKeys": true,
"disallowSpaceAfterPrefixUnaryOperators": true,
"disallowSpacesInFunction": {
"beforeOpeningRoundBrace": true
},
"disallowSpacesInsideParentheses": true,
"disallowTrailingWhitespace": true,
"maximumLineLength": null,
"requireCamelCaseOrUpperCaseIdentifiers": true,
"requireCapitalizedComments": true,
"requireCapitalizedConstructors": true,
"requireCurlyBraces": true,
"requireSpaceAfterKeywords": [
"if",
"else",
"for",
"while",
"do",
"switch",
"case",
"return",
"try",
"catch",
"typeof"
],
"requireSpaceAfterLineComment": true,
"requireSpaceAfterBinaryOperators": true,
"requireSpaceBeforeBinaryOperators": true,
"requireSpaceBeforeBlockStatements": true,
"requireSpaceBeforeObjectValues": true,
"requireSpacesInFunction": {
"beforeOpeningCurlyBrace": true
},
"requireTrailingComma": {
"ignoreSingleLine": true
},
"requireBlocksOnNewline": true,
"validateIndentation": 2,
"validateLineBreaks": "LF",
"validateQuoteMarks": "'"
}
85 changes: 42 additions & 43 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,50 @@ var completion = require('./lib/completion');
var argv = require('minimist')(process.argv.slice(2));
var taskTree = require('./lib/taskTree');

// set env var for ORIGINAL cwd
// Set env var for ORIGINAL cwd
// before anything touches it
process.env.INIT_CWD = process.cwd();

var cli = new Liftoff({
name: 'gulp',
completions: completion,
extensions: interpret.jsVariants,
v8flags: v8flags
v8flags: v8flags,
});

// exit with 0 or 1
// Exit with 0 or 1
var failed = false;
process.once('exit', function(code) {
if (code === 0 && failed) {
exit(1);
}
});

// parse those args m8
// Parse those args m8
var cliPackage = require('./package');
var versionFlag = argv.v || argv.version;
var tasksFlag = argv.T || argv.tasks;
var tasks = argv._;
var toRun = tasks.length ? tasks : ['default'];

// this is a hold-over until we have a better logging system
// This is a hold-over until we have a better logging system
// with log levels
var simpleTasksFlag = argv['tasks-simple'];
var shouldLog = !argv.silent && !simpleTasksFlag;

if (!shouldLog) {
gutil.log = function(){};
gutil.log = function() {};
}

cli.on('require', function (name) {
cli.on('require', function(name) {
gutil.log('Requiring external module', chalk.magenta(name));
});

cli.on('requireFail', function (name) {
cli.on('requireFail', function(name) {
gutil.log(chalk.red('Failed to load external module'), chalk.magenta(name));
});

cli.on('respawn', function (flags, child) {
cli.on('respawn', function(flags, child) {
var nodeFlags = chalk.magenta(flags.join(', '));
var pid = chalk.magenta(child.pid);
gutil.log('Node flags detected:', nodeFlags);
Expand All @@ -69,13 +69,13 @@ function run() {
cwd: argv.cwd,
configPath: argv.gulpfile,
require: argv.require,
completion: argv.completion
completion: argv.completion,
}, handleArguments);
}

module.exports = run;

// the actual logic
// The actual logic
function handleArguments(env) {
if (versionFlag && tasks.length === 0) {
gutil.log('CLI version', cliPackage.version);
Expand All @@ -99,14 +99,14 @@ function handleArguments(env) {
exit(1);
}

// check for semver difference between cli and local installation
// Check for semver difference between cli and local installation
if (semver.gt(cliPackage.version, env.modulePackage.version)) {
gutil.log(chalk.red('Warning: gulp version mismatch:'));
gutil.log(chalk.red('Global gulp is', cliPackage.version));
gutil.log(chalk.red('Local gulp is', env.modulePackage.version));
}

// chdir before requiring gulpfile to make sure
// Chdir before requiring gulpfile to make sure
// we let them chdir as needed
if (process.cwd() !== env.cwd) {
process.chdir(env.cwd);
Expand All @@ -116,14 +116,14 @@ function handleArguments(env) {
);
}

// this is what actually loads up the gulpfile
// 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 () {
process.nextTick(function() {
if (simpleTasksFlag) {
return logTasksSimple(env, gulpInst);
}
Expand All @@ -141,34 +141,33 @@ function logTasks(env, localGulp) {
tree.label = 'Tasks for ' + chalk.magenta(tildify(env.configPath));
archy(tree)
.split('\n')
.filter(function (v, i) {
// log first line as is
if ( i === 0 ) {
.filter(function(v, i) {
// Log first line as is
if (i === 0) {
gutil.log(v);
return false;
return false;
}
// search for longest line
if ( v.length > padding ) {
// Search for longest line
if (v.length > padding) {
padding = v.length;
}
return v.trim().length !== 0;

}).forEach(function (v) {
}).forEach(function(v) {
var line = v.split(' ');
var task = line.slice(1).join(' ');

// log dependencies as is
if ( rdependency.test(v) ) {
// Log dependencies as is
if (rdependency.test(v)) {
gutil.log(v);
// pretty task with optionnal description
} else {
gutil.log(
line[0] + ' ' +
chalk.cyan(task) +
Array( padding + 3 - v.length ).join(' ') +
( localGulp.tasks[task].fn.description || '' )
);
return;
}

// Pretty task with optional description
gutil.log(
line[0] + ' ' + chalk.cyan(task) +
Array(padding + 3 - v.length).join(' ') +
(localGulp.tasks[task].fn.description || '')
);
});
}

Expand All @@ -178,7 +177,7 @@ function logTasksSimple(env, localGulp) {
.trim());
}

// format orchestrator errors
// Format orchestrator errors
function formatError(e) {
if (!e.err) {
return e.message;
Expand All @@ -189,38 +188,38 @@ function formatError(e) {
return e.err.toString();
}

// normal error
// Normal error
if (e.err.stack) {
return e.err.stack;
}

// unknown (string, number, etc.)
// Unknown (string, number, etc.)
return new Error(String(e.err)).stack;
}

// wire up logging events
// Wire up logging events
function logEvents(gulpInst) {

// total hack due to poor error management in orchestrator
gulpInst.on('err', function () {
gulpInst.on('err', function() {
failed = true;
});

gulpInst.on('task_start', function (e) {
gulpInst.on('task_start', function(e) {
// TODO: batch these
// so when 5 tasks start at once it only logs one time with all 5
gutil.log('Starting', '\'' + chalk.cyan(e.task) + '\'...');
});

gulpInst.on('task_stop', function (e) {
gulpInst.on('task_stop', function(e) {
var time = prettyTime(e.hrDuration);
gutil.log(
'Finished', '\'' + chalk.cyan(e.task) + '\'',
'after', chalk.magenta(time)
);
});

gulpInst.on('task_err', function (e) {
gulpInst.on('task_err', function(e) {
var msg = formatError(e);
var time = prettyTime(e.hrDuration);
gutil.log(
Expand All @@ -231,7 +230,7 @@ function logEvents(gulpInst) {
gutil.log(msg);
});

gulpInst.on('task_not_found', function (err) {
gulpInst.on('task_not_found', function(err) {
gutil.log(
chalk.red('Task \'' + err.task + '\' is not in your gulpfile')
);
Expand All @@ -240,7 +239,7 @@ function logEvents(gulpInst) {
});
}

// fix stdout truncation on windows
// Fix stdout truncation on windows
function exit(code) {
if (process.platform === 'win32' && process.stdout.bufferSize) {
process.stdout.once('drain', function() {
Expand Down
2 changes: 1 addition & 1 deletion lib/completion.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var fs = require('fs');
var path = require('path');

module.exports = function (name) {
module.exports = function(name) {
if (typeof name !== 'string') {
throw new Error('Missing completion type');
}
Expand Down
8 changes: 4 additions & 4 deletions lib/taskTree.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict';

module.exports = function (tasks) {
module.exports = function(tasks) {
return Object.keys(tasks)
.reduce(function (prev, task) {
.reduce(function(prev, task) {
prev.nodes.push({
label: task,
nodes: tasks[task].dep
nodes: tasks[task].dep,
});
return prev;
}, {
nodes: []
nodes: [],
});
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@
"coveralls": "^2.7.0",
"gulp": ">=3.8.10",
"istanbul": "^0.3.0",
"jscs": "^1.11.3",
"jshint": "^2.5.0",
"jshint-stylish": "^1.0.0",
"lab": "^5.1.0",
"marked-man": "^0.1.3"
},
"scripts": {
"prepublish": "marked-man --name gulp docs/CLI.md > gulp.1",
"lint": "jshint lib index.js --reporter node_modules/jshint-stylish/stylish.js --exclude node_modules",
"lint": "jshint lib index.js --reporter node_modules/jshint-stylish/stylish.js --exclude node_modules && jscs index.js lib bin test",
"test": "npm run-script lint && lab -cvm 5000",
"coveralls": "istanbul cover _mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage"
},
Expand Down
4 changes: 2 additions & 2 deletions test/es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ var code = require('code');

var child = require('child_process');

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

lab.test('supports es6', function (done) {
lab.test('supports es6', function(done) {
child.exec('node ' + __dirname + '/../bin/gulp.js --cwd ./test --gulpfile test/gulpfile.es6', function(err, stdout) {
code.expect(stdout).to.contain('Requiring external module 6to5/register');
done(err);
Expand Down
4 changes: 2 additions & 2 deletions test/flags-task-simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ var child = require('child_process');

var output = fs.readFileSync(__dirname + '/expected/flags-tasks-simple.txt', 'utf8').replace(/\r\n/g, '\n');

lab.experiment('flag: --tasks-simple', function () {
lab.experiment('flag: --tasks-simple', function() {

lab.test('prints the task list in simple format', function (done) {
lab.test('prints the task list in simple format', function(done) {
child.exec('node ' + __dirname + '/../bin/gulp.js --tasks-simple --cwd ./test', function(err, stdout) {
code.expect(stdout).to.equal(output);
done(err);
Expand Down
4 changes: 2 additions & 2 deletions test/flags-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ var code = require('code');

var child = require('child_process');

lab.experiment('flag: --tasks', function () {
lab.experiment('flag: --tasks', function() {

lab.test('prints the task list', function (done) {
lab.test('prints the task list', function(done) {
child.exec('node ' + __dirname + '/../bin/gulp.js --tasks --cwd ./test', function(err, stdout) {
code.expect(stdout).to.contain('Tasks for');
stdout = stdout.replace(/\\/g, '/').split('Tasks for')[1].split('\n');
Expand Down
4 changes: 2 additions & 2 deletions test/flags-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ var child = require('child_process');
var cliVersion = require('../package.json').version;
var gulpVersion = require('gulp/package.json').version;

lab.experiment('flag: --version', function () {
lab.experiment('flag: --version', function() {

lab.test('prints the version of CLI and local gulp', function (done) {
lab.test('prints the version of CLI and local gulp', function(done) {
child.exec('node ' + __dirname + '/../bin/gulp.js --version --cwd ./test', function(err, stdout) {
code.expect(stdout).to.contain('CLI version ' + cliVersion);
code.expect(stdout).to.contain('Local version ' + gulpVersion);
Expand Down
4 changes: 2 additions & 2 deletions test/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

var gulp = require('gulp');

function noop(){}
function described(){}
function noop() {}
function described() {}
described.description = 'description';

gulp.task('test1', noop);
Expand Down

0 comments on commit 76d6ecf

Please sign in to comment.