@@ -62,11 +62,11 @@ const doShell = (argv, options, execOptions) => new Promise((resolve, reject) =>
62
62
} else {
63
63
// otherwise, respond with the output of the command;
64
64
if ( output ) {
65
- // add a line-wrapping wrapper
66
- const wrapper = document . createElement ( 'div' )
67
- wrapper . classList . add ( 'whitespace' )
68
- wrapper . innerText = cmd === 'ls' ? output . toString ( ) . replace ( / , / g , ' ' ) : output . toString ( )
69
- resolve ( wrapper )
65
+ if ( execOptions && execOptions . json ) {
66
+ resolve ( JSON . parse ( output ) )
67
+ } else {
68
+ resolve ( output )
69
+ }
70
70
71
71
} else {
72
72
resolve ( true )
@@ -87,6 +87,7 @@ const doShell = (argv, options, execOptions) => new Promise((resolve, reject) =>
87
87
88
88
// accumulate doms from the output of the subcommand
89
89
const parentNode = document . createElement ( 'div' )
90
+ let rawOut = ''
90
91
let rawErr = ''
91
92
92
93
proc . stdout . on ( 'data' , data => {
@@ -97,6 +98,7 @@ const doShell = (argv, options, execOptions) => new Promise((resolve, reject) =>
97
98
parentNode . appendChild ( span )
98
99
span . setAttribute ( 'class' , 'whitespace' )
99
100
span . appendChild ( document . createTextNode ( data ) )
101
+ rawOut += data . toString ( )
100
102
}
101
103
} )
102
104
@@ -118,8 +120,22 @@ const doShell = (argv, options, execOptions) => new Promise((resolve, reject) =>
118
120
console . log ( 'shell command done' )
119
121
if ( exitCode === 0 ) {
120
122
// great, the process exited normally. resolve!
121
- //resolve(execOptions.stdout ? stdoutLines : parentNode)
122
- resolve ( parentNode )
123
+ if ( execOptions && execOptions . json ) {
124
+ // caller expects JSON back
125
+ try {
126
+ resolve ( JSON . parse ( rawOut ) )
127
+ } catch ( err ) {
128
+ reject ( { message : 'unexpected non-JSON' , value : rawOut } )
129
+ }
130
+
131
+ } else if ( execOptions && execOptions . raw ) {
132
+ // caller just wants the raw textual output
133
+ resolve ( rawOut )
134
+
135
+ } else {
136
+ // else, we pass back a formatted form of the output
137
+ resolve ( parentNode )
138
+ }
123
139
} else {
124
140
// oops, non-zero exit code. reject!
125
141
if ( execOptions && execOptions . nested ) {
0 commit comments