Skip to content
This repository was archived by the owner on Dec 17, 2024. It is now read-only.

Commit 0d2adeb

Browse files
committed
update shell i.e. ! plugin to allow callers to request the raw model/data
Fixes #916
1 parent b6f2b62 commit 0d2adeb

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

app/plugins/ui/commands/shell.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)