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

80
lib/codeql.js generated
View file

@ -313,7 +313,7 @@ function getCodeQLForCmd(cmd) {
return cmd;
},
async printVersion() {
await new toolrunner.ToolRunner(cmd, ["version", "--format=json"]).exec();
await runTool(cmd, ["version", "--format=json"]);
},
async getTracerEnv(databasePath) {
// Write tracer-env.js to a temp location.
@ -344,7 +344,7 @@ function getCodeQLForCmd(cmd) {
// action/runner has been implemented in `codeql database trace-command`
// _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,
@ -352,18 +352,18 @@ function getCodeQLForCmd(cmd) {
process.execPath,
tracerEnvJs,
envFile,
]).exec();
]);
return JSON.parse(fs.readFileSync(envFile, "utf-8"));
},
async databaseInit(databasePath, language, sourceRoot) {
await new toolrunner.ToolRunner(cmd, [
await runTool(cmd, [
"database",
"init",
databasePath,
`--language=${language}`,
`--source-root=${sourceRoot}`,
...getExtraOptionsFromEnv(["database", "init"]),
]).exec();
]);
},
async runAutobuild(language) {
const cmdName = process.platform === "win32" ? "autobuild.cmd" : "autobuild.sh";
@ -379,7 +379,7 @@ function getCodeQLForCmd(cmd) {
"-Dhttp.keepAlive=false",
"-Dmaven.wagon.http.pool=false",
].join(" ");
await new toolrunner.ToolRunner(autobuildCmd).exec();
await runTool(autobuildCmd);
},
async extractScannedLanguage(databasePath, language) {
// Get extractor location
@ -426,14 +426,7 @@ function getCodeQLForCmd(cmd) {
},
async resolveLanguages() {
const codeqlArgs = ["resolve", "languages", "--format=json"];
let output = "";
await new toolrunner.ToolRunner(cmd, codeqlArgs, {
listeners: {
stdout: (data) => {
output += data.toString();
},
},
}).exec();
const output = await runTool(cmd, codeqlArgs);
try {
return JSON.parse(output);
}
@ -452,14 +445,7 @@ function getCodeQLForCmd(cmd) {
if (extraSearchPath !== undefined) {
codeqlArgs.push("--additional-packs", extraSearchPath);
}
let output = "";
await new toolrunner.ToolRunner(cmd, codeqlArgs, {
listeners: {
stdout: (data) => {
output += data.toString();
},
},
}).exec();
const output = await runTool(cmd, codeqlArgs);
try {
return JSON.parse(output);
}
@ -468,7 +454,7 @@ function getCodeQLForCmd(cmd) {
}
},
async databaseRunQueries(databasePath, extraSearchPath, querySuitePath, memoryFlag, threadsFlag) {
const args = [
const codeqlArgs = [
"database",
"run-queries",
memoryFlag,
@ -479,13 +465,13 @@ function getCodeQLForCmd(cmd) {
...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, querySuitePaths, sarifFile, addSnippetsFlag, threadsFlag, automationDetailsId) {
const args = [
const codeqlArgs = [
"database",
"interpret-results",
threadsFlag,
@ -498,19 +484,11 @@ function getCodeQLForCmd(cmd) {
...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) => {
output += data.toString();
},
},
}).exec();
return output;
return await runTool(cmd, codeqlArgs);
},
/**
* Download specified packs into the package cache. If the specified
@ -523,21 +501,14 @@ function getCodeQLForCmd(cmd) {
* each time this package is requested.
*/
async packDownload(packs) {
const args = [
const codeqlArgs = [
"pack",
"download",
"--format=json",
...getExtraOptionsFromEnv(["pack", "download"]),
...packs.map(packWithVersionToString),
];
let output = "";
await new toolrunner.ToolRunner(cmd, args, {
listeners: {
stdout: (data) => {
output += data.toString("utf8");
},
},
}).exec();
const output = await runTool(cmd, codeqlArgs);
try {
const parsedOutput = JSON.parse(output);
if (Array.isArray(parsedOutput.packs) &&
@ -556,13 +527,13 @@ function getCodeQLForCmd(cmd) {
}
},
async databaseCleanup(databasePath, cleanupLevel) {
const args = [
const codeqlArgs = [
"database",
"cleanup",
databasePath,
`--mode=${cleanupLevel}`,
];
await new toolrunner.ToolRunner(cmd, args).exec();
await runTool(cmd, codeqlArgs);
},
};
}
@ -615,4 +586,15 @@ function getExtraOptions(options, paths, pathInfo) {
return all.concat(specific);
}
exports.getExtraOptions = getExtraOptions;
async function runTool(cmd, args = []) {
let output = "";
await new toolrunner.ToolRunner(cmd, args, {
listeners: {
stdout: (data) => {
output += data.toString();
},
},
}).exec();
return output;
}
//# sourceMappingURL=codeql.js.map