Refactor CLI JSON handling into a dedicated runCliJson function

This commit is contained in:
Mario Campos
2026-06-17 16:38:10 -05:00
parent 889ae42672
commit dc8e1e9aa0
+29 -37
View File
@@ -512,18 +512,17 @@ async function getCodeQLForCmd(
return cmd; return cmd;
}, },
async getVersion() { async getVersion() {
async function runCliVersion() {
return await runCliJson<VersionInfo>(
cmd,
["version", "--format=json"],
{ noStreamStdout: true },
);
}
let result = util.getCachedCodeQlVersion(cmd); let result = util.getCachedCodeQlVersion(cmd);
if (result === undefined) { if (result === undefined) {
const output = await runCli(cmd, ["version", "--format=json"], { result = await runCliVersion();
noStreamStdout: true,
});
try {
result = JSON.parse(output) as VersionInfo;
} catch {
throw Error(
`Invalid JSON output from \`version --format=json\`: ${output}`,
);
}
util.cacheCodeQlVersion(cmd, result); util.cacheCodeQlVersion(cmd, result);
} }
return result; return result;
@@ -774,15 +773,7 @@ async function getCodeQLForCmd(
if (workingDir !== undefined) { if (workingDir !== undefined) {
codeqlArgs.push("--working-dir", workingDir); codeqlArgs.push("--working-dir", workingDir);
} }
const output = await runCli(cmd, codeqlArgs); return await runCliJson<ResolveBuildEnvironmentOutput>(cmd, codeqlArgs);
try {
return JSON.parse(output) as ResolveBuildEnvironmentOutput;
} catch (e) {
throw new Error(
`Unexpected output from codeql resolve build-environment: ${e} in\n${output}`,
);
}
}, },
async databaseRunQueries( async databaseRunQueries(
databasePath: string, databasePath: string,
@@ -963,15 +954,9 @@ async function getCodeQLForCmd(
...getExtraOptionsFromEnv(["resolve", "queries"]), ...getExtraOptionsFromEnv(["resolve", "queries"]),
...queries, ...queries,
]; ];
const output = await runCli(cmd, codeqlArgs, { noStreamStdout: true }); return await runCliJson<string[]>(cmd, codeqlArgs, {
noStreamStdout: true,
try { });
return JSON.parse(output) as string[];
} catch (e) {
throw new Error(
`Unexpected output from codeql resolve queries --format=startingpacks: ${e}`,
);
}
}, },
async resolveDatabase( async resolveDatabase(
databasePath: string, databasePath: string,
@@ -983,15 +968,9 @@ async function getCodeQLForCmd(
"--format=json", "--format=json",
...getExtraOptionsFromEnv(["resolve", "database"]), ...getExtraOptionsFromEnv(["resolve", "database"]),
]; ];
const output = await runCli(cmd, codeqlArgs, { noStreamStdout: true }); return await runCliJson<ResolveDatabaseOutput>(cmd, codeqlArgs, {
noStreamStdout: true,
try { });
return JSON.parse(output) as ResolveDatabaseOutput;
} catch (e) {
throw new Error(
`Unexpected output from codeql resolve database --format=json: ${e}`,
);
}
}, },
async mergeResults( async mergeResults(
sarifFiles: string[], sarifFiles: string[],
@@ -1147,6 +1126,19 @@ async function runCli(
} }
} }
async function runCliJson<T>(
cmd: string,
args: string[] = [],
opts: { stdin?: string; noStreamStdout?: boolean } = {},
): Promise<T> {
const output = await runCli(cmd, args, opts);
try {
return JSON.parse(output) as T;
} catch {
throw Error(`Invalid JSON output from \`${args.join(" ")}\`: ${output}`);
}
}
/** /**
* Writes the code scanning configuration that is to be used by the CLI. * Writes the code scanning configuration that is to be used by the CLI.
* *