diff --git a/README.md b/README.md index 2703dba..8519f39 100644 --- a/README.md +++ b/README.md @@ -368,6 +368,10 @@ from 0), whereas in XPath, they are 1-based. 1. In JSONPath, equality tests utilize (as per JavaScript) multiple equal signs whereas in XPath, they use a single equal sign. +## Command line interface + +A basic command line interface (CLI) is provided. Access it using `npx jsonpath-plus `. + ## Ideas 1. Support OR outside of filters (as in XPath `|`) and grouping. @@ -396,3 +400,4 @@ npm run browser-test ## License [MIT License](https://opensource.org/license/mit/). + diff --git a/badges/coverage-badge.svg b/badges/coverage-badge.svg index 9e7204d..4f849d6 100644 --- a/badges/coverage-badge.svg +++ b/badges/coverage-badge.svg @@ -1 +1 @@ -Statements 100%Statements 100%Branches 100%Branches 100%Lines 100%Lines 100%Functions 100%Functions 100% +Statements 95.88%Statements 95.88%Branches 99.61%Branches 99.61%Lines 95.88%Lines 95.88%Functions 95%Functions 95% diff --git a/bin/jsonpath-cli.js b/bin/jsonpath-cli.js new file mode 100755 index 0000000..58fbb4b --- /dev/null +++ b/bin/jsonpath-cli.js @@ -0,0 +1,36 @@ +#!/usr/bin/env node +import {readFile} from 'fs/promises'; +import {JSONPath as jsonpath} from '../dist/index-node-esm.js'; + +const file = process.argv[2]; +const path = process.argv[3]; + +try { + const json = JSON.parse(await readFile(file, 'utf8')); + runQuery(json, path); +} catch (e) { + /* eslint-disable no-console -- CLI */ + console.error(`usage: ${process.argv[1]} \n`); + console.error(e); + /* eslint-enable no-console -- CLI */ + process.exit(1); +} + +/** + * @typedef {any} JSON + */ + +/** + * @param {JSON} json + * @param {string} pth + * @returns {void} + */ +function runQuery (json, pth) { + const result = jsonpath({ + json, + path: pth + }); + + // eslint-disable-next-line no-console -- CLI + console.log(result); +} diff --git a/package.json b/package.json index eced99d..497d0c2 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,10 @@ "name": "jsonpath-plus", "version": "8.0.0", "type": "module", + "bin": { + "jsonpath": "./bin/jsonpath-cli.js", + "jsonpath-plus": "./bin/jsonpath-cli.js" + }, "main": "dist/index-node-cjs.cjs", "exports": { "./package.json": "./package.json", @@ -137,6 +141,7 @@ "node-import-test": "node --experimental-modules demo/node-import-test.mjs", "open": "open-cli http://localhost:8084/demo/ && npm start", "start": "http-server -p 8084", + "cli": "./bin/jsonpath-cli.js package.json name", "typescript": "tsc -p src", "mocha": "mocha --require test-helpers/node-env.js --reporter-options configFile=mocha-multi-reporters.json test", "c8": "rm -Rf ./coverage && rm -Rf ./node_modules/.cache && c8 --all npm run mocha && npm run coverage-badge", diff --git a/rollup.config.js b/rollup.config.js index d234ab5..c40109b 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -42,7 +42,7 @@ function getRollupObject ({ babel({ babelrc: false, presets: [ - environment === 'node' + environment === 'node' || environment === 'cli' ? ['@babel/preset-env', { targets: [ `node ${pkg.engines.node}` @@ -90,6 +90,11 @@ function getRollupObjectByEnv ({minifying, environment}) { export default [ ...getRollupObjectByEnv({minifying: false, environment: 'node'}), // ...getRollupObjectByEnv({minifying: true, environment: 'node'}), + // getRollupObject({ + // input: 'bin/jsonpath-cli.js', format: 'esm', + // minifying: false, environment: 'cli', + // external: ['fs/promises', 'vm'] + // }), ...getRollupObjectByEnv({minifying: false, environment: 'browser'}), ...getRollupObjectByEnv({minifying: true, environment: 'browser'}) ];