Ai
1 Star 0 Fork 0

子安/fx

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
index.js 1.72 KB
一键复制 编辑 原始数据 按行查看 历史
Anton Medvedev 提交于 2018-12-01 13:17 +08:00 . Improve DX
#!/usr/bin/env node
'use strict'
const os = require('os')
const fs = require('fs')
const path = require('path')
const {stdin, stdout, stderr} = process
try {
require(path.join(os.homedir(), '.fxrc'))
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') {
throw err
}
}
const print = require('./print')
const reduce = require('./reduce')
const usage = `
Usage
$ fx [code ...]
Examples
$ echo '{"key": "value"}' | fx 'x => x.key'
value
$ echo '{"key": "value"}' | fx .key
value
$ echo '[1,2,3]' | fx 'this.map(x => x * 2)'
[2, 4, 6]
$ echo '{"items": ["one", "two"]}' | fx 'this.items' 'this[1]'
two
$ echo '{"count": 0}' | fx '{...this, count: 1}'
{"count": 1}
$ echo '{"foo": 1, "bar": 2}' | fx ?
["foo", "bar"]
`
function main(input) {
let args = process.argv.slice(2)
let filename = 'fx'
if (input === '') {
if (args.length === 0) {
stderr.write(usage)
process.exit(2)
}
input = fs.readFileSync(args[0])
filename = path.basename(args[0])
args = args.slice(1)
}
const json = JSON.parse(input)
if (args.length === 0 && stdout.isTTY) {
require('./fx')(filename, json)
return
}
const output = args.reduce(reduce, json)
if (typeof output === 'undefined') {
stderr.write('undefined\n')
} else if (typeof output === 'string') {
console.log(output)
} else {
const [text] = print(output)
console.log(text)
}
}
function run() {
stdin.setEncoding('utf8')
if (stdin.isTTY) {
main('')
return
}
let buff = ''
stdin.on('readable', () => {
let chunk
while ((chunk = stdin.read())) {
buff += chunk
}
})
stdin.on('end', () => {
main(buff)
})
}
run()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/andrewgithub/fx.git
git@gitee.com:andrewgithub/fx.git
andrewgithub
fx
fx
master

搜索帮助