diff --git a/TODO.md b/TODO.md index 10234be..76785b4 100644 --- a/TODO.md +++ b/TODO.md @@ -1,4 +1,2 @@ -- make CLI - prevent multiple calls to get() from changing state - update documentation -- add tonic to documentation diff --git a/gulpfile.js b/gulpfile.js index 5d650f9..a195d3d 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -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' diff --git a/package.json b/package.json index 6984672..a261635 100644 --- a/package.json +++ b/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": { diff --git a/src/cli.js b/src/cli.js index 5007175..9babfa6 100755 --- a/src/cli.js +++ b/src/cli.js @@ -22,10 +22,11 @@ process.on('uncaughtException', function (e) { program .version(`${packageInfo.name} version ${packageInfo.version}`, '-v, --version') - .usage('[options] ') + .usage('[options] ') .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 @@ -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(); +} diff --git a/test/cli.js b/test/cli.js index 40a055f..fb83dfd 100644 --- a/test/cli.js +++ b/test/cli.js @@ -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) { @@ -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'); + }); });