Skip to content

Instantly share code, notes, and snippets.

@AndersDJohnson
Last active June 5, 2019 13:33
Show Gist options
  • Save AndersDJohnson/4123255 to your computer and use it in GitHub Desktop.
Save AndersDJohnson/4123255 to your computer and use it in GitHub Desktop.
JSONSelect CLI
#!/usr/bin/env coffee
argv = require('optimist')
.alias('s', 'selector')
.default('s', ':root')
.argv
JSONSelect = require('JSONSelect')
# eyes = require('eyes')
# inspect = -> eyes.inspect.apply(eyes, arguments)
run = (text) ->
object = JSON.parse(text)
# inspect(object)
selector = argv.s
match = JSONSelect.match(selector, object)
# inspect match
output = JSON.stringify match
process.stdout.write(output)
process.stdout.write('\n')
stdin = ->
buf = ''
process.stdin.setEncoding('utf8')
process.stdin.on 'data', (chunk) ->
# process.stdout.write('data: ' + chunk)
buf += chunk
process.stdin.on 'end', ->
run(buf)
process.stdin.pause()
# console.log 'buf', buf
# process.stdout.write('end\n');
process.stdin.resume()
stdin()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment