refactor cli

This commit is contained in:
Michael Hohn
2025-03-15 18:02:25 -07:00
parent 29d6b127e2
commit dc745dd8e1

View File

@@ -537,20 +537,6 @@ export class CodeQLCliServer implements Disposable {
}
}
/**
* Spawns a child server process using the CodeQL CLI
* and attaches listeners to it.
*
* @param config The configuration containing the path to the CLI.
* @param name Name of the server being started, to be shown in log and error messages.
* @param command The `codeql` command to be run, provided as an array of command/subcommand names.
* @param commandArgs The arguments to pass to the `codeql` command.
* @param logger Logger to write startup messages.
* @param stderrListener Listener for log messages from the server's stderr stream.
* @param stdoutListener Optional listener for messages from the server's stdout stream.
* @param progressReporter Used to output progress messages, e.g. to the status bar.
* @returns The started child process.
*/
export function spawnServer(
codeqlPath: string,
name: string,
@@ -561,52 +547,36 @@ export function spawnServer(
stdoutListener?: (data: string | Buffer) => void,
progressReporter?: ProgressReporter,
): ChildProcessWithoutNullStreams {
// Enable verbose logging.
const args = command.concat(commandArgs).concat(LOGGING_FLAGS);
const args = [...command, ...commandArgs, ...LOGGING_FLAGS];
progressReporter?.report({ message: `Starting ${name}` });
// Start the server process.
const base = codeqlPath;
const argsString = args.join(" ");
if (progressReporter !== undefined) {
progressReporter.report({ message: `Starting ${name}` });
}
void logger.log(`Starting ${name} using CodeQL CLI: ${base} ${argsString}`);
const child = spawnChildProcess(base, args);
if (!child || !child.pid) {
logger.log(
`Starting ${name} using CodeQL CLI: ${codeqlPath} ${args.join(" ")}`,
);
const child = spawnChildProcess(codeqlPath, args);
if (!child?.pid) {
throw new Error(
`Failed to start ${name} using command ${base} ${argsString}.`,
`Failed to start ${name} using command ${codeqlPath} ${args.join(" ")}`,
);
}
let lastStdout: string | Buffer | undefined = undefined;
child.stdout!.on("data", (data) => {
lastStdout = data;
});
// Set up event listeners.
child.on("close", async (code, signal) => {
if (code !== null) {
void logger.log(`Child process exited with code ${code}`);
}
if (signal) {
void logger.log(
`Child process exited due to receipt of signal ${signal}`,
);
}
// If the process exited abnormally, log the last stdout message,
// It may be from the jvm.
if (code !== 0 && lastStdout !== undefined) {
void logger.log(`Last stdout was "${lastStdout.toString()}"`);
}
});
child.stderr!.on("data", stderrListener);
if (stdoutListener !== undefined) {
child.stdout!.on("data", stdoutListener);
}
let lastStdout: string | Buffer | undefined;
child.stdout!.on("data", (data) => (lastStdout = data));
child.on("close", (code, signal) => {
if (code !== null) logger.log(`Child process exited with code ${code}`);
if (signal) logger.log(`Child process exited due to signal ${signal}`);
if (code !== 0 && lastStdout)
logger.log(`Last stdout was "${lastStdout.toString()}"`);
});
child.stderr!.on("data", stderrListener);
stdoutListener && child.stdout!.on("data", stdoutListener);
progressReporter?.report({ message: `Started ${name}` });
logger.log(`${name} started on PID: ${child.pid}`);
if (progressReporter !== undefined) {
progressReporter.report({ message: `Started ${name}` });
}
void logger.log(`${name} started on PID: ${child.pid}`);
return child;
}