Skip to content

Commit

Permalink
Bring JSCS to Gulp. Fixes gulpjs#904
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjoelkemp committed Mar 15, 2015
1 parent 84df40b commit a54996c
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 107 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": "'"
}
52 changes: 26 additions & 26 deletions bin/gulp.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) {
process.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 @@ -68,10 +68,10 @@ cli.launch({
cwd: argv.cwd,
configPath: argv.gulpfile,
require: argv.require,
completion: argv.completion
completion: argv.completion,
}, handleArguments);

// the actual logic
// The actual logic
function handleArguments(env) {
if (versionFlag && tasks.length === 0) {
gutil.log('CLI version', cliPackage.version);
Expand All @@ -95,14 +95,14 @@ function handleArguments(env) {
process.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 @@ -112,14 +112,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 @@ -135,7 +135,7 @@ function logTasks(env, localGulp) {
tree.label = 'Tasks for ' + chalk.magenta(tildify(env.configPath));
archy(tree)
.split('\n')
.forEach(function (v) {
.forEach(function(v) {
if (v.trim().length === 0) {
return;
}
Expand All @@ -149,7 +149,7 @@ function logTasksSimple(env, localGulp) {
.trim());
}

// format orchestrator errors
// Format orchestrator errors
function formatError(e) {
if (!e.err) {
return e.message;
Expand All @@ -160,38 +160,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 () {
// Total hack due to poor error management in orchestrator
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 @@ -202,7 +202,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 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 @@ -43,6 +43,7 @@
"coveralls": "^2.7.0",
"graceful-fs": "^3.0.0",
"istanbul": "^0.3.0",
"jscs": "~1.11.3",
"jshint": "^2.5.0",
"jshint-stylish": "^1.0.0",
"marked-man": "^0.1.3",
Expand All @@ -55,7 +56,7 @@
},
"scripts": {
"prepublish": "marked-man --name gulp docs/CLI.md > gulp.1",
"lint": "jshint lib bin index.js --reporter node_modules/jshint-stylish/stylish.js --exclude node_modules",
"lint": "jshint lib bin index.js --reporter node_modules/jshint-stylish/stylish.js --exclude node_modules && jscs lib bin test",
"test": "npm run-script lint && mocha --reporter spec",
"coveralls": "istanbul cover _mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage"
},
Expand Down
40 changes: 20 additions & 20 deletions test/dest.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,59 @@ var fs = require('graceful-fs');

require('mocha');

var outpath = join(__dirname, "./out-fixtures");
var outpath = join(__dirname, './out-fixtures');

describe('gulp output stream', function() {
describe('dest()', function() {
beforeEach(rimraf.bind(null, outpath));
afterEach(rimraf.bind(null, outpath));

it('should return a stream', function(done) {
var stream = gulp.dest(join(__dirname, "./fixtures/"));
var stream = gulp.dest(join(__dirname, './fixtures/'));
should.exist(stream);
should.exist(stream.on);
done();
});

it('should return a output stream that writes files', function(done) {
var instream = gulp.src(join(__dirname, "./fixtures/**/*.txt"));
var instream = gulp.src(join(__dirname, './fixtures/**/*.txt'));
var outstream = gulp.dest(outpath);
instream.pipe(outstream);

outstream.on('error', done);
outstream.on('data', function(file) {
// data should be re-emitted right
// Data should be re-emitted right
should.exist(file);
should.exist(file.path);
should.exist(file.contents);
join(file.path,'').should.equal(join(outpath, "./copy/example.txt"));
String(file.contents).should.equal("this is a test");
join(file.path, '').should.equal(join(outpath, './copy/example.txt'));
String(file.contents).should.equal('this is a test');
});
outstream.on('end', function() {
fs.readFile(join(outpath, "copy", "example.txt"), function(err, contents) {
fs.readFile(join(outpath, 'copy', 'example.txt'), function(err, contents) {
should.not.exist(err);
should.exist(contents);
String(contents).should.equal("this is a test");
String(contents).should.equal('this is a test');
done();
});
});
});

it('should return a output stream that does not write non-read files', function(done) {
var instream = gulp.src(join(__dirname, "./fixtures/**/*.txt"), {read:false});
var instream = gulp.src(join(__dirname, './fixtures/**/*.txt'), {read: false});
var outstream = gulp.dest(outpath);
instream.pipe(outstream);

outstream.on('error', done);
outstream.on('data', function(file) {
// data should be re-emitted right
// Data should be re-emitted right
should.exist(file);
should.exist(file.path);
should.not.exist(file.contents);
join(file.path,'').should.equal(join(outpath, "./copy/example.txt"));
join(file.path, '').should.equal(join(outpath, './copy/example.txt'));
});
outstream.on('end', function() {
fs.readFile(join(outpath, "copy", "example.txt"), function(err, contents) {
fs.readFile(join(outpath, 'copy', 'example.txt'), function(err, contents) {
should.exist(err);
should.not.exist(contents);
done();
Expand All @@ -69,7 +69,7 @@ describe('gulp output stream', function() {
});

it('should return a output stream that writes streaming files', function(done) {
var instream = gulp.src(join(__dirname, "./fixtures/**/*.txt"), {buffer:false});
var instream = gulp.src(join(__dirname, './fixtures/**/*.txt'), {buffer: false});
var outstream = instream.pipe(gulp.dest(outpath));

outstream.on('error', done);
Expand All @@ -78,13 +78,13 @@ describe('gulp output stream', function() {
should.exist(file);
should.exist(file.path);
should.exist(file.contents);
join(file.path,'').should.equal(join(outpath, "./copy/example.txt"));
join(file.path, '').should.equal(join(outpath, './copy/example.txt'));
});
outstream.on('end', function() {
fs.readFile(join(outpath, "copy", "example.txt"), function(err, contents) {
fs.readFile(join(outpath, 'copy', 'example.txt'), function(err, contents) {
should.not.exist(err);
should.exist(contents);
String(contents).should.equal("this is a test");
String(contents).should.equal('this is a test');
done();
});
});
Expand All @@ -107,19 +107,19 @@ describe('gulp output stream', function() {
});

function testWriteDir(srcOptions, done) {
var instream = gulp.src(join(__dirname, "./fixtures/stuff"), srcOptions);
var instream = gulp.src(join(__dirname, './fixtures/stuff'), srcOptions);
var outstream = instream.pipe(gulp.dest(outpath));

outstream.on('error', done);
outstream.on('data', function(file) {
// data should be re-emitted right
should.exist(file);
should.exist(file.path);
join(file.path,'').should.equal(join(outpath, "./stuff"));
join(file.path, '').should.equal(join(outpath, './stuff'));
});
outstream.on('end', function() {
fs.exists(join(outpath, "stuff"), function(exists) {
/* stinks that ok is an expression instead of a function call */
fs.exists(join(outpath, 'stuff'), function(exists) {
/* Stinks that ok is an expression instead of a function call */
/* jshint expr: true */
should(exists).be.ok;
/* jshint expr: false */
Expand Down
Loading

0 comments on commit a54996c

Please sign in to comment.