提交 aa9a4820 编写于 作者: E Erich Gamma

Merge branch 'helen3141-runNpmFromContextMenu'

......@@ -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,5 +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`.
......@@ -88,6 +88,10 @@
{
"command": "npm.runSelectedScript",
"title": "%command.runSelectedScript%"
},
{
"command": "npm.runScriptFromFolder",
"title": "%command.runScriptFromFolder%"
}
],
"menus": {
......@@ -115,6 +119,10 @@
{
"command": "npm.runSelectedScript",
"when": "false"
},
{
"command": "npm.runScriptFromFolder",
"when": "false"
}
],
"editor/context": [
......@@ -172,7 +180,14 @@
"when": "view == npm && viewItem == script",
"group": "navigation@3"
}
]
],
"explorer/context": [
{
"when": "config.npm.enableRunFromFolderContextMenu && explorerViewletVisible && explorerResourceIsFolder",
"command": "npm.runScriptFromFolder",
"group": "2_workspace"
}
]
},
"configuration": {
"id": "npm",
......@@ -222,6 +237,12 @@
"scope": "resource",
"description": "%config.npm.enableScriptExplorer%"
},
"npm.enableRunFromFolderContextMenu": {
"type": "boolean",
"default": false,
"scope": "resource",
"description": "%config.npm.enableRunFromFolderContextMenu%"
},
"npm.scriptExplorerAction": {
"type": "string",
"enum": [
......
......@@ -7,6 +7,7 @@
"config.npm.exclude": "Configure glob patterns for folders that should be excluded from automatic script detection.",
"config.npm.enableScriptExplorer": "Enable an explorer view for npm scripts when there is no top-level 'package.json' file.",
"config.npm.scriptExplorerAction": "The default click action used in the scripts explorer: `open` or `run`, the default is `open`.",
"config.npm.enableRunFromFolderContextMenu": "Enable running scripts from the context menu of a folder in Explorer",
"config.npm.fetchOnlinePackageInfo": "Fetch data from https://registry.npmjs.org and https://registry.bower.io to provide auto-completion and information on hover features on npm dependencies.",
"npm.parseError": "Npm task detection: failed to parse the file {0}",
"taskdef.script": "The npm script to customize.",
......@@ -17,5 +18,6 @@
"command.debug": "Debug",
"command.openScript": "Open",
"command.runInstall": "Run Install",
"command.runSelectedScript": "Run Script"
"command.runSelectedScript": "Run Script",
"command.runScriptFromFolder": "Run npm Script in Folder..."
}
......@@ -3,11 +3,14 @@
* 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 {
runScript, findScriptAtPosition
detectNpmScriptsForFolder,
findScriptAtPosition,
runScript
} from './tasks';
import * as nls from 'vscode-nls';
const localize = nls.loadMessageBundle();
......@@ -28,4 +31,18 @@ export function runSelectedScript() {
let message = localize('noScriptFound', 'Could not find a valid npm script at the selection.');
vscode.window.showErrorMessage(message);
}
}
\ No newline at end of file
}
export async function selectAndRunScriptFromFolder(selectedFolder: vscode.Uri) {
let taskList: { label: string, task: vscode.Task }[] = await detectNpmScriptsForFolder(selectedFolder);
if (taskList && taskList.length > 0) {
let result = await vscode.window.showQuickPick(taskList, { placeHolder: 'Select script' });
if (result) {
vscode.tasks.executeTask(result.task);
}
}
else {
vscode.window.showInformationMessage(`No npm scripts found in ${selectedFolder.fsPath}`, { modal: true });
}
}
......@@ -6,10 +6,10 @@
import * as httpRequest from 'request-light';
import * as vscode from 'vscode';
import { addJSONProviders } from './features/jsonContributions';
import { runSelectedScript, selectAndRunScriptFromFolder } from './commands';
import { NpmScriptsTreeDataProvider } from './npmView';
import { invalidateTasksCache, NpmTaskProvider, hasPackageJson } from './tasks';
import { invalidateHoverScriptsCache, NpmScriptHoverProvider } from './scriptHover';
import { runSelectedScript } from './commands';
let treeDataProvider: NpmScriptsTreeDataProvider | undefined;
......@@ -45,6 +45,8 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
if (await hasPackageJson()) {
vscode.commands.executeCommand('setContext', 'npm:showScriptExplorer', true);
}
context.subscriptions.push(vscode.commands.registerCommand('npm.runScriptFromFolder', selectAndRunScriptFromFolder));
}
function registerTaskProvider(context: vscode.ExtensionContext): vscode.Disposable | undefined {
......
......@@ -155,6 +155,46 @@ async function detectNpmScripts(): Promise<Task[]> {
}
}
export async function detectNpmScriptsForFolder(folder: Uri): Promise<{ label: string, task: Task }[]> {
let folderTasks: { label: string, task: Task }[] = [];
try {
let relativePattern = new RelativePattern(folder.fsPath, '**/package.json');
let paths = await workspace.findFiles(relativePattern, '**/node_modules/**');
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 })));
}
}
}
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);
}
}
export async function provideNpmScripts(): Promise<Task[]> {
if (!cachedTasks) {
cachedTasks = await detectNpmScripts();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册