Convert some base commands to typed commands

This commit is contained in:
Koen Vlaswinkel
2023-03-22 17:02:16 +01:00
parent 322c1a8835
commit fe6ff6801a
2 changed files with 35 additions and 39 deletions

View File

@@ -35,7 +35,10 @@ export type SingleSelectionCommandFunction<Item> = (
// Base commands not tied directly to a module like e.g. variant analysis.
export type BaseCommands = {
"codeQL.openDocumentation": () => Promise<void>;
"codeQL.showLogs": () => Promise<void>;
"codeQL.authenticateToGitHub": () => Promise<void>;
"codeQL.copyVersion": () => Promise<void>;
"codeQL.restartQueryServer": () => Promise<void>;
};

View File

@@ -123,6 +123,7 @@ import {
showResultsForCompletedQuery,
} from "./local-queries";
import { getAstCfgCommands } from "./ast-cfg-commands";
import { App } from "./common/app";
/**
* extension.ts
@@ -158,9 +159,18 @@ const extension = extensions.getExtension(extensionId);
* Return all commands that are not tied to the more specific managers.
*/
function getCommands(
app: App,
cliServer: CodeQLCliServer,
queryRunner: QueryRunner,
): BaseCommands {
const getCliVersion = async () => {
try {
return await cliServer.getVersion();
} catch {
return "<missing>";
}
};
return {
"codeQL.openDocumentation": async () => {
await env.openExternal(Uri.parse("https://codeql.github.com/docs/"));
@@ -179,6 +189,27 @@ function getCommands(
title: "Restarting Query Server",
},
),
"codeQL.copyVersion": async () => {
const text = `CodeQL extension version: ${
extension?.packageJSON.version
} \nCodeQL CLI version: ${await getCliVersion()} \nPlatform: ${platform()} ${arch()}`;
await env.clipboard.writeText(text);
void showAndLogInformationMessage(text);
},
"codeQL.authenticateToGitHub": async () => {
/**
* Credentials for authenticating to GitHub.
* These are used when making API calls.
*/
const octokit = await app.credentials.getOctokit();
const userInfo = await octokit.users.getAuthenticated();
void showAndLogInformationMessage(
`Authenticated to GitHub as user: ${userInfo.data.login}`,
);
},
"codeQL.showLogs": async () => {
extLogger.show();
},
};
}
@@ -833,7 +864,7 @@ async function activateWithInstalledDistribution(
void extLogger.log("Registering top-level command palette commands.");
const allCommands: AllCommands = {
...getCommands(cliServer, qs),
...getCommands(app, cliServer, qs),
...localQueryResultsView.getCommands(),
...qhm.getCommands(),
...variantAnalysisManager.getCommands(),
@@ -915,44 +946,6 @@ async function activateWithInstalledDistribution(
}),
);
ctx.subscriptions.push(
commandRunner("codeQL.copyVersion", async () => {
const text = `CodeQL extension version: ${
extension?.packageJSON.version
} \nCodeQL CLI version: ${await getCliVersion()} \nPlatform: ${platform()} ${arch()}`;
await env.clipboard.writeText(text);
void showAndLogInformationMessage(text);
}),
);
const getCliVersion = async () => {
try {
return await cliServer.getVersion();
} catch {
return "<missing>";
}
};
ctx.subscriptions.push(
commandRunner("codeQL.authenticateToGitHub", async () => {
/**
* Credentials for authenticating to GitHub.
* These are used when making API calls.
*/
const octokit = await app.credentials.getOctokit();
const userInfo = await octokit.users.getAuthenticated();
void showAndLogInformationMessage(
`Authenticated to GitHub as user: ${userInfo.data.login}`,
);
}),
);
ctx.subscriptions.push(
commandRunner("codeQL.showLogs", async () => {
extLogger.show();
}),
);
void extLogger.log("Starting language server.");
await client.start();
ctx.subscriptions.push({