Skip to content

Commit dc8e1e9

Browse files
committed
Refactor CLI JSON handling into a dedicated runCliJson function
1 parent 889ae42 commit dc8e1e9

1 file changed

Lines changed: 29 additions & 37 deletions

File tree

src/codeql.ts

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -512,18 +512,17 @@ async function getCodeQLForCmd(
512512
return cmd;
513513
},
514514
async getVersion() {
515+
async function runCliVersion() {
516+
return await runCliJson<VersionInfo>(
517+
cmd,
518+
["version", "--format=json"],
519+
{ noStreamStdout: true },
520+
);
521+
}
522+
515523
let result = util.getCachedCodeQlVersion(cmd);
516524
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-
}
525+
result = await runCliVersion();
527526
util.cacheCodeQlVersion(cmd, result);
528527
}
529528
return result;
@@ -774,15 +773,7 @@ async function getCodeQLForCmd(
774773
if (workingDir !== undefined) {
775774
codeqlArgs.push("--working-dir", workingDir);
776775
}
777-
const output = await runCli(cmd, codeqlArgs);
778-
779-
try {
780-
return JSON.parse(output) as ResolveBuildEnvironmentOutput;
781-
} catch (e) {
782-
throw new Error(
783-
`Unexpected output from codeql resolve build-environment: ${e} in\n${output}`,
784-
);
785-
}
776+
return await runCliJson<ResolveBuildEnvironmentOutput>(cmd, codeqlArgs);
786777
},
787778
async databaseRunQueries(
788779
databasePath: string,
@@ -963,15 +954,9 @@ async function getCodeQLForCmd(
963954
...getExtraOptionsFromEnv(["resolve", "queries"]),
964955
...queries,
965956
];
966-
const output = await runCli(cmd, codeqlArgs, { noStreamStdout: true });
967-
968-
try {
969-
return JSON.parse(output) as string[];
970-
} catch (e) {
971-
throw new Error(
972-
`Unexpected output from codeql resolve queries --format=startingpacks: ${e}`,
973-
);
974-
}
957+
return await runCliJson<string[]>(cmd, codeqlArgs, {
958+
noStreamStdout: true,
959+
});
975960
},
976961
async resolveDatabase(
977962
databasePath: string,
@@ -983,15 +968,9 @@ async function getCodeQLForCmd(
983968
"--format=json",
984969
...getExtraOptionsFromEnv(["resolve", "database"]),
985970
];
986-
const output = await runCli(cmd, codeqlArgs, { noStreamStdout: true });
987-
988-
try {
989-
return JSON.parse(output) as ResolveDatabaseOutput;
990-
} catch (e) {
991-
throw new Error(
992-
`Unexpected output from codeql resolve database --format=json: ${e}`,
993-
);
994-
}
971+
return await runCliJson<ResolveDatabaseOutput>(cmd, codeqlArgs, {
972+
noStreamStdout: true,
973+
});
995974
},
996975
async mergeResults(
997976
sarifFiles: string[],
@@ -1147,6 +1126,19 @@ async function runCli(
11471126
}
11481127
}
11491128

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

0 commit comments

Comments
 (0)