Skip to content

Commit 6cdc606

Browse files
committed
Create wrapper runCliJson
1 parent 628fc3f commit 6cdc606

2 files changed

Lines changed: 59 additions & 93 deletions

File tree

lib/entry-points.js

Lines changed: 27 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: 32 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,19 @@ async function runCli(
11601131
}
11611132
}
11621133

1134+
async function runCliJson<T>(
1135+
cmd: string,
1136+
args: string[] = [],
1137+
opts: { stdin?: string; noStreamStdout?: boolean } = {},
1138+
): Promise<T> {
1139+
const output = await runCli(cmd, args, opts);
1140+
try {
1141+
return JSON.parse(output) as T;
1142+
} catch {
1143+
throw Error(`Invalid JSON output from \`${args.join(" ")}\`: ${output}`);
1144+
}
1145+
}
1146+
11631147
/**
11641148
* Writes the code scanning configuration that is to be used by the CLI.
11651149
*

0 commit comments

Comments
 (0)