Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce support for jsonlines input files #499

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
bin
.playground
12 changes: 12 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current'
}
}
]
]
}
22 changes: 18 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"lint": "standard --verbose --fix",
"posttest": "ls -1 *.md | xargs -t -n 1 markdown-link-check",
"prepublishOnly": "npm run build",
"pretest": "npm run lint && npm run build",
"pretest": "rm -f ./bin/jfq.js && npm run lint && npm run build",
"test": "jest"
},
"repository": {
Expand Down Expand Up @@ -54,6 +54,7 @@
"js-yaml": "^4.0.0",
"json-colorizer": "^2.2.2",
"jsonata": "^1.8.4",
"line-reader": "^0.4.0",
"parse-json": "^5.2.0",
"read-input": "^0.3.1"
},
Expand Down
3 changes: 2 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default {
'json-colorizer',
'js-yaml',
'parse-json',
'read-input'
'read-input',
'line-reader'
]
}
9 changes: 9 additions & 0 deletions src/__tests__/getopts.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@ import getopts from '../getopts.js'
const fakeArgv = (...opts) => ([process.execPath, '/tmp/fakePath.js', ...opts])

describe('getting command line options', () => {
describe('when --yaml is used together with --jsonlines-input', () => {
it('should throw', () => {
expect(() => {
getopts(fakeArgv('--yaml', '--jsonlines-input', '--jsonlines-output'))
}).toThrow(Error)
})
})

describe('when there are no options', () => {
it('returns default values', async () => {
const res = await getopts(fakeArgv())
expect(res.query).toBe('$')
expect(res.files).toEqual([])
expect(res.ndjson).toBe(false)
expect(res.jsonlinesInput).toBe(false)
})
})

Expand Down
34 changes: 32 additions & 2 deletions src/getopts.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,49 @@ export default async argv => {
.option('-a, --accept-yaml', 'YAML input')
.option('-p, --plain-text', 'Do not decorate output')
.option('-q, --query-file <path>', 'JSONata query file')
.option('-l, --jsonlines-input', 'JSON Lines Input')
.option('-k, --jsonlines-output', 'JSON Lines Output')
.parse(argv)
.opts()

const ndjson = !!options.ndjson
const ndjsonOpt = !!options.ndjson
const jsonlinesOutputOpt = !!options.jsonlinesOutput

if (ndjsonOpt && jsonlinesOutputOpt) {
throw new Error('can only set --ndjson or --jsonlines-output')
}

const json = !!options.json
const yamlOut = !!options.yaml
const yamlIn = !!options.acceptYaml
const plainText = !!options.plainText
const queryFile = options.queryFile
// two aliases
const jsonlinesInput = !!options.jsonlinesInput
const jsonlinesOutput = ndjsonOpt || jsonlinesOutputOpt
const ndjson = jsonlinesOutput
const files = program.args.slice(0)

if (yamlIn && jsonlinesInput) {
throw new Error('--accept-yaml and --jsonlines can not be set together')
}

if (jsonlinesInput && !jsonlinesOutput) {
throw new Error('--jsonlines-input requires --jsonlines-output/ndjson as output')
}

const query = await getQuery(queryFile, files)
return { query, files, ndjson, json, yamlOut, yamlIn, plainText }
return {
query,
files,
ndjson,
json,
yamlOut,
yamlIn,
plainText,
jsonlinesInput,
jsonlinesOutput
}
}

const exists = async (path) => {
Expand Down
33 changes: 30 additions & 3 deletions src/jfq.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import colorize from 'json-colorizer'
import jsonata from 'jsonata'
import parseJson from 'parse-json'
import readInput from 'read-input'
import * as lineReader from 'line-reader'
import getopts from './getopts'
import YAML from 'js-yaml'

const main = async () => {
const { files, ndjson, json, yamlIn, yamlOut, query, plainText } = await getopts(process.argv)
const evaluator = parseQuery(query)
const fullFileMode = async (opts, evaluator) => {
const { files, ndjson, json, yamlIn, yamlOut, plainText } = opts

const data = await readInput(files)

data.files.forEach(file => {
Expand All @@ -22,6 +23,32 @@ const main = async () => {
})
}

const streamingMode = async (opts, evaluator) => {
const { files, ndjson, json, plainText } = opts
if (files && files.length > 1) {
console.error('streamingMode / jsonlines only works with one file/stream')
process.exit(1)
}
const input = (files && files[0]) || process.stdin
lineReader.eachLine(input, (line, last, continueCb) => {
const lineInput = parseJson(line)
const result = evaluator.evaluate(lineInput)
const output = formatJson(result, ndjson, json, plainText)
console.log(output)
continueCb()
})
}

const main = async () => {
const opts = await getopts(process.argv)
const evaluator = parseQuery(opts.query)
if (!opts.jsonlinesInput) {
await fullFileMode(opts, evaluator)
} else {
await streamingMode(opts, evaluator)
}
}

const parseQuery = query => {
try {
return jsonata(query)
Expand Down