File tree 3 files changed +101
-7
lines changed
3 files changed +101
-7
lines changed Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env node
2
+ 'use strict' ;
3
+
4
+ const assert = require ( 'assert' ) ;
5
+ const path = require ( 'path' ) ;
6
+ const cp = require ( 'child_process' ) ;
7
+ const os = require ( 'os' ) ;
8
+ const fs = require ( 'fs' ) ;
9
+ const EE = require ( 'events' ) ;
10
+
11
+ process . on ( 'uncaughtException' , err => {
12
+ console . error ( 'uncaughtException:' , { err} ) ;
13
+ process . exit ( 1 ) ;
14
+ } ) ;
15
+
16
+ process . on ( 'unhandledRejection' , ( reason , p ) => {
17
+ // note: unless we force process to exit with 1, process may exit with 0 upon an unhandledRejection
18
+ console . error ( reason ) ;
19
+ process . exit ( 1 ) ;
20
+ } ) ;
21
+
22
+ const { JSONParser} = require ( '@oresoftware/json-stream-parser' ) ;
23
+
24
+ console . log ( 'Running test' , __filename ) ;
25
+
26
+ const k = cp . spawn ( 'bash' ) ;
27
+ const foo = 'medicine' ;
28
+
29
+ k . stdin . end ( `
30
+
31
+ foo="${ foo } "
32
+
33
+ cat <<EOF
34
+
35
+ {"foo":"$foo"} ∆∆
36
+ {"foo":"$foo"} ∆
37
+ ∆{"foo":"$foo"}∆
38
+
39
+ EOF
40
+
41
+ ` ) ;
42
+
43
+ const to = setTimeout ( ( ) => {
44
+ console . error ( 'did not receive parsed JSON object within alloted time.' ) ;
45
+ process . exit ( 1 ) ;
46
+ } , 2000 ) ;
47
+
48
+
49
+ let count = 0 ;
50
+
51
+ k . stdout . pipe ( new JSONParser ( { delimiter :'∆' } ) ) . on ( 'data' , d => {
52
+
53
+ count ++ ;
54
+
55
+ if ( count > 3 ) {
56
+ throw new Error ( 'too many json blobs.' ) ;
57
+ }
58
+
59
+ if ( count === 3 ) {
60
+
61
+ setTimeout ( ( ) => {
62
+
63
+ clearTimeout ( to ) ;
64
+ try {
65
+ assert . deepStrictEqual ( d , { foo : foo } ) ;
66
+ process . exit ( 0 ) ;
67
+ }
68
+ catch ( err ) {
69
+ console . error ( err . message ) ;
70
+ process . exit ( 1 ) ;
71
+ }
72
+
73
+ } , 1000 ) ;
74
+
75
+ }
76
+
77
+
78
+ } ) ;
79
+
Original file line number Diff line number Diff line change @@ -75,7 +75,7 @@ const k = cp.spawn('bash');
75
75
k .stdin .end (` echo '{"foo":"bar"}\n '` ); // make sure to include the newline char when you write
76
76
77
77
k .stdout .pipe (new JSONParser ()).on (' data' , d => {
78
- // => {foo:'bar'}
78
+ // => {foo: 'bar'}
79
79
});
80
80
81
81
```
@@ -94,9 +94,7 @@ k.stdin.end(`
94
94
` );
95
95
96
96
k .stdout .pipe (new JSONParser ()).on (' data' , d => {
97
-
98
- assert .deepStrictEqual (d, {foo: ' medicine' });
99
-
97
+ assert .deepStrictEqual (d, {foo: ' medicine' }); // should pass
100
98
});
101
99
102
100
@@ -111,4 +109,7 @@ new JSONParser({delimiter: '∆∆∆'}); // use 3 alt-j's to separate json chu
111
109
112
110
```
113
111
112
+ For other solutions to parsing JSON from CLIs, see:
113
+ https://stackoverflow.com/questions/56014438/get-single-line-json-from-aws-cli
114
+
114
115
Original file line number Diff line number Diff line change @@ -51,17 +51,22 @@ export class JSONParser extends stream.Transform {
51
51
continue ;
52
52
}
53
53
54
+ let json = null ;
55
+
54
56
try {
55
57
l = String ( l ) . trim ( ) ;
56
58
// l might be an empty string; ignore if so
57
- l && this . push ( JSON . parse ( String ( l ) . trim ( ) ) ) ;
59
+ l && ( json = JSON . parse ( String ( l ) . trim ( ) ) ) ;
58
60
} catch ( err ) {
59
61
if ( this . debug ) {
60
62
console . error ( 'json-parser:' , 'error parsing line:' , l ) ;
61
63
console . error ( 'json-parser:' , err . message ) ;
62
64
}
65
+ continue ;
63
66
// noop
64
67
}
68
+
69
+ json && this . push ( json ) ;
65
70
}
66
71
67
72
cb ( ) ;
@@ -70,11 +75,20 @@ export class JSONParser extends stream.Transform {
70
75
71
76
_flush ( cb : Function ) {
72
77
if ( this . lastLineData ) {
78
+
79
+ let json = null , l = String ( this . lastLineData ) . trim ( ) ;
80
+
73
81
try {
74
- this . push ( JSON . parse ( this . lastLineData ) ) ;
82
+ json = JSON . parse ( l ) ;
75
83
} catch ( err ) {
76
- // noop
84
+ if ( this . debug ) {
85
+ console . error ( 'json-parser:' , 'error parsing line:' , l ) ;
86
+ console . error ( 'json-parser:' , err . message ) ;
87
+ }
77
88
}
89
+
90
+ json && this . push ( json ) ;
91
+
78
92
}
79
93
this . lastLineData = '' ;
80
94
cb ( ) ;
You can’t perform that action at this time.