Skip to content

Commit c1e7c7a

Browse files
authored
Merge pull request #3981 from github/mario-campos/runCliJson
Create wrapper `runCliJson`
2 parents 50bd53b + d5b8046 commit c1e7c7a

2 files changed

Lines changed: 72 additions & 93 deletions

File tree

lib/entry-points.js

Lines changed: 29 additions & 45 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/codeql.ts

Lines changed: 43 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -514,16 +514,13 @@ async function getCodeQLForCmd(
514514
async getVersion() {
515515
let result = util.getCachedCodeQlVersion(cmd);
516516
if (result === undefined) {
517-
const output = await runCli(cmd, ["version", "--format=json"], {
518-
noStreamStdout: true,
519-
});
520-
try {
521-
result = JSON.parse(output) as VersionInfo;
522-
} catch {
523-
throw Error(
524-
`Invalid JSON output from \`version --format=json\`: ${output}`,
525-
);
526-
}
517+
result = await runCliJson<VersionInfo>(
518+
cmd,
519+
["version", "--format=json"],
520+
{
521+
noStreamStdout: true,
522+
},
523+
);
527524
util.cacheCodeQlVersion(cmd, result);
528525
}
529526
return result;
@@ -731,26 +728,20 @@ async function getCodeQLForCmd(
731728
filterToLanguagesWithQueries: boolean;
732729
} = { filterToLanguagesWithQueries: false },
733730
) {
734-
const codeqlArgs = [
731+
return runCliJson<ResolveLanguagesOutput>(cmd, [
735732
"resolve",
736733
"languages",
737734
"--format=betterjson",
738735
"--extractor-options-verbosity=4",
739736
"--extractor-include-aliases",
737+
// TODO: Unconditionally include `--filter-to-languages-with-queries`
738+
// once CODEQL_MINIMUM_VERSION is at least v2.23.0
739+
// — the first version to support this flag.
740740
...(filterToLanguagesWithQueries
741741
? ["--filter-to-languages-with-queries"]
742742
: []),
743743
...getExtraOptionsFromEnv(["resolve", "languages"]),
744-
];
745-
const output = await runCli(cmd, codeqlArgs);
746-
747-
try {
748-
return JSON.parse(output) as ResolveLanguagesOutput;
749-
} catch (e) {
750-
throw new Error(
751-
`Unexpected output from codeql resolve languages with --format=betterjson: ${e}`,
752-
);
753-
}
744+
]);
754745
},
755746
async resolveBuildEnvironment(
756747
workingDir: string | undefined,
@@ -766,15 +757,7 @@ async function getCodeQLForCmd(
766757
if (workingDir !== undefined) {
767758
codeqlArgs.push("--working-dir", workingDir);
768759
}
769-
const output = await runCli(cmd, codeqlArgs);
770-
771-
try {
772-
return JSON.parse(output) as ResolveBuildEnvironmentOutput;
773-
} catch (e) {
774-
throw new Error(
775-
`Unexpected output from codeql resolve build-environment: ${e} in\n${output}`,
776-
);
777-
}
760+
return await runCliJson<ResolveBuildEnvironmentOutput>(cmd, codeqlArgs);
778761
},
779762
async databaseRunQueries(
780763
databasePath: string,
@@ -976,15 +959,9 @@ async function getCodeQLForCmd(
976959
...getExtraOptionsFromEnv(["resolve", "queries"]),
977960
...queries,
978961
];
979-
const output = await runCli(cmd, codeqlArgs, { noStreamStdout: true });
980-
981-
try {
982-
return JSON.parse(output) as string[];
983-
} catch (e) {
984-
throw new Error(
985-
`Unexpected output from codeql resolve queries --format=startingpacks: ${e}`,
986-
);
987-
}
962+
return await runCliJson<string[]>(cmd, codeqlArgs, {
963+
noStreamStdout: true,
964+
});
988965
},
989966
async resolveDatabase(
990967
databasePath: string,
@@ -996,15 +973,9 @@ async function getCodeQLForCmd(
996973
"--format=json",
997974
...getExtraOptionsFromEnv(["resolve", "database"]),
998975
];
999-
const output = await runCli(cmd, codeqlArgs, { noStreamStdout: true });
1000-
1001-
try {
1002-
return JSON.parse(output) as ResolveDatabaseOutput;
1003-
} catch (e) {
1004-
throw new Error(
1005-
`Unexpected output from codeql resolve database --format=json: ${e}`,
1006-
);
1007-
}
976+
return await runCliJson<ResolveDatabaseOutput>(cmd, codeqlArgs, {
977+
noStreamStdout: true,
978+
});
1008979
},
1009980
async mergeResults(
1010981
sarifFiles: string[],
@@ -1160,6 +1131,30 @@ async function runCli(
11601131
}
11611132
}
11621133

1134+
/**
1135+
* Wraps the command executor {@link runCli} and tries to parse the output as JSON.
1136+
* @param cmd The command to run.
1137+
* @param args The arguments to pass to the command.
1138+
* @param opts The options for running the command.
1139+
* @param opts.stdin Optional string to pass to the command's standard input.
1140+
* @param opts.noStreamStdout Optional boolean to indicate whether to stream the command's standard output.
1141+
* @returns The parsed JSON output from the command.
1142+
*/
1143+
async function runCliJson<T>(
1144+
cmd: string,
1145+
args: string[] = [],
1146+
opts: { stdin?: string; noStreamStdout?: boolean } = {},
1147+
): Promise<T> {
1148+
const output = await runCli(cmd, args, opts);
1149+
try {
1150+
return JSON.parse(output) as T;
1151+
} catch (e) {
1152+
throw Error(
1153+
`Unexpected output from codeql ${args.join(" ")}: ${getErrorMessage(e)}`,
1154+
);
1155+
}
1156+
}
1157+
11631158
/**
11641159
* Writes the code scanning configuration that is to be used by the CLI.
11651160
*

0 commit comments

Comments
 (0)