Skip to content

Commit da2d6c6

Browse files
asopianspringcomp
andauthored
feat(cli): provide executable script for the package (#52)
* feat(cli): provide executable script for the package --------- Co-authored-by: Springcomp <[email protected]>
1 parent 9cb9462 commit da2d6c6

File tree

5 files changed

+449
-1517
lines changed

5 files changed

+449
-1517
lines changed

bin/jp.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#! /usr/bin/env node
2+
3+
'use strict';
4+
5+
import * as fs from 'fs';
6+
import { ParseArgsConfig, parseArgs } from 'node:util';
7+
import pkg from '../package.json';
8+
import jmespath, { JSONValue } from '../src';
9+
10+
const args = getArgs();
11+
12+
if (args.values.help) {
13+
printHelp();
14+
process.exit(0);
15+
}
16+
17+
if (!args.values['expr-file'] && args.positionals.length < 1) {
18+
console.log('Must provide a jmespath expression.');
19+
process.exit(1);
20+
}
21+
22+
let expression = '';
23+
if (args.values['expr-file']) {
24+
expression = fs.readFileSync(<string>args.values['expr-file'], { encoding: 'utf8', flag: 'r' });
25+
} else {
26+
expression = args.positionals[0];
27+
}
28+
29+
let inputJSON = '';
30+
if (args?.values?.filename) {
31+
inputJSON = fs.readFileSync(<string>args.values.filename, { encoding: 'utf8', flag: 'r' });
32+
printResult(inputJSON, expression, <boolean>args.values.compact);
33+
} else {
34+
process.stdin.setEncoding('utf-8');
35+
process.stdin.on('readable', function () {
36+
const chunk = process.stdin.read();
37+
if (chunk !== null) {
38+
inputJSON += chunk;
39+
}
40+
});
41+
42+
process.stdin.on('end', function () {
43+
printResult(inputJSON, expression, <boolean>args.values.compact);
44+
});
45+
}
46+
47+
function getArgs() {
48+
const config: ParseArgsConfig = {
49+
options: {
50+
compact: {
51+
type: 'boolean',
52+
short: 'c',
53+
default: false,
54+
},
55+
help: {
56+
type: 'boolean',
57+
short: 'h',
58+
default: false,
59+
},
60+
filename: {
61+
type: 'string',
62+
short: 'f',
63+
},
64+
'expr-file': {
65+
type: 'string',
66+
short: 'e',
67+
},
68+
},
69+
allowPositionals: true,
70+
};
71+
72+
return parseArgs(config);
73+
}
74+
75+
function printHelp(): void {
76+
console.log(`
77+
NAME:
78+
jp - jp [<options>] <expression>
79+
80+
USAGE:
81+
jp [global options] command [command options] [arguments...]
82+
83+
VERSION:
84+
${pkg.name}@${pkg.version}
85+
86+
OPTIONS:
87+
--compact, -c Produce compact JSON output that omits nonessential whitespace.
88+
--filename value, -f value Read input JSON from a file instead of stdin.
89+
--expr-file value, -e value Read JMESPath expression from the specified file.
90+
--help, -h Show help
91+
`);
92+
}
93+
94+
function printResult(inputJSON: string, expression: string, compact = false) {
95+
let parsedInput: JSONValue | null = null;
96+
97+
try {
98+
parsedInput = JSON.parse(inputJSON);
99+
} catch (e) {
100+
throw e;
101+
}
102+
103+
try {
104+
console.log(JSON.stringify(jmespath.search(parsedInput, expression, undefined), null, compact ? 0 : 2));
105+
} catch (e) {
106+
throw e;
107+
}
108+
}

0 commit comments

Comments
 (0)