mirror of
https://github.com/markedjs/marked.git
synced 2026-11-30 16:48:05 +01:00
fix: wait for CLI error output before exiting (#4263)
Co-authored-by: 梁镇贤 <251972984+xiaoyao-666-coder@users.noreply.github.com>
This commit is contained in:
+6
-5
@@ -298,11 +298,12 @@ export async function main(nodeProcess) {
|
||||
await start(nodeProcess.argv.slice(2));
|
||||
nodeProcess.exit(0);
|
||||
} catch(err) {
|
||||
if (err.code === 'ENOENT') {
|
||||
nodeProcess.stderr.write('marked: ' + err.path + ': No such file or directory');
|
||||
} else {
|
||||
nodeProcess.stderr.write(err.message);
|
||||
}
|
||||
const message = err.code === 'ENOENT'
|
||||
? 'marked: ' + err.path + ': No such file or directory'
|
||||
: err.message;
|
||||
await new Promise(resolve => {
|
||||
nodeProcess.stderr.write(message, resolve);
|
||||
});
|
||||
return nodeProcess.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
+25
-1
@@ -32,7 +32,10 @@ function createMocks() {
|
||||
}),
|
||||
},
|
||||
stderr: {
|
||||
write: mock.fn((str) => { mocks.stderr += str; }),
|
||||
write: mock.fn((str, callback) => {
|
||||
mocks.stderr += str;
|
||||
callback?.();
|
||||
}),
|
||||
},
|
||||
stdin: {
|
||||
setEncoding: mock.fn(),
|
||||
@@ -115,6 +118,27 @@ describe('bin/marked', () => {
|
||||
});
|
||||
|
||||
describe('output', () => {
|
||||
it('writes all error output before exiting when stderr is piped', async() => {
|
||||
const dir = await mkdtemp(resolve(tmpdir(), 'marked-stderr-'));
|
||||
try {
|
||||
const config = resolve(dir, 'config.cjs');
|
||||
const message = 'config error '.repeat(131072);
|
||||
await writeFile(config, `module.exports = () => { throw new Error(${JSON.stringify(message)}); };\n`);
|
||||
await assert.rejects(execFileAsync(process.execPath, [
|
||||
resolve(import.meta.dirname, '../../bin/marked.js'),
|
||||
'--config', config, '--string', '# test',
|
||||
], { maxBuffer: 4 * 1024 * 1024 }), (err) => {
|
||||
assert.strictEqual(err.code, 1);
|
||||
assert.strictEqual(err.stdout, '');
|
||||
assert.strictEqual(err.stderr.length, message.length);
|
||||
assert.strictEqual(err.stderr, message);
|
||||
return true;
|
||||
});
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it('writes all output before exiting when stdout is piped', async() => {
|
||||
const dir = await mkdtemp(resolve(tmpdir(), 'marked-stdout-'));
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user