Skip to content

Commit

Permalink
adding --pipe to cli. bumping version.
Browse files Browse the repository at this point in the history
  • Loading branch information
skratchdot committed Nov 21, 2015
1 parent e12c7c5 commit cdb4cde
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 8 deletions.
2 changes: 0 additions & 2 deletions TODO.md
@@ -1,4 +1,2 @@
- make CLI
- prevent multiple calls to get() from changing state
- update documentation
- add tonic to documentation
2 changes: 1 addition & 1 deletion gulpfile.js
Expand Up @@ -94,7 +94,7 @@ gulp.task('test', function () {
// Force `require` to return covered files
.pipe(istanbul.hookRequire())
.on('finish', function () {
gulp.src(files.test, {read: false})
return gulp.src(files.test, {read: false})
// gulp-mocha needs filepaths so you can't have any plugins before it
.pipe(mocha({
reporter: 'spec'
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "stats-collector",
"version": "0.0.5",
"version": "0.0.6",
"description": "collect stats about numbers",
"main": "lib/index.js",
"bin": {
Expand Down
22 changes: 20 additions & 2 deletions src/cli.js
Expand Up @@ -22,10 +22,11 @@ process.on('uncaughtException', function (e) {

program
.version(`${packageInfo.name} version ${packageInfo.version}`, '-v, --version')
.usage('[options] <values ...>')
.usage('[options] <values>')
.option('-c, --collectors [collectors]', 'add collectors', convertToList, [])
.option('-f, --filters [filters]', 'add filters', convertToList, [])
.option('-t, --type [type]', 'type of stats [empty,basic,stats,advanced]', 'stats')
.option('-p, --pipe', 'whether or not to accept piped data from stdin')
.parse(process.argv);

// setup defaults and validate
Expand Down Expand Up @@ -57,4 +58,21 @@ values = program.args.join(' ').replace(/,/gi, ' ').split(' ')
.filter(Number.isFinite);
collector.update(values);

console.log(JSON.stringify(collector.get(true), null, ' '));
const onData = function (data) {
collector.update(convertToList(data.toString()));
};

const onFinish = function () {
console.log(JSON.stringify(collector.get(true), null, ' '));
process.exit(0);
};

if (program.pipe) {
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', onData);
process.stdin.on('end', onFinish);
process.stdin.on('exit', onFinish);
} else {
onFinish();
}
25 changes: 23 additions & 2 deletions test/cli.js
Expand Up @@ -6,8 +6,11 @@ const expect = require('chai').expect;
const packageInfo = require('../package.json');
const testHelper = function (commands, expected, fn) {
const args = commands.split(' ');
args.unshift(`${__dirname}/../lib/cli.js`);
const result = childProcess.spawnSync('node', args, {encoding: 'utf-8'});
const result = childProcess.spawnSync(
`${__dirname}/../scripts/cli.js`,
args,
{ encoding: 'utf-8' }
);
fn(result.stderr.toString(), result.stdout.toString());
};
const test = function (commands, expected) {
Expand Down Expand Up @@ -46,4 +49,22 @@ describe('command line tool', function () {
test('-t empty -c count 0,1,2,3,4,5', '"count": 6');
test('-t empty -c min,max 0,5,2,1,4,3', '"max": 5');
});
it('should work with --pipe', function () {
expect(childProcess.execSync(
`echo "1 2 3 4 5" | ${__dirname}/../scripts/cli.js --pipe`,
{ encoding: 'utf-8' }
)).to.contain('"count": 5');
expect(childProcess.execSync(
`echo "1 2 3 4 5" | ${__dirname}/../scripts/cli.js --pipe 4,5,6`,
{ encoding: 'utf-8' }
)).to.contain('"count": 8');
expect(childProcess.execSync(
`echo "1 2 3 4 5" | ${__dirname}/../scripts/cli.js`,
{ encoding: 'utf-8' }
)).to.contain('"count": 0');
expect(childProcess.execSync(
`echo "1 2 3 4 5" | ${__dirname}/../scripts/cli.js 4,5,6`,
{ encoding: 'utf-8' }
)).to.contain('"count": 3');
});
});

0 comments on commit cdb4cde

Please sign in to comment.