forked from Archives/fx
first commit
commit
776645e6c5
@ -0,0 +1,2 @@
|
||||
/node_modules/
|
||||
package-lock.json
|
@ -0,0 +1,3 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "7"
|
@ -0,0 +1,68 @@
|
||||
#!/usr/bin/env node
|
||||
'use strict';
|
||||
const meow = require('meow')
|
||||
const stdin = require('get-stdin')
|
||||
const cardinal = require('cardinal')
|
||||
const theme = require('cardinal/themes/tomorrow-night')
|
||||
|
||||
const cli = meow(`
|
||||
Usage
|
||||
$ fx [code ...]
|
||||
|
||||
Examples
|
||||
$ echo '{"key": "value"}' | fx "x => x.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}
|
||||
`)
|
||||
|
||||
|
||||
|
||||
const highlight = process.stdout.isTTY ? cardinal.highlight : x => x
|
||||
|
||||
async function main() {
|
||||
const text = await stdin()
|
||||
|
||||
if (text === '') {
|
||||
cli.showHelp()
|
||||
}
|
||||
|
||||
const json = JSON.parse(text)
|
||||
const result = cli.input.reduce(reduce, json)
|
||||
|
||||
if (typeof result === 'undefined') {
|
||||
console.log(undefined)
|
||||
} else {
|
||||
const text = JSON.stringify(result, null, 4)
|
||||
console.log(highlight(text, {theme}))
|
||||
}
|
||||
}
|
||||
|
||||
function reduce(json, code) {
|
||||
if (/^\w+\s*=>/.test(code)) {
|
||||
const fx = eval(code)
|
||||
return fx(json)
|
||||
} else if (/yield/.test(code)) {
|
||||
const fx = eval(`
|
||||
function fn() {
|
||||
const gen = (function*(){
|
||||
${code.replace(/\\\n/g, '')}
|
||||
}).call(this)
|
||||
return [...gen]
|
||||
}; fn
|
||||
`)
|
||||
return fx.call(json)
|
||||
} else {
|
||||
const fx = eval(`function fn() { return ${code} }; fn`)
|
||||
return fx.call(json)
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "fx",
|
||||
"version": "0.0.0",
|
||||
"description": "Command-line JSON processing tool",
|
||||
"repository": "antonmedv/fx",
|
||||
"author": "Anton Medvedev <anton@medv.io>",
|
||||
"license": "MIT",
|
||||
"bin": {
|
||||
"fx": "index.js"
|
||||
},
|
||||
"files": [
|
||||
"index.js"
|
||||
],
|
||||
"scripts": {
|
||||
"test": "bash test.sh"
|
||||
},
|
||||
"keywords": [
|
||||
"json",
|
||||
"cli"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=9"
|
||||
},
|
||||
"dependencies": {
|
||||
"cardinal": "^1.0.0",
|
||||
"get-stdin": "^5.0.1",
|
||||
"meow": "^4.0.0"
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euxo pipefail
|
||||
alias fx='node index.js'
|
||||
echo '[{"greeting": "hello world"}]' | fx
|
||||
echo '{"key": "value"}' | fx "x => x.key"
|
||||
echo '[1, 2, 3, 4, 5]' | fx "this.map(x => x * this.length)"
|
||||
echo '[1, 2, 3, 4, 5]' | fx "for (let i of this) if (i % 2 == 0) yield i"
|
||||
echo '{"items": ["foo", "bar"]}' | fx "this.items" "yield* this; yield 'baz'"
|
Loading…
Reference in New Issue