forked from HeyPuter/puter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjq.js
76 lines (70 loc) · 2.46 KB
/
jq.js
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
/*
* Copyright (C) 2024 Puter Technologies Inc.
*
* This file is part of Phoenix Shell.
*
* Phoenix Shell is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import jsonQuery from 'json-query';
import { signals } from '../../ansi-shell/signals.js';
import { Exit } from './coreutil_lib/exit.js';
export default {
name: 'jq',
usage: 'jq FILTER [FILE...]',
description: 'Process JSON input FILE(s) according to FILTER.\n\n' +
'Reads from standard input if no FILE is provided.',
input: {
syncLines: true,
},
args: {
$: 'simple-parser',
allowPositionals: true,
},
execute: async ctx => {
const { externs } = ctx;
const { sdkv2 } = externs;
const { positionals } = ctx.locals;
const [query] = positionals;
// Read one line at a time
const { in_, out, err } = ctx.externs;
let rslv_sigint;
const p_int = new Promise(rslv => rslv_sigint = rslv);
ctx.externs.sig.on((signal) => {
if ( signal === signals.SIGINT ) {
rslv_sigint({ is_sigint: true });
}
});
let line, done;
const next_line = async () => {
let is_sigint = false;
({ value: line, done, is_sigint } = await Promise.race([
p_int, in_.read(),
]));
if ( is_sigint ) {
throw new Exit(130);
}
// ({ value: line, done } = await in_.read());
}
for ( await next_line() ; ! done ; await next_line() ) {
let data; try {
data = JSON.parse(line);
} catch (e) {
await err.write('Error: ' + e.message + '\n');
continue;
}
const result = jsonQuery(query, { data });
await out.write(JSON.stringify(result.value) + '\n');
}
}
}