Skip to content

Commit

Permalink
feat: add basic cli (#206)
Browse files Browse the repository at this point in the history
Co-authored-by: Brett Zamir <brettz9@yahoo.com>
  • Loading branch information
vid and brettz9 authored Mar 5, 2024
1 parent b292631 commit c0b51ec
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <json-file> <jsonpath-query>`.

## Ideas

1. Support OR outside of filters (as in XPath `|`) and grouping.
Expand Down Expand Up @@ -396,3 +400,4 @@ npm run browser-test
## License

[MIT License](https://opensource.org/license/mit/).

2 changes: 1 addition & 1 deletion badges/coverage-badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions bin/jsonpath-cli.js
Original file line number Diff line number Diff line change
@@ -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]} <file> <path>\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);
}
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
7 changes: 6 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function getRollupObject ({
babel({
babelrc: false,
presets: [
environment === 'node'
environment === 'node' || environment === 'cli'
? ['@babel/preset-env', {
targets: [
`node ${pkg.engines.node}`
Expand Down Expand Up @@ -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'})
];

0 comments on commit c0b51ec

Please sign in to comment.