fix(techdocs-cli): wait for child process to finish

Signed-off-by: Camila Belo <camilaibs@gmail.com>
This commit is contained in:
Camila Belo
2021-11-11 16:13:31 +01:00
parent 71e7b0ee92
commit 7a3c2f9301
3 changed files with 20 additions and 25 deletions
@@ -122,5 +122,10 @@ export default async function serve(cmd: Command) {
);
});
await waitForSignal([mkdocsChildProcess]);
try {
await waitForSignal([mkdocsChildProcess]);
process.exit(0);
} catch {
process.exit(1);
}
}
+4 -7
View File
@@ -56,13 +56,10 @@ describe('end-to-end', () => {
it('can serve in backstage', async () => {
jest.setTimeout(10000);
const proc = await executeTechDocsCliCommand(
['serve', '--no-docker', '--mkdocs-port=8888'],
{
cwd: FIXTURE_DIR,
killAfter: 8000,
},
);
const proc = await executeTechDocsCliCommand(['serve', '--no-docker'], {
cwd: FIXTURE_DIR,
killAfter: 8000,
});
expect(proc.combinedStdOutErr).toContain('Starting mkdocs server');
expect(proc.combinedStdOutErr).toContain('Serving docs in Backstage at');
+10 -17
View File
@@ -49,7 +49,6 @@ export const run = async (
const childProcess = spawn(name, args, {
stdio: stdio,
shell: true,
...options,
env,
});
@@ -72,6 +71,14 @@ export async function waitForSignal(
): Promise<void> {
const promises: Array<Promise<void>> = [];
for (const signal of ['SIGINT', 'SIGTERM'] as const) {
process.on(signal, () => {
childProcesses.forEach(childProcess => {
childProcess.kill();
});
});
}
childProcesses.forEach(childProcess => {
if (typeof childProcess.exitCode === 'number') {
if (childProcess.exitCode) {
@@ -80,24 +87,10 @@ export async function waitForSignal(
return;
}
for (const signal of ['SIGINT', 'SIGTERM'] as const) {
process.on(signal, () => {
childProcess.kill(signal);
// exit instead of resolve. The process is shutting down and resolving a promise here logs an error
process.exit();
});
}
promises.push(
new Promise<void>((resolve, reject) => {
childProcess.once('error', error => reject(error));
childProcess.once('exit', code => {
if (code) {
reject(new Error(`Non zero exit code from child process`));
} else {
resolve();
}
});
childProcess.once('error', reject);
childProcess.once('exit', resolve);
}),
);
});