Added command to Run the selected npm script
This commit is contained in:
parent
0218d1d3f1
commit
82423033d9
6 changed files with 88 additions and 4 deletions
|
@ -19,7 +19,8 @@ The Npm Script Explorer shows the npm scripts found in your workspace. The explo
|
|||
|
||||
### Run Scripts from the Editor
|
||||
|
||||
The extension provides code lense actions to run or debug a script from the editor.
|
||||
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`.
|
||||
|
||||
### Others
|
||||
|
||||
|
|
|
@ -82,6 +82,10 @@
|
|||
"light": "resources/light/refresh.svg",
|
||||
"dark": "resources/dark/refresh.svg"
|
||||
}
|
||||
},
|
||||
{
|
||||
"command": "npm.runSelectedScript",
|
||||
"title": "%command.runSelectedScript%"
|
||||
}
|
||||
],
|
||||
"menus": {
|
||||
|
|
|
@ -16,5 +16,6 @@
|
|||
"command.run": "Run",
|
||||
"command.debug": "Debug",
|
||||
"command.openScript": "Open",
|
||||
"command.runInstall": "Run Install"
|
||||
"command.runInstall": "Run Install",
|
||||
"command.runSelectedScript": "Run Selected Npm Script"
|
||||
}
|
||||
|
|
32
extensions/npm/src/commands.ts
Normal file
32
extensions/npm/src/commands.ts
Normal file
|
@ -0,0 +1,32 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
'use strict';
|
||||
|
||||
import * as vscode from 'vscode';
|
||||
import {
|
||||
runScript, findScriptAtPosition
|
||||
} from './tasks';
|
||||
import * as nls from 'vscode-nls';
|
||||
|
||||
const localize = nls.loadMessageBundle();
|
||||
|
||||
export function runSelectedScript() {
|
||||
let editor = vscode.window.activeTextEditor;
|
||||
if (!editor) {
|
||||
return;
|
||||
}
|
||||
let document = editor.document;
|
||||
let contents = document.getText();
|
||||
let selection = editor.selection;
|
||||
let offset = document.offsetAt(selection.anchor);
|
||||
|
||||
let script = findScriptAtPosition(contents, offset);
|
||||
if (script) {
|
||||
runScript(script, document);
|
||||
} else {
|
||||
let message = localize('noScriptFound', 'Could not find an npm script at the selection.');
|
||||
vscode.window.showErrorMessage(message);
|
||||
}
|
||||
}
|
|
@ -10,6 +10,7 @@ import { addJSONProviders } from './features/jsonContributions';
|
|||
import { NpmScriptsTreeDataProvider } from './npmView';
|
||||
import { invalidateTasksCache, NpmTaskProvider } from './tasks';
|
||||
import { invalidateHoverScriptsCache, NpmScriptHoverProvider } from './scriptHover';
|
||||
import { runSelectedScript } from './commands';
|
||||
|
||||
export async function activate(context: vscode.ExtensionContext): Promise<void> {
|
||||
const taskProvider = registerTaskProvider(context);
|
||||
|
@ -37,7 +38,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
|
|||
invalidateHoverScriptsCache(e.document);
|
||||
});
|
||||
context.subscriptions.push(d);
|
||||
|
||||
context.subscriptions.push(vscode.commands.registerCommand('npm.runSelectedScript', runSelectedScript));
|
||||
context.subscriptions.push(addJSONProviders(httpRequest.xhr));
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
import {
|
||||
TaskDefinition, Task, TaskGroup, WorkspaceFolder, RelativePattern, ShellExecution, Uri, workspace,
|
||||
DebugConfiguration, debug, TaskProvider, ExtensionContext
|
||||
DebugConfiguration, debug, TaskProvider, ExtensionContext, TextDocument, tasks
|
||||
} from 'vscode';
|
||||
import * as path from 'path';
|
||||
import * as fs from 'fs';
|
||||
|
@ -288,6 +288,15 @@ async function readFile(file: string): Promise<string> {
|
|||
});
|
||||
}
|
||||
|
||||
export function runScript(script: string, document: TextDocument) {
|
||||
let uri = document.uri;
|
||||
let folder = workspace.getWorkspaceFolder(uri);
|
||||
if (folder) {
|
||||
let task = createTask(script, `run ${script}`, folder, uri);
|
||||
tasks.executeTask(task);
|
||||
}
|
||||
}
|
||||
|
||||
export function extractDebugArgFromScript(scriptValue: string): [string, number] | undefined {
|
||||
// matches --debug, --debug=1234, --debug-brk, debug-brk=1234, --inspect,
|
||||
// --inspect=1234, --inspect-brk, --inspect-brk=1234,
|
||||
|
@ -405,6 +414,42 @@ export function findAllScriptRanges(buffer: string): Map<string, [number, number
|
|||
return scripts;
|
||||
}
|
||||
|
||||
export function findScriptAtPosition(buffer: string, offset: number): string | undefined {
|
||||
let script: string | undefined = undefined;
|
||||
let inScripts = false;
|
||||
let scriptStart: number | undefined;
|
||||
let visitor: JSONVisitor = {
|
||||
onError(_error: ParseErrorCode, _offset: number, _length: number) {
|
||||
},
|
||||
onObjectEnd() {
|
||||
if (inScripts) {
|
||||
inScripts = false;
|
||||
scriptStart = undefined;
|
||||
}
|
||||
},
|
||||
onLiteralValue(value: any, nodeOffset: number, nodeLength: number) {
|
||||
if (inScripts && scriptStart) {
|
||||
if (offset >= scriptStart && offset < nodeOffset + nodeLength) {
|
||||
// found the script
|
||||
inScripts = false;
|
||||
} else {
|
||||
script = undefined;
|
||||
}
|
||||
}
|
||||
},
|
||||
onObjectProperty(property: string, nodeOffset: number, nodeLength: number) {
|
||||
if (property === 'scripts') {
|
||||
inScripts = true;
|
||||
}
|
||||
else if (inScripts) {
|
||||
scriptStart = nodeOffset;
|
||||
script = property;
|
||||
}
|
||||
}
|
||||
};
|
||||
visit(buffer, visitor);
|
||||
return script;
|
||||
}
|
||||
|
||||
export async function getScripts(packageJsonUri: Uri): Promise<StringMap | undefined> {
|
||||
|
||||
|
|
Loading…
Reference in a new issue