1
1
const fs = require ( "node:fs/promises" ) ;
2
- const { exec } = require ( "node:child_process" ) ;
2
+ const { exec, spawn } = require ( "node:child_process" ) ;
3
3
const { promisify } = require ( "node:util" ) ;
4
4
const path = require ( "node:path" ) ;
5
5
const { tmpdir } = require ( "node:os" ) ;
6
6
7
- const jq = require ( "node-jq" ) ;
8
7
const jsonSchemaGenerator = require ( "json-schema-generator" ) ;
9
8
const input = require ( "@inquirer/input" ) . default ;
10
9
@@ -17,7 +16,7 @@ exports._instructions = async function () {
17
16
json_file_path = value ;
18
17
} catch {
19
18
generate_json_command_context = `command_to_generate_json: \`${ value } \`\n` ;
20
- const { stdout } = await promisify ( exec ) ( value , { maxBuffer : 100 * 1024 * 1024 } ) ;
19
+ const { stdout } = await promisify ( exec ) ( value , { maxBuffer : 100 * 1024 * 1024 } ) ;
21
20
json_file_path = path . join ( tmpdir ( ) , `${ process . env . LLM_AGENT_NAME } -${ process . pid } .data.json` ) ;
22
21
await fs . writeFile ( json_file_path , stdout ) ;
23
22
console . log ( `ⓘ Generated json data saved to: ${ json_file_path } ` ) ;
@@ -44,6 +43,19 @@ json_schema: ${JSON.stringify(json_schema, null, 2)}
44
43
*/
45
44
exports . print_json = async function ( args ) {
46
45
const { json_file_path, jq_expr } = args ;
47
- const result = await jq . run ( jq_expr , json_file_path , { raw : true } ) ;
48
- console . log ( result ) ;
46
+ return new Promise ( ( resolve , reject ) => {
47
+ const child = spawn ( "jq" , [ "-r" , jq_expr , json_file_path ] , { stdio : "inherit" } ) ;
48
+
49
+ child . on ( 'close' , code => {
50
+ if ( code === 0 ) {
51
+ resolve ( ) ;
52
+ } else {
53
+ reject ( new Error ( `jq exited with code ${ code } ` ) ) ;
54
+ }
55
+ } ) ;
56
+
57
+ child . on ( 'error' , err => {
58
+ reject ( err ) ;
59
+ } ) ;
60
+ } ) ;
49
61
}
0 commit comments