-
-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathjsonpath-cli.js
More file actions
executable file
·36 lines (31 loc) · 768 Bytes
/
jsonpath-cli.js
File metadata and controls
executable file
·36 lines (31 loc) · 768 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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);
}