Refactor codeql.ts

Extract a `runTool` function that captures the stdout and returns it.
A small refactoring that reduces copied code.
This commit is contained in:
Andrew Eisenberg 2021-06-09 13:17:25 -07:00
parent db01c78de0
commit 49b2220f92
3 changed files with 64 additions and 99 deletions

View file

@ -541,7 +541,7 @@ function getCodeQLForCmd(cmd: string): CodeQL {
return cmd;
},
async printVersion() {
await new toolrunner.ToolRunner(cmd, ["version", "--format=json"]).exec();
await runTool(cmd, ["version", "--format=json"]);
},
async getTracerEnv(databasePath: string) {
// Write tracer-env.js to a temp location.
@ -582,7 +582,7 @@ function getCodeQLForCmd(cmd: string): CodeQL {
// _and_ is present in the latest supported CLI release.)
const envFile = path.resolve(databasePath, "working", "env.tmp");
await new toolrunner.ToolRunner(cmd, [
await runTool(cmd, [
"database",
"trace-command",
databasePath,
@ -590,7 +590,7 @@ function getCodeQLForCmd(cmd: string): CodeQL {
process.execPath,
tracerEnvJs,
envFile,
]).exec();
]);
return JSON.parse(fs.readFileSync(envFile, "utf-8"));
},
async databaseInit(
@ -598,14 +598,14 @@ function getCodeQLForCmd(cmd: string): CodeQL {
language: Language,
sourceRoot: string
) {
await new toolrunner.ToolRunner(cmd, [
await runTool(cmd, [
"database",
"init",
databasePath,
`--language=${language}`,
`--source-root=${sourceRoot}`,
...getExtraOptionsFromEnv(["database", "init"]),
]).exec();
]);
},
async runAutobuild(language: Language) {
const cmdName =
@ -629,7 +629,7 @@ function getCodeQLForCmd(cmd: string): CodeQL {
"-Dmaven.wagon.http.pool=false",
].join(" ");
await new toolrunner.ToolRunner(autobuildCmd).exec();
await runTool(autobuildCmd);
},
async extractScannedLanguage(databasePath: string, language: Language) {
// Get extractor location
@ -694,14 +694,7 @@ function getCodeQLForCmd(cmd: string): CodeQL {
},
async resolveLanguages() {
const codeqlArgs = ["resolve", "languages", "--format=json"];
let output = "";
await new toolrunner.ToolRunner(cmd, codeqlArgs, {
listeners: {
stdout: (data: Buffer) => {
output += data.toString();
},
},
}).exec();
const output = await runTool(cmd, codeqlArgs);
try {
return JSON.parse(output);
@ -725,14 +718,7 @@ function getCodeQLForCmd(cmd: string): CodeQL {
if (extraSearchPath !== undefined) {
codeqlArgs.push("--additional-packs", extraSearchPath);
}
let output = "";
await new toolrunner.ToolRunner(cmd, codeqlArgs, {
listeners: {
stdout: (data: Buffer) => {
output += data.toString();
},
},
}).exec();
const output = await runTool(cmd, codeqlArgs);
try {
return JSON.parse(output);
@ -747,7 +733,7 @@ function getCodeQLForCmd(cmd: string): CodeQL {
memoryFlag: string,
threadsFlag: string
): Promise<void> {
const args = [
const codeqlArgs = [
"database",
"run-queries",
memoryFlag,
@ -758,10 +744,10 @@ function getCodeQLForCmd(cmd: string): CodeQL {
...getExtraOptionsFromEnv(["database", "run-queries"]),
];
if (extraSearchPath !== undefined) {
args.push("--additional-packs", extraSearchPath);
codeqlArgs.push("--additional-packs", extraSearchPath);
}
args.push(querySuitePath);
await new toolrunner.ToolRunner(cmd, args).exec();
codeqlArgs.push(querySuitePath);
await runTool(cmd, codeqlArgs);
},
async databaseInterpretResults(
databasePath: string,
@ -771,7 +757,7 @@ function getCodeQLForCmd(cmd: string): CodeQL {
threadsFlag: string,
automationDetailsId: string | undefined
): Promise<string> {
const args = [
const codeqlArgs = [
"database",
"interpret-results",
threadsFlag,
@ -784,19 +770,11 @@ function getCodeQLForCmd(cmd: string): CodeQL {
...getExtraOptionsFromEnv(["database", "interpret-results"]),
];
if (automationDetailsId !== undefined) {
args.push("--sarif-category", automationDetailsId);
codeqlArgs.push("--sarif-category", automationDetailsId);
}
args.push(databasePath, ...querySuitePaths);
codeqlArgs.push(databasePath, ...querySuitePaths);
// capture stdout, which contains analysis summaries
let output = "";
await new toolrunner.ToolRunner(cmd, args, {
listeners: {
stdout: (data: Buffer) => {
output += data.toString();
},
},
}).exec();
return output;
return await runTool(cmd, codeqlArgs);
},
/**
@ -810,7 +788,7 @@ function getCodeQLForCmd(cmd: string): CodeQL {
* each time this package is requested.
*/
async packDownload(packs: PackWithVersion[]): Promise<PackDownloadOutput> {
const args = [
const codeqlArgs = [
"pack",
"download",
"--format=json",
@ -818,14 +796,7 @@ function getCodeQLForCmd(cmd: string): CodeQL {
...packs.map(packWithVersionToString),
];
let output = "";
await new toolrunner.ToolRunner(cmd, args, {
listeners: {
stdout: (data: Buffer) => {
output += data.toString("utf8");
},
},
}).exec();
const output = await runTool(cmd, codeqlArgs);
try {
const parsedOutput: PackDownloadOutput = JSON.parse(output);
@ -850,13 +821,13 @@ function getCodeQLForCmd(cmd: string): CodeQL {
databasePath: string,
cleanupLevel: string
): Promise<void> {
const args = [
const codeqlArgs = [
"database",
"cleanup",
databasePath,
`--mode=${cleanupLevel}`,
];
await new toolrunner.ToolRunner(cmd, args).exec();
await runTool(cmd, codeqlArgs);
},
};
}
@ -923,3 +894,15 @@ export function getExtraOptions(
);
return all.concat(specific);
}
async function runTool(cmd: string, args: string[] = []) {
let output = "";
await new toolrunner.ToolRunner(cmd, args, {
listeners: {
stdout: (data: Buffer) => {
output += data.toString();
},
},
}).exec();
return output;
}