Dae*_*Dae 7 visual-studio-code vscode-extensions
let result = await vscode.commands.executeCommand('workbench.action.tasks.build');
Run Code Online (Sandbox Code Playgroud)
立即解决。
如何使用 VS Code API 等待构建任务?
我想到了!不能从 等待任务vscode.tasks.executeTask,但我们可以等待vscode.tasks.onDidEndTask并检查已结束的任务是否是我们的任务。
async function executeBuildTask(task: vscode.Task) {
const execution = await vscode.tasks.executeTask(task);
return new Promise<void>(resolve => {
let disposable = vscode.tasks.onDidEndTask(e => {
if (e.execution.task.group === vscode.TaskGroup.Build) {
disposable.dispose();
resolve();
}
});
});
}
async function getBuildTasks() {
return new Promise<vscode.Task[]>(resolve => {
vscode.tasks.fetchTasks().then((tasks) => {
resolve(tasks.filter((task) => task.group === vscode.TaskGroup.Build));
});
});
}
export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.commands.registerCommand('extension.helloWorld', async () => {
const buildTasks = await getBuildTasks();
await executeBuildTask(buildTasks[0]);
}));
}
Run Code Online (Sandbox Code Playgroud)
请注意,当前存在一个错误 #96643,它阻止我们对 vscode.Task 对象进行比较:if (e.execution.task === execution.task) { ... }