From 35280be40f296c6570f6051601ce0888ef3d2da7 Mon Sep 17 00:00:00 2001 From: Anton Medvedev Date: Sat, 15 Sep 2018 00:00:36 +0700 Subject: [PATCH] Improve startup time --- README.md | 4 +--- index.js | 40 ++++++++++++++++++++++++++++------------ package.json | 4 +--- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 4f2081c..a1d0a00 100644 --- a/README.md +++ b/README.md @@ -134,9 +134,7 @@ Convert object to array: ``` $ cat package.json | fx 'Object.keys(this.dependencies)' [ - "@medv/prettyjson", - "get-stdin", - "meow" + "@medv/prettyjson" ] ``` diff --git a/index.js b/index.js index d8f0843..9b2af45 100755 --- a/index.js +++ b/index.js @@ -1,10 +1,8 @@ #!/usr/bin/env node --max-old-space-size=8192 'use strict' -const meow = require('meow') -const stdin = require('get-stdin') const pretty = require('@medv/prettyjson') -const cli = meow(` +const usage = ` Usage $ fx [code ...] @@ -26,17 +24,16 @@ const cli = meow(` $ echo '{"key": "value"}' | fx .key value -`) +` -async function main() { - const text = await stdin() - - if (text === '') { - cli.showHelp() +function main(input) { + if (input === '') { + console.log(usage) + process.exit(2) } - const json = JSON.parse(text) - const result = cli.input.reduce(reduce, json) + const json = JSON.parse(input) + const result = process.argv.slice(2).reduce(reduce, json) if (typeof result === 'undefined') { process.stderr.write('undefined\n') @@ -80,4 +77,23 @@ function reduce(json, code) { return fx.call(json) } -main() +const stdin = process.stdin +let buff = '' + +if (stdin.isTTY) { + main(buff) +} + +stdin.setEncoding('utf8') + +stdin.on('readable', () => { + let chunk + + while ((chunk = stdin.read())) { + buff += chunk + } +}) + +stdin.on('end', () => { + main(buff) +}) diff --git a/package.json b/package.json index 703eee1..cdc61c2 100644 --- a/package.json +++ b/package.json @@ -23,9 +23,7 @@ "node": ">=8" }, "dependencies": { - "@medv/prettyjson": "^1.0.0", - "get-stdin": "^6.0.0", - "meow": "^5.0.0" + "@medv/prettyjson": "^1.0.0" }, "devDependencies": { "ava": "^0.25.0",