Skip to content

Commit 64bdded

Browse files
authored
Merge pull request #7 from capsulerun/feature/v0.1.0/manage-executor-exception
feat: add support for /dev/null, /dev/stdout, and /dev/stderr
2 parents 3fd934d + 1fcf062 commit 64bdded

1 file changed

Lines changed: 27 additions & 2 deletions

File tree

packages/bash/src/core/executor.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ export class Executor {
2424

2525
for (const r of node.redirects) {
2626
if (r.op === '<') {
27+
if (r.file === '/dev/null') {
28+
stdin = '';
29+
continue;
30+
}
31+
2732
try {
2833
stdin = await this.runtime.executeCode(this.state, `
2934
const fs = require('fs');
@@ -45,25 +50,45 @@ export class Executor {
4550
result = { stdout: '', stderr: `bash: ${name}: command not found`, exitCode: 127 };
4651
}
4752

53+
let currentStdout = result.stdout;
54+
let currentStderr = result.stderr;
55+
4856
for (const r of node.redirects) {
4957
if (r.op === '>' || r.op === '>>') {
58+
if (r.file === '/dev/null') {
59+
currentStdout = '';
60+
continue;
61+
}
62+
63+
if (r.file === '/dev/stdout') {
64+
continue;
65+
}
66+
67+
if (r.file === '/dev/stderr') {
68+
currentStderr += currentStdout;
69+
currentStdout = '';
70+
continue;
71+
}
72+
5073
try {
5174
await this.runtime.executeCode(this.state, `
5275
const fs = require('fs');
5376
const path = require('path');
5477
const filePath = path.resolve(${JSON.stringify(r.file)});
5578
5679
fs.mkdirSync(path.dirname(filePath), { recursive: true });
57-
fs.${r.op === '>>' ? 'appendFileSync' : 'writeFileSync'}(filePath, ${JSON.stringify(result.stdout)});
80+
fs.${r.op === '>>' ? 'appendFileSync' : 'writeFileSync'}(filePath, ${JSON.stringify(currentStdout)});
5881
`);
5982

60-
result = { ...result, stdout: '' };
83+
currentStdout = '';
6184
} catch {
6285
return { stdout: '', stderr: `bash: ${r.file}: No such file or directory`, exitCode: 1 };
6386
}
6487
}
6588
}
6689

90+
result = { ...result, stdout: currentStdout, stderr: currentStderr };
91+
6792
this.state.setLastExitCode(result.exitCode);
6893
return result;
6994
}

0 commit comments

Comments
 (0)