Skip to content

Commit

Permalink
Accepts piped input from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjoelkemp authored and markelog committed Jul 29, 2014
1 parent fb984e3 commit 94870e3
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 18 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ To run `jscs`, you can use the following command from the project root:
jscs path[ path[...]]
```

You can also pipe input into jscs:

```
cat myfile.js | jscs
```

## CLI

### `--config`
Expand Down
19 changes: 18 additions & 1 deletion bin/jscs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env node
var cli = require('../lib/cli');
var stdin = process.stdin;
var _stdInput = [];

/**
* Command line implementation for JSCS.
Expand All @@ -26,4 +28,19 @@ program
.option('', '\t /path/to/my-reporter\t\t(absolute path without extension)')
.parse(process.argv);

cli(program);
if (process.argv.length === 2) {
// Handle usage like cat myfile.js | jscs
stdin.setEncoding('utf8');

stdin.on('data', function(chunk) {
_stdInput.push(chunk);
});

stdin.on('end', function() {
program._stdin = _stdInput.join();
cli(program);
});

} else {
cli(program);
}
47 changes: 30 additions & 17 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ module.exports = function(program) {
return returnArgs;
}

if (args.length === 0) {
if (args.length === 0 && !program._stdin) {
console.error('No input files specified. Try option --help for usage information.');
defer.reject(1);

Expand Down Expand Up @@ -105,25 +105,38 @@ module.exports = function(program) {
checker.registerDefaultRules();
checker.configure(config);

/**
* Processing specified files and dirs.
*/
Vow.all(args.map(checker.checkPath, checker)).then(function(results) {
var errorsCollection = [].concat.apply([], results);

reporter(errorsCollection);
if (typeof program._stdin !== 'undefined') {
var errors = checker.checkString(program._stdin);
reporter([errors]);

errorsCollection.forEach(function(errors) {
if (!errors.isEmpty()) {
defer.reject(2);
}
});
if (!errors.isEmpty()) {
defer.reject(2);
}

defer.resolve(0);
}).fail(function(e) {
console.error(e.stack);
defer.reject(1);
});
}

if (args.length) {
/**
* Processing specified files and dirs.
*/
Vow.all(args.map(checker.checkPath, checker)).then(function(results) {
var errorsCollection = [].concat.apply([], results);

reporter(errorsCollection);

errorsCollection.forEach(function(errors) {
if (!errors.isEmpty()) {
defer.reject(2);
}
});

defer.resolve(0);
}).fail(function(e) {
console.error(e.stack);
defer.reject(1);
});
}

return returnArgs;
};
42 changes: 42 additions & 0 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ var hasAnsi = require('has-ansi');

var path = require('path');

var exec = require('child_process').exec;

var cli = require('../lib/cli');
var startingDir = process.cwd();

Expand Down Expand Up @@ -162,6 +164,46 @@ describe('modules/cli', function() {
});
});

describe('input via stdin (#448)', function() {
var bin = path.resolve(__dirname, '../bin/jscs');

it('should accept cat\'d file input via stdin', function (done) {
rAfter();

var testFile = __dirname + '/data/cli/stdin.js';
var cmd = 'cat ' + testFile + ' | ' + bin;

exec(cmd, function (error, stdout) {
assert(!error);
done();
});
});

it('should accept echo\'d input via stdin', function (done) {
rAfter();

var cmd = 'echo "var x = [1, 2];" | ' + bin;

exec(cmd, function (error, stdout) {
assert(!error);
done();
});
});

it('should still bail for empty input being piped', function(done) {
// 'cat myEmptyFile.js | jscs' should report a successful run
rAfter();

var testFile = __dirname + '/data/cli/success.js';
var cmd = 'cat ' + testFile + ' | ' + bin;

exec(cmd, function (error) {
assert(error.code);
done();
});
});
});

describe('reporter option', function() {
it('should set implicitly set checkstyle reporter', function() {
var result = cli({
Expand Down
1 change: 1 addition & 0 deletions test/data/cli/stdin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var x = {a: 'a'};

0 comments on commit 94870e3

Please sign in to comment.