@@ -62,11 +62,11 @@ const doShell = (argv, options, execOptions) => new Promise((resolve, reject) =>
6262 } else {
6363 // otherwise, respond with the output of the command;
6464 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+ }
7070
7171 } else {
7272 resolve ( true )
@@ -87,6 +87,7 @@ const doShell = (argv, options, execOptions) => new Promise((resolve, reject) =>
8787
8888 // accumulate doms from the output of the subcommand
8989 const parentNode = document . createElement ( 'div' )
90+ let rawOut = ''
9091 let rawErr = ''
9192
9293 proc . stdout . on ( 'data' , data => {
@@ -97,6 +98,7 @@ const doShell = (argv, options, execOptions) => new Promise((resolve, reject) =>
9798 parentNode . appendChild ( span )
9899 span . setAttribute ( 'class' , 'whitespace' )
99100 span . appendChild ( document . createTextNode ( data ) )
101+ rawOut += data . toString ( )
100102 }
101103 } )
102104
@@ -118,8 +120,22 @@ const doShell = (argv, options, execOptions) => new Promise((resolve, reject) =>
118120 console . log ( 'shell command done' )
119121 if ( exitCode === 0 ) {
120122 // 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+ }
123139 } else {
124140 // oops, non-zero exit code. reject!
125141 if ( execOptions && execOptions . nested ) {
0 commit comments