Archived
1
0
This repository has been archived on 2024-09-09. You can view files and clone it, but cannot push or open issues or pull requests.
code-server/lib/vscode/extensions/npm/src/commands.ts
2021-03-11 10:27:10 -07:00

71 lines
2.2 KiB
TypeScript

/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as nls from 'vscode-nls';
import * as vscode from 'vscode';
import {
detectNpmScriptsForFolder,
findScriptAtPosition,
runScript,
FolderTaskItem
} from './tasks';
const localize = nls.loadMessageBundle();
export function runSelectedScript(context: vscode.ExtensionContext) {
let editor = vscode.window.activeTextEditor;
if (!editor) {
return;
}
let document = editor.document;
let contents = document.getText();
let script = findScriptAtPosition(editor.document, contents, editor.selection.anchor);
if (script) {
runScript(context, script, document);
} else {
let message = localize('noScriptFound', 'Could not find a valid npm script at the selection.');
vscode.window.showErrorMessage(message);
}
}
export async function selectAndRunScriptFromFolder(context: vscode.ExtensionContext, selectedFolders: vscode.Uri[]) {
if (selectedFolders.length === 0) {
return;
}
const selectedFolder = selectedFolders[0];
let taskList: FolderTaskItem[] = await detectNpmScriptsForFolder(context, selectedFolder);
if (taskList && taskList.length > 0) {
const quickPick = vscode.window.createQuickPick<FolderTaskItem>();
quickPick.title = 'Run NPM script in Folder';
quickPick.placeholder = 'Select an npm script';
quickPick.items = taskList;
const toDispose: vscode.Disposable[] = [];
let pickPromise = new Promise<FolderTaskItem | undefined>((c) => {
toDispose.push(quickPick.onDidAccept(() => {
toDispose.forEach(d => d.dispose());
c(quickPick.selectedItems[0]);
}));
toDispose.push(quickPick.onDidHide(() => {
toDispose.forEach(d => d.dispose());
c(undefined);
}));
});
quickPick.show();
let result = await pickPromise;
quickPick.dispose();
if (result) {
vscode.tasks.executeTask(result.task);
}
}
else {
vscode.window.showInformationMessage(`No npm scripts found in ${selectedFolder.fsPath}`, { modal: true });
}
}