Skip to content

Commit cecda63

Browse files
committed
Pull Request feedback: updated documentation and naming, read from cache if available instead of creating new tasks
1 parent 3577fd0 commit cecda63

6 files changed

Lines changed: 44 additions & 34 deletions

File tree

extensions/npm/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ The Npm Script Explorer shows the npm scripts found in your workspace. The explo
2222
The extension supports to run the selected script as a task when editing the `package.json`file. You can either run a script from
2323
the hover shown on a script or using the command `Run Selected Npm Script`.
2424

25+
### Run Scripts from a Folder in Explorer
26+
27+
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.
28+
2529
### Others
2630

2731
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.
3438
- `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/**'.
3539
- `npm.enableScriptExplorer` - Enable an explorer view for npm scripts.
3640
- `npm.scriptExplorerAction` - The default click action: `open` or `run`, the default is `open`.
41+
- `npm.enableRunFromFolderContextMenu` - Enable running npm scripts from the context menu of folders in Explorer, the default is `false`.
3742
- `npm.scriptCodeLens.enable` - Enable/disable the code lenses to run a script, the default is `false`.
38-
- `npm.enableRunFromFolderContextMenu` - Enable running npm scripts from the context menu of folders in Explorer, the default is `true`.
3943

4044

extensions/npm/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@
9090
"title": "%command.runSelectedScript%"
9191
},
9292
{
93-
"command": "npm.runScriptsFromFolder",
94-
"title": "%command.runScriptsFromFolder%"
93+
"command": "npm.runScriptFromFolder",
94+
"title": "%command.runScriptFromFolder%"
9595
}
9696
],
9797
"menus": {
@@ -180,7 +180,7 @@
180180
"explorer/context": [
181181
{
182182
"when": "config.npm.enableRunFromFolderContextMenu && explorerViewletVisible && explorerResourceIsFolder",
183-
"command": "npm.runScriptsFromFolder",
183+
"command": "npm.runScriptFromFolder",
184184
"group": "2_workspace"
185185
}
186186
]
@@ -235,7 +235,7 @@
235235
},
236236
"npm.enableRunFromFolderContextMenu": {
237237
"type": "boolean",
238-
"default": true,
238+
"default": false,
239239
"scope": "resource",
240240
"description": "%config.npm.enableRunFromFolderContextMenu%"
241241
},

extensions/npm/package.nls.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919
"command.openScript": "Open",
2020
"command.runInstall": "Run Install",
2121
"command.runSelectedScript": "Run Script",
22-
"command.runScriptsFromFolder": "Select npm Scripts to Run..."
22+
"command.runScriptFromFolder": "Run npm Script in Folder..."
2323
}

extensions/npm/src/commands.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import * as vscode from 'vscode';
99
import {
1010
detectNpmScriptsForFolder,
1111
findScriptAtPosition,
12-
getPackageJsonUriFromTask,
1312
runScript
1413
} from './tasks';
1514

@@ -34,27 +33,16 @@ export function runSelectedScript() {
3433
}
3534
}
3635

37-
export async function selectAndRunScriptFromFolder(folderInfo: vscode.Uri) {
38-
type TaskMap = { [id: string]: vscode.Task; };
39-
let taskList = await detectNpmScriptsForFolder(folderInfo.path);
36+
export async function selectAndRunScriptFromFolder(selectedFolder: vscode.Uri) {
37+
let taskList: { label: string, task: vscode.Task }[] = await detectNpmScriptsForFolder(selectedFolder);
4038

4139
if (taskList && taskList.length > 0) {
42-
let taskMap: TaskMap = {};
43-
taskList.forEach(t => {
44-
let uri = getPackageJsonUriFromTask(t);
45-
if (uri && uri.fsPath.length >= folderInfo.fsPath.length) {
46-
let taskName = uri.fsPath.substring(folderInfo.fsPath.length, uri.fsPath.length - '/package.json'.length) + ' > ' + t.name.substring(0, t.name.search('-'));
47-
taskMap[taskName] = t;
48-
}
49-
});
50-
let result = await vscode.window.showQuickPick(Object.keys(taskMap).sort(), {
51-
placeHolder: `Run scripts on folder ${folderInfo.fsPath}...`,
52-
});
40+
let result = await vscode.window.showQuickPick(taskList, { placeHolder: 'Select script' });
5341
if (result) {
54-
vscode.tasks.executeTask(taskMap[result]);
42+
vscode.tasks.executeTask(result.task);
5543
}
5644
}
5745
else {
58-
vscode.window.showInformationMessage(`No scripts detected in folder ${folderInfo.path}`);
46+
vscode.window.showInformationMessage(`No npm scripts found in ${selectedFolder.fsPath}`, { modal: true });
5947
}
6048
}

extensions/npm/src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
4646
vscode.commands.executeCommand('setContext', 'npm:showScriptExplorer', true);
4747
}
4848

49-
context.subscriptions.push(vscode.commands.registerCommand('npm.runScriptsFromFolder', selectAndRunScriptFromFolder));
49+
context.subscriptions.push(vscode.commands.registerCommand('npm.runScriptFromFolder', selectAndRunScriptFromFolder));
5050
}
5151

5252
function registerTaskProvider(context: vscode.ExtensionContext): vscode.Disposable | undefined {

extensions/npm/src/tasks.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,22 +156,40 @@ async function detectNpmScripts(): Promise<Task[]> {
156156
}
157157

158158

159-
export async function detectNpmScriptsForFolder(folder: string): Promise<Task[]> {
159+
export async function detectNpmScriptsForFolder(folder: Uri): Promise<{ label: string, task: Task }[]> {
160160

161-
let allTasks: Task[] = [];
162-
let visitedPackageJsonFiles: Set<string> = new Set();
161+
let folderTasks: { label: string, task: Task }[] = [];
163162

164163
try {
165-
let relativePattern = new RelativePattern(folder, '**/package.json');
164+
let relativePattern = new RelativePattern(folder.fsPath, '**/package.json');
166165
let paths = await workspace.findFiles(relativePattern, '**/node_modules/**');
167-
for (const path of paths) {
168-
if (!visitedPackageJsonFiles.has(path.fsPath)) {
169-
let tasks = await provideNpmScriptsForFolder(path);
170-
visitedPackageJsonFiles.add(path.fsPath);
171-
allTasks.push(...tasks);
166+
167+
if (cachedTasks) {
168+
let workspaceFolder = workspace.getWorkspaceFolder(folder);
169+
if (workspaceFolder) {
170+
let rootUri = workspaceFolder.uri.path;
171+
if (rootUri === folder.path) {
172+
return cachedTasks.map(t => ({ label: t.name, task: t }));
173+
}
174+
175+
let relativePaths = paths.map(p => ' - ' + p.path.substring(rootUri.length + 1, p.path.length - '/package.json'.length));
176+
for (const relativePath of relativePaths) {
177+
folderTasks.push(...cachedTasks.filter(t => t.name.endsWith(relativePath)).map(t => ({ label: t.name, task: t })));
178+
}
172179
}
173180
}
174-
return allTasks;
181+
else {
182+
let visitedPackageJsonFiles: Set<string> = new Set();
183+
for (const path of paths) {
184+
if (!visitedPackageJsonFiles.has(path.fsPath)) {
185+
let tasks = await provideNpmScriptsForFolder(path);
186+
visitedPackageJsonFiles.add(path.fsPath);
187+
folderTasks.push(...tasks.map(t => ({ label: t.name, task: t })));
188+
}
189+
}
190+
}
191+
192+
return folderTasks;
175193
} catch (error) {
176194
return Promise.reject(error);
177195
}

0 commit comments

Comments
 (0)