fix(cli): wait for stdout before exiting (#4197)

Co-authored-by: pandaxiong0729 <239575197+pandaxiong0729@users.noreply.github.com>
This commit is contained in:
Panda Xiong
2026-11-16 09:25:08 -07:00
committed by GitHub
co-authored by pandaxiong0729
parent ffa06d52ce
commit b535046bf5
2 changed files with 31 additions and 2 deletions
+9 -1
View File
@@ -237,7 +237,15 @@ export async function main(nodeProcess) {
}
}
nodeProcess.stdout.write(html + '\n');
await new Promise((resolve, reject) => {
nodeProcess.stdout.write(html + '\n', (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
/**
+22 -1
View File
@@ -26,7 +26,10 @@ function createMocks() {
env: [],
argv: [],
stdout: {
write: mock.fn((str) => { mocks.stdout += str; }),
write: mock.fn((str, callback) => {
mocks.stdout += str;
callback?.();
}),
},
stderr: {
write: mock.fn((str) => { mocks.stderr += str; }),
@@ -101,6 +104,24 @@ function execMarked({ args = '', stdin = '', stdout = '', stderr = '' }) {
describe('bin/marked', () => {
describe('output', () => {
it('writes all output before exiting when stdout is piped', async() => {
const dir = await mkdtemp(resolve(tmpdir(), 'marked-stdout-'));
try {
const input = resolve(dir, 'input.md');
const markdown = 'markdown output '.repeat(131072);
await writeFile(input, markdown);
const { stdout, stderr } = await execFileAsync(process.execPath, [
resolve(import.meta.dirname, '../../bin/marked.js'),
'--input', input,
], { maxBuffer: 4 * 1024 * 1024 });
assert.strictEqual(stdout, `<p>${markdown}</p>\n\n`);
assert.strictEqual(stderr, '');
} finally {
await rm(dir, { recursive: true, force: true });
}
});
it('does not follow an existing dangling symlink with --no-clobber', { skip: process.platform === 'win32' }, async() => {
const dir = await mkdtemp(resolve(tmpdir(), 'marked-no-clobber-'));
try {