2018-10-26 10:59:46 +00:00
|
|
|
#!/usr/bin/env node
|
2018-03-13 03:28:40 +00:00
|
|
|
'use strict'
|
2018-11-10 10:31:26 +00:00
|
|
|
const os = require('os')
|
2018-11-09 18:57:24 +00:00
|
|
|
const fs = require('fs')
|
2018-11-10 10:31:26 +00:00
|
|
|
const path = require('path')
|
2019-12-12 10:48:49 +00:00
|
|
|
const JSON = require('lossless-json')
|
2019-12-09 06:24:33 +00:00
|
|
|
const std = require('./std')
|
2019-04-03 17:16:04 +00:00
|
|
|
|
2018-11-10 10:31:26 +00:00
|
|
|
try {
|
2019-02-24 14:22:55 +00:00
|
|
|
require(path.join(os.homedir(), '.fxrc')) // Should be required before config.js usage.
|
2018-11-10 10:31:26 +00:00
|
|
|
} catch (err) {
|
|
|
|
if (err.code !== 'MODULE_NOT_FOUND') {
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
2019-02-24 14:22:55 +00:00
|
|
|
|
2018-11-09 18:57:24 +00:00
|
|
|
const print = require('./print')
|
|
|
|
const reduce = require('./reduce')
|
2019-02-25 19:06:12 +00:00
|
|
|
const stream = require('./stream')
|
2018-01-25 17:30:05 +00:00
|
|
|
|
2018-09-14 17:00:36 +00:00
|
|
|
const usage = `
|
2018-01-25 17:30:05 +00:00
|
|
|
Usage
|
2019-11-06 08:07:28 +00:00
|
|
|
$ fx [code ...]
|
2019-11-06 07:16:22 +00:00
|
|
|
|
2018-01-25 17:30:05 +00:00
|
|
|
Examples
|
2018-01-26 02:35:58 +00:00
|
|
|
$ echo '{"key": "value"}' | fx 'x => x.key'
|
2018-08-30 15:09:12 +00:00
|
|
|
value
|
2019-02-24 18:41:01 +00:00
|
|
|
|
2018-11-09 18:57:24 +00:00
|
|
|
$ echo '{"key": "value"}' | fx .key
|
|
|
|
value
|
2018-01-25 17:30:05 +00:00
|
|
|
|
2018-01-26 02:35:58 +00:00
|
|
|
$ echo '[1,2,3]' | fx 'this.map(x => x * 2)'
|
2018-01-25 17:30:05 +00:00
|
|
|
[2, 4, 6]
|
|
|
|
|
2018-01-26 02:35:58 +00:00
|
|
|
$ echo '{"items": ["one", "two"]}' | fx 'this.items' 'this[1]'
|
2018-08-30 15:09:12 +00:00
|
|
|
two
|
2018-01-25 17:30:05 +00:00
|
|
|
|
2018-01-26 02:35:58 +00:00
|
|
|
$ echo '{"count": 0}' | fx '{...this, count: 1}'
|
2018-01-25 17:30:05 +00:00
|
|
|
{"count": 1}
|
2019-02-24 18:41:01 +00:00
|
|
|
|
2018-08-30 15:09:12 +00:00
|
|
|
$ echo '{"foo": 1, "bar": 2}' | fx ?
|
|
|
|
["foo", "bar"]
|
2019-02-24 18:41:01 +00:00
|
|
|
|
2018-09-14 17:00:36 +00:00
|
|
|
`
|
2018-01-25 17:30:05 +00:00
|
|
|
|
2019-02-24 18:41:01 +00:00
|
|
|
const {stdin, stdout, stderr} = process
|
|
|
|
const args = process.argv.slice(2)
|
2019-02-24 14:22:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
void function main() {
|
|
|
|
stdin.setEncoding('utf8')
|
2019-02-24 18:41:01 +00:00
|
|
|
|
2019-02-24 14:22:55 +00:00
|
|
|
if (stdin.isTTY) {
|
2019-02-24 18:41:01 +00:00
|
|
|
handle('')
|
2019-02-24 14:22:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-25 19:06:12 +00:00
|
|
|
const reader = stream(stdin, apply)
|
2019-02-24 14:22:55 +00:00
|
|
|
|
2019-02-24 18:41:01 +00:00
|
|
|
stdin.on('readable', reader.read)
|
2019-02-24 14:22:55 +00:00
|
|
|
stdin.on('end', () => {
|
2019-02-24 18:58:09 +00:00
|
|
|
if (!reader.isStream()) {
|
|
|
|
handle(reader.value())
|
|
|
|
}
|
2019-02-24 14:22:55 +00:00
|
|
|
})
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
2019-02-24 18:41:01 +00:00
|
|
|
function handle(input) {
|
2018-11-09 18:57:24 +00:00
|
|
|
let filename = 'fx'
|
|
|
|
|
2018-09-14 17:00:36 +00:00
|
|
|
if (input === '') {
|
2019-01-30 03:33:13 +00:00
|
|
|
if (args.length === 0 || (args.length === 1 && (args[0] === '-h' || args[0] === '--help'))) {
|
2018-11-09 18:57:24 +00:00
|
|
|
stderr.write(usage)
|
|
|
|
process.exit(2)
|
|
|
|
}
|
2019-02-09 13:12:42 +00:00
|
|
|
if (args.length === 1 && (args[0] === '-v' || args[0] === '--version')) {
|
|
|
|
stderr.write(require('./package.json').version + '\n')
|
|
|
|
process.exit(2)
|
|
|
|
}
|
2019-02-09 16:48:13 +00:00
|
|
|
if (args.length === 1 && args[0] === '--life') {
|
2019-02-24 14:22:55 +00:00
|
|
|
require('./bang')
|
|
|
|
return
|
2019-02-09 16:48:13 +00:00
|
|
|
}
|
2019-01-30 03:33:13 +00:00
|
|
|
|
2019-12-12 10:48:49 +00:00
|
|
|
input = fs.readFileSync(args[0]).toString('utf8')
|
2018-11-09 18:57:24 +00:00
|
|
|
filename = path.basename(args[0])
|
2019-12-09 06:24:33 +00:00
|
|
|
global.FX_FILENAME = filename
|
2019-02-24 18:41:01 +00:00
|
|
|
args.shift()
|
2018-01-25 17:30:05 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 17:00:36 +00:00
|
|
|
const json = JSON.parse(input)
|
2018-11-02 18:24:56 +00:00
|
|
|
|
|
|
|
if (args.length === 0 && stdout.isTTY) {
|
2018-11-09 18:57:24 +00:00
|
|
|
require('./fx')(filename, json)
|
2018-11-02 18:24:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-24 18:41:01 +00:00
|
|
|
apply(json)
|
2019-02-24 14:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-24 18:41:01 +00:00
|
|
|
function apply(json) {
|
2019-02-24 14:22:55 +00:00
|
|
|
let output
|
|
|
|
|
|
|
|
try {
|
|
|
|
output = args.reduce(reduce, json)
|
|
|
|
} catch (e) {
|
2019-12-09 06:24:33 +00:00
|
|
|
if (e !== std.skip) {
|
2019-02-24 14:22:55 +00:00
|
|
|
throw e
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2018-01-25 17:30:05 +00:00
|
|
|
|
2018-11-09 18:57:24 +00:00
|
|
|
if (typeof output === 'undefined') {
|
2018-11-02 18:24:56 +00:00
|
|
|
stderr.write('undefined\n')
|
2018-11-09 18:57:24 +00:00
|
|
|
} else if (typeof output === 'string') {
|
|
|
|
console.log(output)
|
2018-01-25 17:30:05 +00:00
|
|
|
} else {
|
2018-11-09 18:57:24 +00:00
|
|
|
const [text] = print(output)
|
|
|
|
console.log(text)
|
2018-01-25 17:30:05 +00:00
|
|
|
}
|
|
|
|
}
|