-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjpp
More file actions
executable file
·131 lines (110 loc) · 4.25 KB
/
jpp
File metadata and controls
executable file
·131 lines (110 loc) · 4.25 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env node
// jpp - jsonpath-plus command-line utility.
// MIT License
// Copyright (c) 2021 John Scott.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
'use strict';
const fs = require('fs');
const pkg = require('../package.json');
const {program} = require('commander');
const {JSONPath} = require('jsonpath-plus');
program
.version(pkg.version, '-V, --version', 'display version')
.usage('[options] [JSONPath Expression] [JSON Input File]')
.option('-i, --input <file>', 'input JSON file (default: stdin)')
.option('-e, --jsonpath <expression>', 'matching expression')
.option('-o, --output <file>', 'output file for matches (default: stdout)')
.option('-p, --pretty', 'pretty print matches', false)
.option('-s, --separate', 'output each match separately', false)
.option('-n, --noquote', 'if matches are strings, don\'t wrap in quotes (assumes --separate)', false)
.option('-b, --before <text>', 'output text before any matches')
.option('-r, --prefix [prefix]', 'prefix when using --separate', false)
.option('-u, --suffix <suffix>', 'suffix each match except last with <suffix>')
.option('-U, --suffix-all <suffix>', 'suffix all matches with <suffix>')
.option('-a, --after <text>', "output text after all matches")
.parse(process.argv);
if (process.argv.length === 2) {
program.help();
}
let options = program.opts();
let arg = 0;
let input = options.input;
try {
let jsonpath = options.jsonpath;
if(!jsonpath) {
if (program.args.length <= arg) {
throw new Error('Missing JSONPath expression');
}
jsonpath = program.args[arg++];
}
if (!input && program.args.length > arg) {
input = program.args[arg++];
}
let file = fs.readFileSync(input ? input : 0); // 0 = stdin
let json = JSON.parse(file);
let results = JSONPath(jsonpath, json);
let fout = options.output ? fs.openSync(options.output, 'w') : 1; // 1 = stdout
if(options.before) {
fs.writeSync(fout, options.before.replace('{0}', `${results.length}`) + '\n');
}
if(!options.separate) {
let data;
if(options.pretty) {
data = JSON.stringify(results, null, 2);
} else {
data = JSON.stringify(results);
}
fs.writeSync(fout, data);
fs.writeSync(fout, '\n');
} else {
let i=1;
results.forEach(result => {
let data;
if(options.noquote && typeof result === 'string') {
data = result;
} else if(options.pretty) {
data = JSON.stringify(result, null, 2);
} else {
data = JSON.stringify(result);
}
if(options.prefix) {
if(options.prefix === true) {
fs.writeSync(fout, `${i} `);
} else {
fs.writeSync(fout, options.prefix.replace('{0}', `${i}`));
}
}
fs.writeSync(fout, data);
if(options.suffixAll) {
fs.writeSync(fout, options.suffixAll.replace('{0}', `${i}`));
} else if(options.suffix && i < results.length) {
fs.writeSync(fout, options.suffix.replace('{0}', `${i}`));
}
fs.writeSync(fout, '\n');
i++;
});
}
if(options.after) {
fs.writeSync(fout, options.after.replace('{0}', `${results.length}`) + '\n');
}
if (options.output) {
fs.closeSync(fout);
}
} catch(error) {
console.error(error.name, error.message);
process.exit(1);
}