├── .gitignore ├── src ├── __tests__ │ ├── fixtures │ │ ├── query.jsonata │ │ ├── bad.yaml │ │ ├── bad.json │ │ ├── c.yaml │ │ ├── a.json │ │ └── b.json │ ├── jfq_query.js │ ├── getopts.js │ ├── jfq_inputs.js │ └── jfq_outputs.js ├── test-helper.js ├── getopts.js └── jfq.js ├── .npmignore ├── .babelrc ├── .github ├── dependabot.yml └── workflows │ ├── test.yml │ └── publish.yml ├── rollup.config.js ├── LICENSE ├── package.json └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | bin 3 | -------------------------------------------------------------------------------- /src/__tests__/fixtures/query.jsonata: -------------------------------------------------------------------------------- 1 | name 2 | -------------------------------------------------------------------------------- /src/__tests__/fixtures/bad.yaml: -------------------------------------------------------------------------------- 1 | { 2 | foo: 42 3 | -------------------------------------------------------------------------------- /src/__tests__/fixtures/bad.json: -------------------------------------------------------------------------------- 1 | { 2 | foo: 42 3 | } 4 | -------------------------------------------------------------------------------- /src/__tests__/fixtures/c.yaml: -------------------------------------------------------------------------------- 1 | foo: bar 2 | baz: 3 | - alpha 4 | - beta 5 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | * 2 | !README.md 3 | !package.json 4 | !LICENSE.md 5 | !bin/jfq.js 6 | -------------------------------------------------------------------------------- /src/__tests__/fixtures/a.json: -------------------------------------------------------------------------------- 1 | { 2 | "foo": "bar", 3 | "listy": [ 4 | "alpha", 5 | "beta" 6 | ] 7 | } 8 | -------------------------------------------------------------------------------- /src/__tests__/fixtures/b.json: -------------------------------------------------------------------------------- 1 | { 2 | "foo": "baz", 3 | "listy": [ 4 | "gamma", 5 | "delta" 6 | ] 7 | } 8 | -------------------------------------------------------------------------------- /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | ["@babel/preset-env", { 4 | "targets": { 5 | "node": "6" 6 | } 7 | }] 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: npm 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | open-pull-requests-limit: 10 8 | versioning-strategy: increase 9 | - package-ecosystem: github-actions 10 | directory: "/" 11 | schedule: 12 | interval: daily 13 | open-pull-requests-limit: 10 14 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: test 2 | on: [push, pull_request] 3 | jobs: 4 | test: 5 | runs-on: ubuntu-latest 6 | strategy: 7 | matrix: 8 | node: [ '14', '16', '18' ] 9 | name: Node ${{ matrix.node }} 10 | steps: 11 | - uses: actions/checkout@v4 12 | - name: Setup node 13 | uses: actions/setup-node@v4 14 | with: 15 | node-version: ${{ matrix.node }} 16 | - run: npm install 17 | - run: npm test 18 | -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | import babel from 'rollup-plugin-babel' 2 | 3 | export default { 4 | input: 'src/jfq.js', 5 | output: { 6 | file: 'bin/jfq.js', 7 | format: 'cjs', 8 | banner: '#!/usr/bin/env node' 9 | }, 10 | plugins: [ 11 | babel({ 12 | exclude: 'node_modules/**' 13 | }) 14 | ], 15 | external: [ 16 | 'commander', 17 | 'file-exists', 18 | 'fs-readfile-promise', 19 | 'jsonata', 20 | 'json-colorizer', 21 | 'js-yaml', 22 | 'parse-json', 23 | 'read-input' 24 | ] 25 | } 26 | -------------------------------------------------------------------------------- /src/test-helper.js: -------------------------------------------------------------------------------- 1 | import childProcess from 'child_process' 2 | 3 | export const run = (...parms) => exec(command(parms)) 4 | 5 | export const runStdin = (stdin, ...parms) => exec(`echo '${stdin}' | ` + command(parms)) 6 | 7 | const command = params => './bin/jfq.js -p ' + params.join(' ') 8 | 9 | const exec = command => { 10 | return new Promise(resolve => { 11 | childProcess.exec(command, (error, stdout, stderr) => { 12 | resolve({ 13 | error, 14 | stdout: stdout.trim(), 15 | stderr: stderr.trim() 16 | }) 17 | }) 18 | }) 19 | } 20 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | name: publish 2 | on: 3 | push: 4 | branches: 5 | - main 6 | jobs: 7 | publish: 8 | runs-on: ubuntu-latest 9 | steps: 10 | - uses: actions/checkout@v4 11 | - uses: actions/setup-node@v4 12 | with: 13 | node-version: 16 14 | - run: npm install 15 | - run: npm test 16 | - id: publish 17 | uses: JS-DevTools/npm-publish@v3 18 | with: 19 | token: ${{ secrets.NPM_TOKEN }} 20 | - if: steps.publish.outputs.type != 'none' 21 | run: | 22 | echo "Version changed: ${{ steps.publish.outputs.old-version }} => ${{ steps.publish.outputs.version }}" 23 | -------------------------------------------------------------------------------- /src/__tests__/jfq_query.js: -------------------------------------------------------------------------------- 1 | /* eslint-env jest */ 2 | 3 | import { run } from '../test-helper' 4 | 5 | describe('queries', () => { 6 | describe('valid', () => { 7 | describe('single property', () => { 8 | it('gets the corresponding value', async () => { 9 | const result = await run('name', 'package.json') 10 | expect(result.error).toBeNull() 11 | expect(result.stderr).toBe('') 12 | expect(result.stdout).toEqual('jfq') 13 | }) 14 | }) 15 | }) 16 | 17 | describe('invalid', () => { 18 | it('returns the error from JSONata', async () => { 19 | const result = await run('na!me', 'package.json') 20 | expect(result.error.message).toContain('Failed to compile JSONata expression: Unknown operator: "!"') 21 | }) 22 | }) 23 | }) 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 George Blue 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/getopts.js: -------------------------------------------------------------------------------- 1 | import fileExists from 'file-exists' 2 | import program from 'commander' 3 | import readFilePromise from 'fs-readfile-promise' 4 | 5 | export default async argv => { 6 | const options = program 7 | .option('-n, --ndjson', 'Newline Delimited JSON') 8 | .option('-j, --json', 'Force JSON output') 9 | .option('-y, --yaml', 'YAML output') 10 | .option('-a, --accept-yaml', 'YAML input') 11 | .option('-p, --plain-text', 'Do not decorate output') 12 | .option('-q, --query-file ', 'JSONata query file') 13 | .parse(argv) 14 | .opts() 15 | 16 | const ndjson = !!options.ndjson 17 | const json = !!options.json 18 | const yamlOut = !!options.yaml 19 | const yamlIn = !!options.acceptYaml 20 | const plainText = !!options.plainText 21 | const queryFile = options.queryFile 22 | const files = program.args.slice(0) 23 | 24 | const query = await getQuery(queryFile, files) 25 | return { query, files, ndjson, json, yamlOut, yamlIn, plainText } 26 | } 27 | 28 | const exists = async (path) => { 29 | try { 30 | return await fileExists(path) 31 | } catch (err) { 32 | return false 33 | } 34 | } 35 | 36 | const getQuery = async (queryFile, files) => { 37 | if (typeof queryFile === 'string' && queryFile.length) { 38 | return readFilePromise(queryFile, 'utf8') 39 | } else { 40 | // If the first argument is a file that exists, then it was not a query string 41 | // If the first argument is not defined, then it was not a query string 42 | const isNotQuery = files[0] ? await exists(files[0]) : true 43 | return (isNotQuery ? '$' : files.shift()) 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jfq", 3 | "version": "1.2.11", 4 | "description": "JSONata on the command line", 5 | "main": "index.js", 6 | "engines": { 7 | "node": ">=6" 8 | }, 9 | "scripts": { 10 | "build": "rollup -c && chmod a+x ./bin/jfq.js", 11 | "lint": "standard --verbose --fix", 12 | "posttest": "ls -1 *.md | xargs -t -n 1 markdown-link-check", 13 | "prepublishOnly": "npm run build", 14 | "pretest": "npm run lint && npm run build", 15 | "test": "jest" 16 | }, 17 | "repository": { 18 | "type": "git", 19 | "url": "git+https://github.com/blgm/jfq.git" 20 | }, 21 | "author": "George Blue", 22 | "license": "MIT", 23 | "bugs": { 24 | "url": "https://github.com/blgm/jfq/issues" 25 | }, 26 | "keywords": [ 27 | "jsonata", 28 | "jq", 29 | "cli", 30 | "command", 31 | "json", 32 | "yaml", 33 | "pipe", 34 | "file" 35 | ], 36 | "homepage": "https://github.com/blgm/jfq#readme", 37 | "devDependencies": { 38 | "@babel/core": "7.24.7", 39 | "@babel/preset-env": "7.24.3", 40 | "babel-core": "7.0.0-bridge.0", 41 | "babel-jest": "29.7.0", 42 | "jest": "29.7.0", 43 | "markdown-link-check": "3.12.2", 44 | "regenerator-runtime": "0.14.1", 45 | "rollup": "2.79.1", 46 | "rollup-plugin-babel": "4.4.0", 47 | "standard": "17.1.0" 48 | }, 49 | "dependencies": { 50 | "commander": "^11.1.0", 51 | "file-exists": "^5.0.1", 52 | "fs-readfile-promise": "^3.0.1", 53 | "js-yaml": "^4.1.0", 54 | "json-colorizer": "^2.2.2", 55 | "jsonata": "^2.0.5", 56 | "read-input": "^0.3.1" 57 | }, 58 | "bin": "bin/jfq.js" 59 | } 60 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![npm](https://img.shields.io/npm/v/jfq.svg)](https://www.npmjs.com/package/jfq) 2 | [![test](https://github.com/blgm/jfq/workflows/test/badge.svg?branch=main)](https://github.com/blgm/jfq/actions?query=workflow%3Atest+branch%3Amain) 3 | 4 | # jfq 5 | [JSONata](http://jsonata.org/) on the command line. 6 | 7 | This was inspired by the excellent [jq](https://stedolan.github.io/jq/) utility, and uses JSONata rather than the 8 | `jq` language. 9 | 10 | ## Installation 11 | ``` 12 | npm install --global jfq 13 | ``` 14 | 15 | ## Usage 16 | ``` 17 | jfq [options] [] [] 18 | ``` 19 | 20 | It is good practice to put the JSONata query in single quotes, so that the shell does 21 | not attempt to interpret it. 22 | 23 | The output will formatted as JSON, unless it's an array of simple objects (e.g. string, number) 24 | when the output is flattened to a series of lines, so that it can be piped to another program such as `xargs`. 25 | 26 | Options 27 | - `-n, --ndjson` output as newline-delimited JSON (each object on a single line) 28 | - `-j, --json` force output as JSON, when it would normally be flattened 29 | - `-y, --yaml` output as YAML 30 | - `-a, --accept-yaml` accept YAML input 31 | - `-q, --query-file ` read JSONata query from a file 32 | 33 | ## Examples 34 | - To read the version of JSONata from the file `package.json`: 35 | ``` 36 | jfq 'dependencies.jsonata' package.json 37 | 38 | # ^1.5.0 39 | ``` 40 | 41 | - To find out how many downloads of JSONata there have been each month in the past year: 42 | ``` 43 | curl -s \ 44 | https://api.npmjs.org/downloads/range/last-year/jsonata \ 45 | | jfq 'downloads{$substring(day, 0, 7): $sum(downloads)}' 46 | 47 | # { 48 | # "2017-02": 36216, 49 | # "2017-03": 46460, 50 | # "2017-04": 40336, 51 | # ... 52 | # } 53 | ``` 54 | -------------------------------------------------------------------------------- /src/__tests__/getopts.js: -------------------------------------------------------------------------------- 1 | /* eslint-env jest */ 2 | import getopts from '../getopts.js' 3 | 4 | const fakeArgv = (...opts) => ([process.execPath, '/tmp/fakePath.js', ...opts]) 5 | 6 | describe('getting command line options', () => { 7 | describe('when there are no options', () => { 8 | it('returns default values', async () => { 9 | const res = await getopts(fakeArgv()) 10 | expect(res.query).toBe('$') 11 | expect(res.files).toEqual([]) 12 | expect(res.ndjson).toBe(false) 13 | }) 14 | }) 15 | 16 | describe('when there is a query and a file', () => { 17 | it('reads the query and the file', async () => { 18 | const res = await getopts(fakeArgv('fake.query', 'fake.file')) 19 | expect(res.query).toBe('fake.query') 20 | expect(res.files).toEqual(['fake.file']) 21 | }) 22 | }) 23 | 24 | describe('when the first argument is a file and not a query', () => { 25 | it('reads it as a file, using the default query', async () => { 26 | const res = await getopts(fakeArgv('package.json')) 27 | expect(res.query).toBe('$') 28 | expect(res.files).toEqual(['package.json']) 29 | }) 30 | }) 31 | 32 | describe('when the first argument is too long to be a file name', () => { 33 | it('reads it as a query', async () => { 34 | const longquery = 'stuff'.repeat(1000) 35 | const res = await getopts(fakeArgv(longquery, 'fake.file')) 36 | expect(res.query).toEqual(longquery) 37 | expect(res.files).toEqual(['fake.file']) 38 | }) 39 | }) 40 | 41 | describe('when a query file is specified', () => { 42 | it('reads the query from the file', async () => { 43 | const res = await getopts(fakeArgv('-q', './src/__tests__/fixtures/query.jsonata')) 44 | expect(res.query).toContain('name') 45 | }) 46 | }) 47 | }) 48 | -------------------------------------------------------------------------------- /src/jfq.js: -------------------------------------------------------------------------------- 1 | import colorize from 'json-colorizer' 2 | import jsonata from 'jsonata' 3 | import readInput from 'read-input' 4 | import getopts from './getopts' 5 | import YAML from 'js-yaml' 6 | 7 | const main = async () => { 8 | const { files, ndjson, json, yamlIn, yamlOut, query, plainText } = await getopts(process.argv) 9 | const evaluator = parseQuery(query) 10 | const data = await readInput(files) 11 | 12 | for (const file of data.files) { 13 | if (file.error) { 14 | throw file.error 15 | } else { 16 | const input = yamlIn ? parseYaml(file.data, file.name) : parseJson(file.data, file.name) 17 | const result = await evaluator.evaluate(input) 18 | const output = yamlOut ? formatYaml(result) : formatJson(result, ndjson, json, plainText) 19 | console.log(output) 20 | } 21 | } 22 | } 23 | 24 | const parseQuery = query => { 25 | try { 26 | return jsonata(query) 27 | } catch (err) { 28 | throw new Error('Failed to compile JSONata expression: ' + err.message) 29 | } 30 | } 31 | 32 | const formatJson = (data, ndjson, json, plainText) => { 33 | if (typeof data === 'undefined') { 34 | return '' 35 | } 36 | 37 | if (!json) { 38 | if (typeof data === 'string') { 39 | return data 40 | } 41 | 42 | if (isSimpleArray(data)) { 43 | return data.join('\n') 44 | } 45 | } 46 | 47 | const formatted = ndjson ? JSON.stringify(data) : JSON.stringify(data, null, 2) 48 | return plainText ? formatted : colorize(formatted) 49 | } 50 | 51 | // Is it an array containing only simple types 52 | const isSimpleArray = arr => Array.isArray(arr) && !arr.some(i => typeof i !== 'string' && typeof i !== 'number') 53 | 54 | const parseYaml = (string, fileName) => { 55 | try { 56 | return YAML.load(string, { json: true }) 57 | } catch (err) { 58 | if (fileName) { 59 | throw new Error(err.message + ' in file ' + fileName) 60 | } else { 61 | throw err 62 | } 63 | } 64 | } 65 | 66 | const formatYaml = yaml => typeof yaml === 'undefined' ? '' : YAML.dump(yaml) 67 | 68 | const parseJson = (string, fileName) => { 69 | try { 70 | return JSON.parse(string) 71 | } catch (err) { 72 | if (fileName) { 73 | throw new Error(err.message + ' in file ' + fileName) 74 | } else { 75 | throw err 76 | } 77 | } 78 | } 79 | 80 | main() 81 | .catch(err => { 82 | console.error(err.message) 83 | process.exit(1) 84 | }) 85 | -------------------------------------------------------------------------------- /src/__tests__/jfq_inputs.js: -------------------------------------------------------------------------------- 1 | /* eslint-env jest */ 2 | 3 | import { run, runStdin } from '../test-helper' 4 | 5 | describe('inputs', () => { 6 | describe('methods of input', () => { 7 | it('takes input from a file', async () => { 8 | const res = await run('$', 'package.json') 9 | expect(res.error).toBeNull() 10 | expect(res.stderr).toBe('') 11 | expect(JSON.parse(res.stdout).name).toEqual('jfq') 12 | }) 13 | 14 | it('takes input from STDIN', async () => { 15 | const input = '{"fake":"data"}' 16 | const res = await runStdin(input, 'fake') 17 | expect(res.error).toBeNull() 18 | expect(res.stderr).toBe('') 19 | expect(res.stdout).toEqual('data') 20 | }) 21 | 22 | describe('when there are multiple files', () => { 23 | it('takes input from multiple files', async () => { 24 | const res = await run('foo', 'src/__tests__/fixtures/a.json', 'src/__tests__/fixtures/b.json') 25 | expect(res.error).toBeNull() 26 | expect(res.stderr).toBe('') 27 | expect(res.stdout).toEqual('bar\nbaz') 28 | }) 29 | 30 | it('joins the output of arrays nicely', async () => { 31 | const res = await run('listy', 'src/__tests__/fixtures/a.json', 'src/__tests__/fixtures/b.json') 32 | expect(res.error).toBeNull() 33 | expect(res.stderr).toBe('') 34 | expect(res.stdout).toEqual('alpha\nbeta\ngamma\ndelta') 35 | }) 36 | }) 37 | }) 38 | 39 | describe('ommitting the expression', () => { 40 | it('treats the first argument as an input file', async () => { 41 | const res = await run('package.json') 42 | expect(res.error).toBeNull() 43 | expect(res.stderr).toBe('') 44 | expect(JSON.parse(res.stdout).name).toEqual('jfq') 45 | }) 46 | }) 47 | 48 | describe('JSON parse errors', () => { 49 | it('reports the position in STDIN', async () => { 50 | const input = '{"foo": bar' 51 | const res = await runStdin(input) 52 | expect(res.error.message).toContain('Unexpected token b in JSON at position 8') 53 | }) 54 | 55 | it('reports the position and file name for files', async () => { 56 | const res = await run('$', 'src/__tests__/fixtures/bad.json') 57 | expect(res.error.message).toContain('Unexpected token f in JSON at position 4 in file src/__tests__/fixtures/bad.json') 58 | }) 59 | }) 60 | 61 | describe('YAML input', () => { 62 | it('accepts YAML input', async () => { 63 | const res = await run('-a', 'baz', 'src/__tests__/fixtures/c.yaml') 64 | expect(res.error).toBeNull() 65 | expect(res.stderr).toBe('') 66 | expect(res.stdout).toBe('alpha\nbeta') 67 | }) 68 | 69 | describe('parse errors', () => { 70 | it('reports the file name', async () => { 71 | const res = await run('-a', '$', 'src/__tests__/fixtures/bad.yaml') 72 | expect(res.error.message).toContain('in file src/__tests__/fixtures/bad.yaml') 73 | }) 74 | 75 | it('does not report file names with stdin', async () => { 76 | const res = await runStdin('{foo}}', '-a') 77 | expect(res.error.message).toContain('end of the stream or a document separator is expected (1:6)') 78 | }) 79 | }) 80 | }) 81 | 82 | describe('files not found', () => { 83 | it('reports the error', async () => { 84 | const res = await run('$', 'not_here_sucker.json') 85 | expect(res.error.message).toContain("ENOENT: no such file or directory, open 'not_here_sucker.json'") 86 | }) 87 | }) 88 | 89 | describe('query in a file', () => { 90 | it('reads the file', async () => { 91 | const res = await run('-q', './src/__tests__/fixtures/query.jsonata', 'package.json') 92 | expect(res.error).toBeNull() 93 | expect(res.stderr).toBe('') 94 | expect(res.stdout).toContain('jfq') 95 | }) 96 | }) 97 | }) 98 | -------------------------------------------------------------------------------- /src/__tests__/jfq_outputs.js: -------------------------------------------------------------------------------- 1 | /* eslint-env jest */ 2 | 3 | import { run, runStdin } from '../test-helper' 4 | import YAML from 'js-yaml' 5 | 6 | describe('output format', () => { 7 | describe('when there is no output', () => { 8 | it('outputs nothing', async () => { 9 | const res = await run('notexists', 'package.json') 10 | expect(res.error).toBeNull() 11 | expect(res.stderr).toBe('') 12 | expect(res.stdout).toBe('') 13 | }) 14 | }) 15 | 16 | describe('when the output is a single string', () => { 17 | it('prints it as an undecorated string', async () => { 18 | const res = await run('name', 'package.json') 19 | expect(res.error).toBeNull() 20 | expect(res.stderr).toBe('') 21 | expect(res.stdout).toBe('jfq') 22 | }) 23 | 24 | it('prints a decorated string when the -j flag is specified', async () => { 25 | const res = await run('-j', 'name', 'package.json') 26 | expect(res.error).toBeNull() 27 | expect(res.stderr).toBe('') 28 | expect(res.stdout).toBe('"jfq"') 29 | }) 30 | }) 31 | 32 | describe('when the output is a single number', () => { 33 | it('prints it as an undecorated number', async () => { 34 | const input = '{"num":42}' 35 | const res = await runStdin(input, 'num') 36 | expect(res.error).toBeNull() 37 | expect(res.stderr).toBe('') 38 | expect(res.stdout).toBe('42') 39 | }) 40 | }) 41 | 42 | describe('when the output is an array of string/number types', () => { 43 | it('prints each entry on a new line', async () => { 44 | const input = '{"arr":[1,"foo"]}' 45 | const res = await runStdin(input, 'arr') 46 | expect(res.error).toBeNull() 47 | expect(res.stderr).toBe('') 48 | expect(res.stdout).toBe('1\nfoo') 49 | }) 50 | 51 | it('prints as JSON when the -j flag is specified', async () => { 52 | const data = { arr: [1, 'foo'] } 53 | const input = JSON.stringify(data) 54 | const expected = JSON.stringify(data.arr, null, 2) 55 | const res = await runStdin(input, '-j', 'arr') 56 | expect(res.error).toBeNull() 57 | expect(res.stderr).toBe('') 58 | expect(res.stdout).toBe(expected) 59 | }) 60 | }) 61 | 62 | describe('when the output is an array with complex types', () => { 63 | it('prints JSON', async () => { 64 | const data = { arr: [1, 'foo', [6]] } 65 | const input = JSON.stringify(data) 66 | const expected = JSON.stringify(data.arr, null, 2) 67 | const res = await runStdin(input, 'arr') 68 | expect(res.error).toBeNull() 69 | expect(res.stderr).toBe('') 70 | expect(res.stdout).toBe(expected) 71 | }) 72 | }) 73 | 74 | describe('default', () => { 75 | it('outputs as formatted JSON over multiple lines', async () => { 76 | const data = { fake: [{ json: 'data' }] } 77 | const input = JSON.stringify(data) 78 | const expected = JSON.stringify(data, null, 2) 79 | const res = await runStdin(input) 80 | expect(res.error).toBeNull() 81 | expect(res.stderr).toBe('') 82 | expect(res.stdout).toEqual(expected) 83 | }) 84 | }) 85 | 86 | describe('when the `-n` flag is specified', () => { 87 | it('outputs as formatted JSON', async () => { 88 | const data = { fake: [{ json: 'data' }] } 89 | const input = JSON.stringify(data) 90 | const expected = JSON.stringify(data) 91 | const res = await runStdin(input, '-n') 92 | expect(res.error).toBeNull() 93 | expect(res.stderr).toBe('') 94 | expect(res.stdout).toEqual(expected) 95 | }) 96 | 97 | describe('when the `-n` flag is specified with other options', () => { 98 | it('outputs as formatted JSON', async () => { 99 | const res = await run('-n', 'bugs', 'package.json') 100 | expect(res.error).toBeNull() 101 | expect(res.stderr).toBe('') 102 | expect(res.stdout).toEqual('{"url":"https://github.com/blgm/jfq/issues"}') 103 | }) 104 | }) 105 | }) 106 | 107 | describe('when the `-y` flag is specified', () => { 108 | it('prints output in YAML', async () => { 109 | const data = { fake: [{ json: 'data' }] } 110 | const input = JSON.stringify(data) 111 | const expected = YAML.dump(data).trim() 112 | const res = await runStdin(input, '-y') 113 | expect(res.error).toBeNull() 114 | expect(res.stderr).toBe('') 115 | expect(res.stdout).toEqual(expected) 116 | }) 117 | 118 | describe('when there is no output', () => { 119 | it('outputs nothing', async () => { 120 | const res = await run('-y', 'notexists', 'package.json') 121 | expect(res.error).toBeNull() 122 | expect(res.stderr).toBe('') 123 | expect(res.stdout).toBe('') 124 | }) 125 | }) 126 | }) 127 | }) 128 | --------------------------------------------------------------------------------