From cecda63be5fee20b26ef4b731b2ea3ff4351a4bc Mon Sep 17 00:00:00 2001 From: helen3141 Date: Fri, 11 Oct 2019 15:18:31 -0700 Subject: [PATCH] Pull Request feedback: updated documentation and naming, read from cache if available instead of creating new tasks --- extensions/npm/README.md | 6 +++++- extensions/npm/package.json | 8 +++---- extensions/npm/package.nls.json | 2 +- extensions/npm/src/commands.ts | 22 +++++-------------- extensions/npm/src/main.ts | 2 +- extensions/npm/src/tasks.ts | 38 ++++++++++++++++++++++++--------- 6 files changed, 44 insertions(+), 34 deletions(-) diff --git a/extensions/npm/README.md b/extensions/npm/README.md index 44c08ff3998..38058db7c75 100644 --- a/extensions/npm/README.md +++ b/extensions/npm/README.md @@ -22,6 +22,10 @@ The Npm Script Explorer shows the npm scripts found in your workspace. The explo The extension supports to run the selected script as a task when editing the `package.json`file. You can either run a script from the hover shown on a script or using the command `Run Selected Npm Script`. +### Run Scripts from a Folder in Explorer + +The extension supports running a script as a task from the Explorer. Right-click a folder in Explorer and select the `Run npm Script in Folder...` option to bring up a command palette listing the scripts that the folder contains. You can run the script by selecting from the options listed in the command palette. + ### Others The extension fetches data from https://registry.npmjs/org and https://registry.bower.io to provide auto-completion and information on hover features on npm dependencies. @@ -34,7 +38,7 @@ The extension fetches data from https://registry.npmjs/org and https://registry. - `npm.exclude` - Glob patterns for folders that should be excluded from automatic script detection. The pattern is matched against the **absolute path** of the package.json. For example, to exclude all test folders use '**/test/**'. - `npm.enableScriptExplorer` - Enable an explorer view for npm scripts. - `npm.scriptExplorerAction` - The default click action: `open` or `run`, the default is `open`. +- `npm.enableRunFromFolderContextMenu` - Enable running npm scripts from the context menu of folders in Explorer, the default is `false`. - `npm.scriptCodeLens.enable` - Enable/disable the code lenses to run a script, the default is `false`. -- `npm.enableRunFromFolderContextMenu` - Enable running npm scripts from the context menu of folders in Explorer, the default is `true`. diff --git a/extensions/npm/package.json b/extensions/npm/package.json index ad3d9933908..03be1f4b4c9 100644 --- a/extensions/npm/package.json +++ b/extensions/npm/package.json @@ -90,8 +90,8 @@ "title": "%command.runSelectedScript%" }, { - "command": "npm.runScriptsFromFolder", - "title": "%command.runScriptsFromFolder%" + "command": "npm.runScriptFromFolder", + "title": "%command.runScriptFromFolder%" } ], "menus": { @@ -180,7 +180,7 @@ "explorer/context": [ { "when": "config.npm.enableRunFromFolderContextMenu && explorerViewletVisible && explorerResourceIsFolder", - "command": "npm.runScriptsFromFolder", + "command": "npm.runScriptFromFolder", "group": "2_workspace" } ] @@ -235,7 +235,7 @@ }, "npm.enableRunFromFolderContextMenu": { "type": "boolean", - "default": true, + "default": false, "scope": "resource", "description": "%config.npm.enableRunFromFolderContextMenu%" }, diff --git a/extensions/npm/package.nls.json b/extensions/npm/package.nls.json index a0944189689..90520b575c4 100644 --- a/extensions/npm/package.nls.json +++ b/extensions/npm/package.nls.json @@ -19,5 +19,5 @@ "command.openScript": "Open", "command.runInstall": "Run Install", "command.runSelectedScript": "Run Script", - "command.runScriptsFromFolder": "Select npm Scripts to Run..." + "command.runScriptFromFolder": "Run npm Script in Folder..." } diff --git a/extensions/npm/src/commands.ts b/extensions/npm/src/commands.ts index f0b97600ebc..e9a94e0763f 100644 --- a/extensions/npm/src/commands.ts +++ b/extensions/npm/src/commands.ts @@ -9,7 +9,6 @@ import * as vscode from 'vscode'; import { detectNpmScriptsForFolder, findScriptAtPosition, - getPackageJsonUriFromTask, runScript } from './tasks'; @@ -34,27 +33,16 @@ export function runSelectedScript() { } } -export async function selectAndRunScriptFromFolder(folderInfo: vscode.Uri) { - type TaskMap = { [id: string]: vscode.Task; }; - let taskList = await detectNpmScriptsForFolder(folderInfo.path); +export async function selectAndRunScriptFromFolder(selectedFolder: vscode.Uri) { + let taskList: { label: string, task: vscode.Task }[] = await detectNpmScriptsForFolder(selectedFolder); if (taskList && taskList.length > 0) { - let taskMap: TaskMap = {}; - taskList.forEach(t => { - let uri = getPackageJsonUriFromTask(t); - if (uri && uri.fsPath.length >= folderInfo.fsPath.length) { - let taskName = uri.fsPath.substring(folderInfo.fsPath.length, uri.fsPath.length - '/package.json'.length) + ' > ' + t.name.substring(0, t.name.search('-')); - taskMap[taskName] = t; - } - }); - let result = await vscode.window.showQuickPick(Object.keys(taskMap).sort(), { - placeHolder: `Run scripts on folder ${folderInfo.fsPath}...`, - }); + let result = await vscode.window.showQuickPick(taskList, { placeHolder: 'Select script' }); if (result) { - vscode.tasks.executeTask(taskMap[result]); + vscode.tasks.executeTask(result.task); } } else { - vscode.window.showInformationMessage(`No scripts detected in folder ${folderInfo.path}`); + vscode.window.showInformationMessage(`No npm scripts found in ${selectedFolder.fsPath}`, { modal: true }); } } diff --git a/extensions/npm/src/main.ts b/extensions/npm/src/main.ts index 921908468d0..3777f393757 100644 --- a/extensions/npm/src/main.ts +++ b/extensions/npm/src/main.ts @@ -46,7 +46,7 @@ export async function activate(context: vscode.ExtensionContext): Promise vscode.commands.executeCommand('setContext', 'npm:showScriptExplorer', true); } - context.subscriptions.push(vscode.commands.registerCommand('npm.runScriptsFromFolder', selectAndRunScriptFromFolder)); + context.subscriptions.push(vscode.commands.registerCommand('npm.runScriptFromFolder', selectAndRunScriptFromFolder)); } function registerTaskProvider(context: vscode.ExtensionContext): vscode.Disposable | undefined { diff --git a/extensions/npm/src/tasks.ts b/extensions/npm/src/tasks.ts index c7a9f52d152..f80e0b1526b 100644 --- a/extensions/npm/src/tasks.ts +++ b/extensions/npm/src/tasks.ts @@ -156,22 +156,40 @@ async function detectNpmScripts(): Promise { } -export async function detectNpmScriptsForFolder(folder: string): Promise { +export async function detectNpmScriptsForFolder(folder: Uri): Promise<{ label: string, task: Task }[]> { - let allTasks: Task[] = []; - let visitedPackageJsonFiles: Set = new Set(); + let folderTasks: { label: string, task: Task }[] = []; try { - let relativePattern = new RelativePattern(folder, '**/package.json'); + let relativePattern = new RelativePattern(folder.fsPath, '**/package.json'); let paths = await workspace.findFiles(relativePattern, '**/node_modules/**'); - for (const path of paths) { - if (!visitedPackageJsonFiles.has(path.fsPath)) { - let tasks = await provideNpmScriptsForFolder(path); - visitedPackageJsonFiles.add(path.fsPath); - allTasks.push(...tasks); + + if (cachedTasks) { + let workspaceFolder = workspace.getWorkspaceFolder(folder); + if (workspaceFolder) { + let rootUri = workspaceFolder.uri.path; + if (rootUri === folder.path) { + return cachedTasks.map(t => ({ label: t.name, task: t })); + } + + let relativePaths = paths.map(p => ' - ' + p.path.substring(rootUri.length + 1, p.path.length - '/package.json'.length)); + for (const relativePath of relativePaths) { + folderTasks.push(...cachedTasks.filter(t => t.name.endsWith(relativePath)).map(t => ({ label: t.name, task: t }))); + } } } - return allTasks; + else { + let visitedPackageJsonFiles: Set = new Set(); + for (const path of paths) { + if (!visitedPackageJsonFiles.has(path.fsPath)) { + let tasks = await provideNpmScriptsForFolder(path); + visitedPackageJsonFiles.add(path.fsPath); + folderTasks.push(...tasks.map(t => ({ label: t.name, task: t }))); + } + } + } + + return folderTasks; } catch (error) { return Promise.reject(error); }