import { commands, ExtensionContext, Uri, window } from 'vscode';
import { getProjectByPath } from '@nx-console/vscode-nx-workspace';
import { createProjectTargetString } from '@nx-console/vscode-utils';
import { CliTaskProvider } from './cli-task-provider';
import { selectRunInformation } from '@nx-console/vscode-nx-cli-quickpicks';
import { getTelemetry } from '@nx-console/vscode-telemetry';
export async function registerCliTaskCommands(context: ExtensionContext) {
context.subscriptions.push(
commands.registerCommand(
`nx.run`,
async (
project?: string,
target?: string,
configuration?: string,
askForFlags?: boolean,
) => {
getTelemetry().logUsage('tasks.run', {
source: target ? 'nx-commands-panel' : 'command',
});
selectRunInformationAndRun(project, target, configuration, askForFlags);
},
),
commands.registerCommand(
`nx.run.fileexplorer`,
async (uri: Uri | undefined) => {
getTelemetry().logUsage('tasks.run', {
source: uri ? 'explorer-context-menu' : 'command',
});
if (!uri) {
uri = window.activeTextEditor?.document.uri;
}
if (!uri) {
return;
}
selectRunInformationAndRun(await getCliProjectFromUri(uri));
},
),
commands.registerCommand(`nx.run.target`, async () => {
getTelemetry().logUsage('tasks.run');
selectRunInformationAndRun(undefined, undefined, undefined, true, true);
}),
);
}
export async function selectRunInformationAndRun(
projectName?: string,
targetName?: string,
configuration?: string,
askForFlags = true,
selectTargetFirst = false,
) {
const runInformation = await selectRunInformation(
projectName,
targetName,
configuration,
askForFlags,
selectTargetFirst,
);
if (
!runInformation ||
!runInformation.projectName ||
!runInformation.targetName ||
!runInformation.flags
) {
return;
}
const {
projectName: p,
targetName: t,
configuration: c,
flags: f,
} = runInformation;
const positional = createProjectTargetString(p, t, c);
CliTaskProvider.instance.executeTask({
positional,
command: 'run',
flags: f,
});
}
export async function getCliProjectFromUri(
uri: Uri,
): Promise<string | undefined> {
const project = await getProjectByPath(uri.fsPath);
return project?.name;
}