Skip to content

Commit

Permalink
Improve startup time
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Sep 14, 2018
1 parent df12c3a commit 35280be
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,7 @@ Convert object to array:
```
$ cat package.json | fx 'Object.keys(this.dependencies)'
[
"@medv/prettyjson",
"get-stdin",
"meow"
"@medv/prettyjson"
]
```

Expand Down
40 changes: 28 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -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 ...]
Expand All @@ -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')
Expand Down Expand Up @@ -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)
})
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 35280be

Please sign in to comment.