Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for sequences of commands #25

Merged
merged 2 commits into from
Jul 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
"postinstall": "echo \"lint-staged installed! Do not forget to configure it. See https://github.com/okonet/lint-staged/blob/master/README.md\" && exit 0",
"lint": "eslint .",
"lint:fix": "npm run lint -- --fix",
"eslint": "eslint --quiet",
"eslint": "eslint --quiet --fix",
"git:add": "git add",
"pre-commit": "node index.js",
"release": "npmpub",
"test": "mocha --compilers js:babel-core/register ./test/*.spec.js"
},
"lint-staged": {
"eslint": "*.js"
"*.js": ["eslint", "git:add"]
},
"pre-commit": [
"pre-commit"
Expand Down Expand Up @@ -46,6 +47,7 @@
"homepage": "https://github.com/okonet/lint-staged#readme",
"dependencies": {
"app-root-path": "^1.2.1",
"batchflow": "^0.4.0",
"minimatch": "^3.0.0",
"npm-which": "^2.0.0",
"object-assign": "^4.1.0",
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ sgf('ACM', function(err, results) {
return file.filename;
});
if (filePaths.length) {
Object.keys(linters).forEach(function(linter) {
var extensions = linters[linter];
var fileList = filePaths.filter(minimatch.filter(extensions, { matchBase: true }));
Object.keys(linters).forEach(function(key) {
var linter = linters[key];
var fileList = filePaths.filter(minimatch.filter(key, { matchBase: true }));
if (fileList.length) {
spinner.text = 'Running ' + linter + '...';
runScript(linter, fileList, config, function(error, exitCode) {
Expand Down
48 changes: 31 additions & 17 deletions src/runScript.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
var findBin = require('./findBin');
var spawn = require('child_process').spawn;
var batch = require('batchflow');

module.exports = function runScript(linter, paths, config, cb) {
findBin(linter, paths, config, function(err, binPath, args) {
if (err) {
module.exports = function runScript(linters, paths, config, cb) {
var lintersArray = Array.isArray(linters) ? linters : [linters];
batch(lintersArray)
.sequential()
.each(function(i, linter, next) {
findBin(linter, paths, config, function(err, binPath, args) {
if (err) {
throw new Error(err);
}
var npmStream = spawn(binPath, args, {
stdio: 'inherit' // <== IMPORTANT: use this option to inherit the parent's environment
});
npmStream.on('error', function(error) {
// process.exitCode = 1;
throw new Error(error);
});
npmStream.on('exit', function(code) {
process.exitCode = code;
});
npmStream.on('close', function(code) {
next(code);
});
});
})
.error(function(err) {
console.error(err);
}
var npmStream = spawn(binPath, args, {
stdio: 'inherit' // <== IMPORTANT: use this option to inherit the parent's environment
});
npmStream.on('error', function(error) {
process.exitCode = 1;
cb.call(this, error, null);
});
npmStream.on('close', function(code) {
cb.call(this, err, null);
})
.end(function(codes) {
var exitCode = codes.length ? Math.max.apply(this, codes) : 0;
if (typeof cb === 'function') {
cb.call(this, null, code);
cb.call(this, null, exitCode);
}
});
npmStream.on('exit', function(code) {
process.exitCode = code;
});
});
};
51 changes: 28 additions & 23 deletions test/runScript.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,56 @@ import mockSpawn from 'mock-spawn';
import rewire from 'rewire';
const runScript = rewire('../src/runScript');

var mySpawn = mockSpawn();
require('child_process').spawn = mySpawn;
mySpawn.setDefault(mySpawn.simple(1 /* exit code */, 'hello world' /* stdout */));

const packageJSON = {
scripts: {
test: 'noop'
test: 'noop',
test2: 'noop'
},
'lint-staged': {}
};

describe('runScript', () => {
it.skip('should run the callback with the proper exit code', done => {
it('should run the callback with the proper exit code', done => {
const spy = expect.createSpy();
const findBinSpy = expect.createSpy(runScript.__get__('findBin')).andCallThrough();
runScript.__set__('findBin', findBinSpy);
const mySpawn = mockSpawn();
mySpawn.setDefault(mySpawn.simple(0));
runScript.__set__('spawn', mySpawn);

runScript('test', 'test.js', packageJSON, spy);
setTimeout(() => {
expect(findBinSpy.calls.length).toEqual(1);
expect(mySpawn.calls.length).toEqual(1);
expect(mySpawn.calls[0].exitCode).toEqual(0);
expect(mySpawn.calls[0].command).toEqual('npm');
expect(mySpawn.calls[0].args).toEqual(['run', '-s', 'test', '--', 'test.js']);

expect(spy.calls.length).toEqual(1);
expect(findBinSpy.calls[0].arguments[0]).toEqual('test');
expect(findBinSpy.calls[0].arguments[1]).toEqual('test.js');
expect(spy).toHaveBeenCalledWith(null, 1);
expect(spy).toHaveBeenCalledWith(null, 0);
done();
}, 0);
}, 10);
});

it.skip('should support array of scripts as a first argument', done => {
it('should support array of scripts as a first argument', done => {
const spy = expect.createSpy();
const findBinSpy = expect.createSpy(runScript.__get__('findBin')).andCallThrough();
runScript.__set__('findBin', findBinSpy);
const mySpawn = mockSpawn();
mySpawn.sequence.add(mySpawn.simple(0));
mySpawn.sequence.add(mySpawn.simple(1));
runScript.__set__('spawn', mySpawn);

runScript(['test', 'test2'], 'test.js', packageJSON, spy);
setTimeout(() => {
expect(findBinSpy.calls.length).toEqual(2);

expect(findBinSpy.calls[0].arguments[0]).toEqual('test');
expect(findBinSpy.calls[0].arguments[1]).toEqual('test.js');
expect(mySpawn.calls.length).toEqual(2);
expect(mySpawn.calls[0].exitCode).toEqual(0);
expect(mySpawn.calls[0].command).toEqual('npm');
expect(mySpawn.calls[0].args).toEqual(['run', '-s', 'test', '--', 'test.js']);

expect(findBinSpy.calls[1].arguments[0]).toEqual('test2');
expect(findBinSpy.calls[1].arguments[1]).toEqual('test.js');
expect(mySpawn.calls[1].exitCode).toEqual(1);
expect(mySpawn.calls[1].command).toEqual('npm');
expect(mySpawn.calls[1].args).toEqual(['run', '-s', 'test2', '--', 'test.js']);

expect(spy.calls.length).toEqual(1);
expect(spy).toHaveBeenCalledWith(null, 1);
done();
}, 0);
}, 10);
});
});

Expand Down