Pull Request feedback: updated documentation and naming, read from cache if available instead of creating new tasks

This commit is contained in:
helen3141 2019-10-11 15:18:31 -07:00
parent 3577fd0fab
commit cecda63be5
6 changed files with 44 additions and 34 deletions

View file

@ -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`.

View file

@ -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%"
},

View file

@ -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..."
}

View file

@ -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 });
}
}

View file

@ -46,7 +46,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
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 {

View file

@ -156,22 +156,40 @@ async function detectNpmScripts(): Promise<Task[]> {
}
export async function detectNpmScriptsForFolder(folder: string): Promise<Task[]> {
export async function detectNpmScriptsForFolder(folder: Uri): Promise<{ label: string, task: Task }[]> {
let allTasks: Task[] = [];
let visitedPackageJsonFiles: Set<string> = 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<string> = 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);
}