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:
parent
db01c78de0
commit
49b2220f92
3 changed files with 64 additions and 99 deletions
80
lib/codeql.js
generated
80
lib/codeql.js
generated
|
|
@ -313,7 +313,7 @@ function getCodeQLForCmd(cmd) {
|
||||||
return cmd;
|
return cmd;
|
||||||
},
|
},
|
||||||
async printVersion() {
|
async printVersion() {
|
||||||
await new toolrunner.ToolRunner(cmd, ["version", "--format=json"]).exec();
|
await runTool(cmd, ["version", "--format=json"]);
|
||||||
},
|
},
|
||||||
async getTracerEnv(databasePath) {
|
async getTracerEnv(databasePath) {
|
||||||
// Write tracer-env.js to a temp location.
|
// 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`
|
// action/runner has been implemented in `codeql database trace-command`
|
||||||
// _and_ is present in the latest supported CLI release.)
|
// _and_ is present in the latest supported CLI release.)
|
||||||
const envFile = path.resolve(databasePath, "working", "env.tmp");
|
const envFile = path.resolve(databasePath, "working", "env.tmp");
|
||||||
await new toolrunner.ToolRunner(cmd, [
|
await runTool(cmd, [
|
||||||
"database",
|
"database",
|
||||||
"trace-command",
|
"trace-command",
|
||||||
databasePath,
|
databasePath,
|
||||||
|
|
@ -352,18 +352,18 @@ function getCodeQLForCmd(cmd) {
|
||||||
process.execPath,
|
process.execPath,
|
||||||
tracerEnvJs,
|
tracerEnvJs,
|
||||||
envFile,
|
envFile,
|
||||||
]).exec();
|
]);
|
||||||
return JSON.parse(fs.readFileSync(envFile, "utf-8"));
|
return JSON.parse(fs.readFileSync(envFile, "utf-8"));
|
||||||
},
|
},
|
||||||
async databaseInit(databasePath, language, sourceRoot) {
|
async databaseInit(databasePath, language, sourceRoot) {
|
||||||
await new toolrunner.ToolRunner(cmd, [
|
await runTool(cmd, [
|
||||||
"database",
|
"database",
|
||||||
"init",
|
"init",
|
||||||
databasePath,
|
databasePath,
|
||||||
`--language=${language}`,
|
`--language=${language}`,
|
||||||
`--source-root=${sourceRoot}`,
|
`--source-root=${sourceRoot}`,
|
||||||
...getExtraOptionsFromEnv(["database", "init"]),
|
...getExtraOptionsFromEnv(["database", "init"]),
|
||||||
]).exec();
|
]);
|
||||||
},
|
},
|
||||||
async runAutobuild(language) {
|
async runAutobuild(language) {
|
||||||
const cmdName = process.platform === "win32" ? "autobuild.cmd" : "autobuild.sh";
|
const cmdName = process.platform === "win32" ? "autobuild.cmd" : "autobuild.sh";
|
||||||
|
|
@ -379,7 +379,7 @@ function getCodeQLForCmd(cmd) {
|
||||||
"-Dhttp.keepAlive=false",
|
"-Dhttp.keepAlive=false",
|
||||||
"-Dmaven.wagon.http.pool=false",
|
"-Dmaven.wagon.http.pool=false",
|
||||||
].join(" ");
|
].join(" ");
|
||||||
await new toolrunner.ToolRunner(autobuildCmd).exec();
|
await runTool(autobuildCmd);
|
||||||
},
|
},
|
||||||
async extractScannedLanguage(databasePath, language) {
|
async extractScannedLanguage(databasePath, language) {
|
||||||
// Get extractor location
|
// Get extractor location
|
||||||
|
|
@ -426,14 +426,7 @@ function getCodeQLForCmd(cmd) {
|
||||||
},
|
},
|
||||||
async resolveLanguages() {
|
async resolveLanguages() {
|
||||||
const codeqlArgs = ["resolve", "languages", "--format=json"];
|
const codeqlArgs = ["resolve", "languages", "--format=json"];
|
||||||
let output = "";
|
const output = await runTool(cmd, codeqlArgs);
|
||||||
await new toolrunner.ToolRunner(cmd, codeqlArgs, {
|
|
||||||
listeners: {
|
|
||||||
stdout: (data) => {
|
|
||||||
output += data.toString();
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}).exec();
|
|
||||||
try {
|
try {
|
||||||
return JSON.parse(output);
|
return JSON.parse(output);
|
||||||
}
|
}
|
||||||
|
|
@ -452,14 +445,7 @@ function getCodeQLForCmd(cmd) {
|
||||||
if (extraSearchPath !== undefined) {
|
if (extraSearchPath !== undefined) {
|
||||||
codeqlArgs.push("--additional-packs", extraSearchPath);
|
codeqlArgs.push("--additional-packs", extraSearchPath);
|
||||||
}
|
}
|
||||||
let output = "";
|
const output = await runTool(cmd, codeqlArgs);
|
||||||
await new toolrunner.ToolRunner(cmd, codeqlArgs, {
|
|
||||||
listeners: {
|
|
||||||
stdout: (data) => {
|
|
||||||
output += data.toString();
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}).exec();
|
|
||||||
try {
|
try {
|
||||||
return JSON.parse(output);
|
return JSON.parse(output);
|
||||||
}
|
}
|
||||||
|
|
@ -468,7 +454,7 @@ function getCodeQLForCmd(cmd) {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async databaseRunQueries(databasePath, extraSearchPath, querySuitePath, memoryFlag, threadsFlag) {
|
async databaseRunQueries(databasePath, extraSearchPath, querySuitePath, memoryFlag, threadsFlag) {
|
||||||
const args = [
|
const codeqlArgs = [
|
||||||
"database",
|
"database",
|
||||||
"run-queries",
|
"run-queries",
|
||||||
memoryFlag,
|
memoryFlag,
|
||||||
|
|
@ -479,13 +465,13 @@ function getCodeQLForCmd(cmd) {
|
||||||
...getExtraOptionsFromEnv(["database", "run-queries"]),
|
...getExtraOptionsFromEnv(["database", "run-queries"]),
|
||||||
];
|
];
|
||||||
if (extraSearchPath !== undefined) {
|
if (extraSearchPath !== undefined) {
|
||||||
args.push("--additional-packs", extraSearchPath);
|
codeqlArgs.push("--additional-packs", extraSearchPath);
|
||||||
}
|
}
|
||||||
args.push(querySuitePath);
|
codeqlArgs.push(querySuitePath);
|
||||||
await new toolrunner.ToolRunner(cmd, args).exec();
|
await runTool(cmd, codeqlArgs);
|
||||||
},
|
},
|
||||||
async databaseInterpretResults(databasePath, querySuitePaths, sarifFile, addSnippetsFlag, threadsFlag, automationDetailsId) {
|
async databaseInterpretResults(databasePath, querySuitePaths, sarifFile, addSnippetsFlag, threadsFlag, automationDetailsId) {
|
||||||
const args = [
|
const codeqlArgs = [
|
||||||
"database",
|
"database",
|
||||||
"interpret-results",
|
"interpret-results",
|
||||||
threadsFlag,
|
threadsFlag,
|
||||||
|
|
@ -498,19 +484,11 @@ function getCodeQLForCmd(cmd) {
|
||||||
...getExtraOptionsFromEnv(["database", "interpret-results"]),
|
...getExtraOptionsFromEnv(["database", "interpret-results"]),
|
||||||
];
|
];
|
||||||
if (automationDetailsId !== undefined) {
|
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
|
// capture stdout, which contains analysis summaries
|
||||||
let output = "";
|
return await runTool(cmd, codeqlArgs);
|
||||||
await new toolrunner.ToolRunner(cmd, args, {
|
|
||||||
listeners: {
|
|
||||||
stdout: (data) => {
|
|
||||||
output += data.toString();
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}).exec();
|
|
||||||
return output;
|
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
* Download specified packs into the package cache. If the specified
|
* Download specified packs into the package cache. If the specified
|
||||||
|
|
@ -523,21 +501,14 @@ function getCodeQLForCmd(cmd) {
|
||||||
* each time this package is requested.
|
* each time this package is requested.
|
||||||
*/
|
*/
|
||||||
async packDownload(packs) {
|
async packDownload(packs) {
|
||||||
const args = [
|
const codeqlArgs = [
|
||||||
"pack",
|
"pack",
|
||||||
"download",
|
"download",
|
||||||
"--format=json",
|
"--format=json",
|
||||||
...getExtraOptionsFromEnv(["pack", "download"]),
|
...getExtraOptionsFromEnv(["pack", "download"]),
|
||||||
...packs.map(packWithVersionToString),
|
...packs.map(packWithVersionToString),
|
||||||
];
|
];
|
||||||
let output = "";
|
const output = await runTool(cmd, codeqlArgs);
|
||||||
await new toolrunner.ToolRunner(cmd, args, {
|
|
||||||
listeners: {
|
|
||||||
stdout: (data) => {
|
|
||||||
output += data.toString("utf8");
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}).exec();
|
|
||||||
try {
|
try {
|
||||||
const parsedOutput = JSON.parse(output);
|
const parsedOutput = JSON.parse(output);
|
||||||
if (Array.isArray(parsedOutput.packs) &&
|
if (Array.isArray(parsedOutput.packs) &&
|
||||||
|
|
@ -556,13 +527,13 @@ function getCodeQLForCmd(cmd) {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async databaseCleanup(databasePath, cleanupLevel) {
|
async databaseCleanup(databasePath, cleanupLevel) {
|
||||||
const args = [
|
const codeqlArgs = [
|
||||||
"database",
|
"database",
|
||||||
"cleanup",
|
"cleanup",
|
||||||
databasePath,
|
databasePath,
|
||||||
`--mode=${cleanupLevel}`,
|
`--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);
|
return all.concat(specific);
|
||||||
}
|
}
|
||||||
exports.getExtraOptions = getExtraOptions;
|
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
|
//# sourceMappingURL=codeql.js.map
|
||||||
File diff suppressed because one or more lines are too long
|
|
@ -541,7 +541,7 @@ function getCodeQLForCmd(cmd: string): CodeQL {
|
||||||
return cmd;
|
return cmd;
|
||||||
},
|
},
|
||||||
async printVersion() {
|
async printVersion() {
|
||||||
await new toolrunner.ToolRunner(cmd, ["version", "--format=json"]).exec();
|
await runTool(cmd, ["version", "--format=json"]);
|
||||||
},
|
},
|
||||||
async getTracerEnv(databasePath: string) {
|
async getTracerEnv(databasePath: string) {
|
||||||
// Write tracer-env.js to a temp location.
|
// 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.)
|
// _and_ is present in the latest supported CLI release.)
|
||||||
const envFile = path.resolve(databasePath, "working", "env.tmp");
|
const envFile = path.resolve(databasePath, "working", "env.tmp");
|
||||||
|
|
||||||
await new toolrunner.ToolRunner(cmd, [
|
await runTool(cmd, [
|
||||||
"database",
|
"database",
|
||||||
"trace-command",
|
"trace-command",
|
||||||
databasePath,
|
databasePath,
|
||||||
|
|
@ -590,7 +590,7 @@ function getCodeQLForCmd(cmd: string): CodeQL {
|
||||||
process.execPath,
|
process.execPath,
|
||||||
tracerEnvJs,
|
tracerEnvJs,
|
||||||
envFile,
|
envFile,
|
||||||
]).exec();
|
]);
|
||||||
return JSON.parse(fs.readFileSync(envFile, "utf-8"));
|
return JSON.parse(fs.readFileSync(envFile, "utf-8"));
|
||||||
},
|
},
|
||||||
async databaseInit(
|
async databaseInit(
|
||||||
|
|
@ -598,14 +598,14 @@ function getCodeQLForCmd(cmd: string): CodeQL {
|
||||||
language: Language,
|
language: Language,
|
||||||
sourceRoot: string
|
sourceRoot: string
|
||||||
) {
|
) {
|
||||||
await new toolrunner.ToolRunner(cmd, [
|
await runTool(cmd, [
|
||||||
"database",
|
"database",
|
||||||
"init",
|
"init",
|
||||||
databasePath,
|
databasePath,
|
||||||
`--language=${language}`,
|
`--language=${language}`,
|
||||||
`--source-root=${sourceRoot}`,
|
`--source-root=${sourceRoot}`,
|
||||||
...getExtraOptionsFromEnv(["database", "init"]),
|
...getExtraOptionsFromEnv(["database", "init"]),
|
||||||
]).exec();
|
]);
|
||||||
},
|
},
|
||||||
async runAutobuild(language: Language) {
|
async runAutobuild(language: Language) {
|
||||||
const cmdName =
|
const cmdName =
|
||||||
|
|
@ -629,7 +629,7 @@ function getCodeQLForCmd(cmd: string): CodeQL {
|
||||||
"-Dmaven.wagon.http.pool=false",
|
"-Dmaven.wagon.http.pool=false",
|
||||||
].join(" ");
|
].join(" ");
|
||||||
|
|
||||||
await new toolrunner.ToolRunner(autobuildCmd).exec();
|
await runTool(autobuildCmd);
|
||||||
},
|
},
|
||||||
async extractScannedLanguage(databasePath: string, language: Language) {
|
async extractScannedLanguage(databasePath: string, language: Language) {
|
||||||
// Get extractor location
|
// Get extractor location
|
||||||
|
|
@ -694,14 +694,7 @@ function getCodeQLForCmd(cmd: string): CodeQL {
|
||||||
},
|
},
|
||||||
async resolveLanguages() {
|
async resolveLanguages() {
|
||||||
const codeqlArgs = ["resolve", "languages", "--format=json"];
|
const codeqlArgs = ["resolve", "languages", "--format=json"];
|
||||||
let output = "";
|
const output = await runTool(cmd, codeqlArgs);
|
||||||
await new toolrunner.ToolRunner(cmd, codeqlArgs, {
|
|
||||||
listeners: {
|
|
||||||
stdout: (data: Buffer) => {
|
|
||||||
output += data.toString();
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}).exec();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return JSON.parse(output);
|
return JSON.parse(output);
|
||||||
|
|
@ -725,14 +718,7 @@ function getCodeQLForCmd(cmd: string): CodeQL {
|
||||||
if (extraSearchPath !== undefined) {
|
if (extraSearchPath !== undefined) {
|
||||||
codeqlArgs.push("--additional-packs", extraSearchPath);
|
codeqlArgs.push("--additional-packs", extraSearchPath);
|
||||||
}
|
}
|
||||||
let output = "";
|
const output = await runTool(cmd, codeqlArgs);
|
||||||
await new toolrunner.ToolRunner(cmd, codeqlArgs, {
|
|
||||||
listeners: {
|
|
||||||
stdout: (data: Buffer) => {
|
|
||||||
output += data.toString();
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}).exec();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return JSON.parse(output);
|
return JSON.parse(output);
|
||||||
|
|
@ -747,7 +733,7 @@ function getCodeQLForCmd(cmd: string): CodeQL {
|
||||||
memoryFlag: string,
|
memoryFlag: string,
|
||||||
threadsFlag: string
|
threadsFlag: string
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const args = [
|
const codeqlArgs = [
|
||||||
"database",
|
"database",
|
||||||
"run-queries",
|
"run-queries",
|
||||||
memoryFlag,
|
memoryFlag,
|
||||||
|
|
@ -758,10 +744,10 @@ function getCodeQLForCmd(cmd: string): CodeQL {
|
||||||
...getExtraOptionsFromEnv(["database", "run-queries"]),
|
...getExtraOptionsFromEnv(["database", "run-queries"]),
|
||||||
];
|
];
|
||||||
if (extraSearchPath !== undefined) {
|
if (extraSearchPath !== undefined) {
|
||||||
args.push("--additional-packs", extraSearchPath);
|
codeqlArgs.push("--additional-packs", extraSearchPath);
|
||||||
}
|
}
|
||||||
args.push(querySuitePath);
|
codeqlArgs.push(querySuitePath);
|
||||||
await new toolrunner.ToolRunner(cmd, args).exec();
|
await runTool(cmd, codeqlArgs);
|
||||||
},
|
},
|
||||||
async databaseInterpretResults(
|
async databaseInterpretResults(
|
||||||
databasePath: string,
|
databasePath: string,
|
||||||
|
|
@ -771,7 +757,7 @@ function getCodeQLForCmd(cmd: string): CodeQL {
|
||||||
threadsFlag: string,
|
threadsFlag: string,
|
||||||
automationDetailsId: string | undefined
|
automationDetailsId: string | undefined
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const args = [
|
const codeqlArgs = [
|
||||||
"database",
|
"database",
|
||||||
"interpret-results",
|
"interpret-results",
|
||||||
threadsFlag,
|
threadsFlag,
|
||||||
|
|
@ -784,19 +770,11 @@ function getCodeQLForCmd(cmd: string): CodeQL {
|
||||||
...getExtraOptionsFromEnv(["database", "interpret-results"]),
|
...getExtraOptionsFromEnv(["database", "interpret-results"]),
|
||||||
];
|
];
|
||||||
if (automationDetailsId !== undefined) {
|
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
|
// capture stdout, which contains analysis summaries
|
||||||
let output = "";
|
return await runTool(cmd, codeqlArgs);
|
||||||
await new toolrunner.ToolRunner(cmd, args, {
|
|
||||||
listeners: {
|
|
||||||
stdout: (data: Buffer) => {
|
|
||||||
output += data.toString();
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}).exec();
|
|
||||||
return output;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -810,7 +788,7 @@ function getCodeQLForCmd(cmd: string): CodeQL {
|
||||||
* each time this package is requested.
|
* each time this package is requested.
|
||||||
*/
|
*/
|
||||||
async packDownload(packs: PackWithVersion[]): Promise<PackDownloadOutput> {
|
async packDownload(packs: PackWithVersion[]): Promise<PackDownloadOutput> {
|
||||||
const args = [
|
const codeqlArgs = [
|
||||||
"pack",
|
"pack",
|
||||||
"download",
|
"download",
|
||||||
"--format=json",
|
"--format=json",
|
||||||
|
|
@ -818,14 +796,7 @@ function getCodeQLForCmd(cmd: string): CodeQL {
|
||||||
...packs.map(packWithVersionToString),
|
...packs.map(packWithVersionToString),
|
||||||
];
|
];
|
||||||
|
|
||||||
let output = "";
|
const output = await runTool(cmd, codeqlArgs);
|
||||||
await new toolrunner.ToolRunner(cmd, args, {
|
|
||||||
listeners: {
|
|
||||||
stdout: (data: Buffer) => {
|
|
||||||
output += data.toString("utf8");
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}).exec();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const parsedOutput: PackDownloadOutput = JSON.parse(output);
|
const parsedOutput: PackDownloadOutput = JSON.parse(output);
|
||||||
|
|
@ -850,13 +821,13 @@ function getCodeQLForCmd(cmd: string): CodeQL {
|
||||||
databasePath: string,
|
databasePath: string,
|
||||||
cleanupLevel: string
|
cleanupLevel: string
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const args = [
|
const codeqlArgs = [
|
||||||
"database",
|
"database",
|
||||||
"cleanup",
|
"cleanup",
|
||||||
databasePath,
|
databasePath,
|
||||||
`--mode=${cleanupLevel}`,
|
`--mode=${cleanupLevel}`,
|
||||||
];
|
];
|
||||||
await new toolrunner.ToolRunner(cmd, args).exec();
|
await runTool(cmd, codeqlArgs);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
@ -923,3 +894,15 @@ export function getExtraOptions(
|
||||||
);
|
);
|
||||||
return all.concat(specific);
|
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;
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue