mirror of
https://github.com/microsoft/vscode.git
synced 2026-05-08 09:08:48 +01:00
add setting to define whether pre or post scripts should be filtered
This commit is contained in:
@@ -15,3 +15,4 @@ For more information about auto detection of Tasks pls see the [documentation](h
|
|||||||
- `npm.packageManager` the package manager used to run the scripts: `npm` or `yarn`, the default is `npm`.
|
- `npm.packageManager` the package manager used to run the scripts: `npm` or `yarn`, the default is `npm`.
|
||||||
- `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.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.enableScriptExplorer` enable an explorer view for npm scripts.
|
||||||
|
- `npm.filterPrePostScripts` filter pre or post scripts, the default is `true`.
|
||||||
|
|||||||
@@ -151,6 +151,12 @@
|
|||||||
"default": false,
|
"default": false,
|
||||||
"scope": "resource",
|
"scope": "resource",
|
||||||
"description": "%config.npm.enableScriptExplorer%"
|
"description": "%config.npm.enableScriptExplorer%"
|
||||||
|
},
|
||||||
|
"npm.filterPrePostScripts": {
|
||||||
|
"type": "boolean",
|
||||||
|
"default": true,
|
||||||
|
"scope": "resource",
|
||||||
|
"description": "%config.npm.filterPrePostScripts%"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
"config.npm.packageManager": "The package manager used to run scripts.",
|
"config.npm.packageManager": "The package manager used to run scripts.",
|
||||||
"config.npm.exclude": "Configure glob patterns for folders that should be excluded from automatic script detection.",
|
"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.",
|
"config.npm.enableScriptExplorer": "Enable an explorer view for npm scripts.",
|
||||||
|
"config.npm.filterPrePostScripts": "Filter pre- and postscripts. Default is true.",
|
||||||
"npm.parseError": "Npm task detection: failed to parse the file {0}",
|
"npm.parseError": "Npm task detection: failed to parse the file {0}",
|
||||||
"taskdef.script": "The npm script to customize.",
|
"taskdef.script": "The npm script to customize.",
|
||||||
"taskdef.path": "The path to the folder of the package.json file that provides the script. Can be omitted.",
|
"taskdef.path": "The path to the folder of the package.json file that provides the script. Can be omitted.",
|
||||||
|
|||||||
+19
-16
@@ -9,37 +9,37 @@ import * as vscode from 'vscode';
|
|||||||
|
|
||||||
import { addJSONProviders } from './features/jsonContributions';
|
import { addJSONProviders } from './features/jsonContributions';
|
||||||
import { NpmScriptsTreeDataProvider } from './npmView';
|
import { NpmScriptsTreeDataProvider } from './npmView';
|
||||||
import { provideNpmScripts } from './tasks';
|
import { provideNpmScripts, invalidateScriptsCache } from './tasks';
|
||||||
|
|
||||||
let taskProvider: vscode.Disposable | undefined;
|
let taskProvider: vscode.Disposable | undefined;
|
||||||
|
|
||||||
export async function activate(context: vscode.ExtensionContext): Promise<void> {
|
export async function activate(context: vscode.ExtensionContext): Promise<void> {
|
||||||
taskProvider = registerTaskProvider(context);
|
taskProvider = registerTaskProvider(context);
|
||||||
registerExplorer(context);
|
let treeDataProvider = registerExplorer(context);
|
||||||
configureHttpRequest();
|
configureHttpRequest();
|
||||||
vscode.workspace.onDidChangeConfiguration(() => {
|
vscode.workspace.onDidChangeConfiguration((e) => {
|
||||||
configureHttpRequest();
|
configureHttpRequest();
|
||||||
|
if (e.affectsConfiguration('npm.filterPrePostScripts')) {
|
||||||
|
invalidateScriptsCache();
|
||||||
|
if (treeDataProvider) {
|
||||||
|
treeDataProvider.refresh();
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
context.subscriptions.push(addJSONProviders(httpRequest.xhr));
|
context.subscriptions.push(addJSONProviders(httpRequest.xhr));
|
||||||
}
|
}
|
||||||
|
|
||||||
function registerTaskProvider(context: vscode.ExtensionContext): vscode.Disposable | undefined {
|
function registerTaskProvider(context: vscode.ExtensionContext): vscode.Disposable | undefined {
|
||||||
if (vscode.workspace.workspaceFolders) {
|
if (vscode.workspace.workspaceFolders) {
|
||||||
let cachedTasks: vscode.Task[] | undefined = undefined;
|
|
||||||
|
|
||||||
let flushCache = () => cachedTasks = undefined;
|
|
||||||
let watcher = vscode.workspace.createFileSystemWatcher('**/package.json');
|
let watcher = vscode.workspace.createFileSystemWatcher('**/package.json');
|
||||||
watcher.onDidChange((_e) => flushCache());
|
watcher.onDidChange((_e) => invalidateScriptsCache());
|
||||||
watcher.onDidDelete((_e) => flushCache());
|
watcher.onDidDelete((_e) => invalidateScriptsCache());
|
||||||
watcher.onDidCreate((_e) => flushCache());
|
watcher.onDidCreate((_e) => invalidateScriptsCache());
|
||||||
context.subscriptions.push(watcher);
|
context.subscriptions.push(watcher);
|
||||||
|
|
||||||
let provider: vscode.TaskProvider = {
|
let provider: vscode.TaskProvider = {
|
||||||
provideTasks: async () => {
|
provideTasks: async () => {
|
||||||
if (!cachedTasks) {
|
return provideNpmScripts();
|
||||||
cachedTasks = await provideNpmScripts();
|
|
||||||
}
|
|
||||||
return cachedTasks;
|
|
||||||
},
|
},
|
||||||
resolveTask(_task: vscode.Task): vscode.Task | undefined {
|
resolveTask(_task: vscode.Task): vscode.Task | undefined {
|
||||||
return undefined;
|
return undefined;
|
||||||
@@ -50,11 +50,14 @@ function registerTaskProvider(context: vscode.ExtensionContext): vscode.Disposab
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function registerExplorer(context: vscode.ExtensionContext) {
|
function registerExplorer(context: vscode.ExtensionContext): NpmScriptsTreeDataProvider | undefined {
|
||||||
if (vscode.workspace.workspaceFolders) {
|
if (vscode.workspace.workspaceFolders) {
|
||||||
let treeDataProvider = vscode.window.registerTreeDataProvider('npm', new NpmScriptsTreeDataProvider(context));
|
let treeDataProvider = new NpmScriptsTreeDataProvider(context);
|
||||||
context.subscriptions.push(treeDataProvider);
|
let disposable = vscode.window.registerTreeDataProvider('npm', treeDataProvider);
|
||||||
|
context.subscriptions.push(disposable);
|
||||||
|
return treeDataProvider;
|
||||||
}
|
}
|
||||||
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
function configureHttpRequest() {
|
function configureHttpRequest() {
|
||||||
|
|||||||
@@ -251,7 +251,7 @@ export class NpmScriptsTreeDataProvider implements TreeDataProvider<TreeItem> {
|
|||||||
await window.showTextDocument(document, { selection: new Selection(position, position) });
|
await window.showTextDocument(document, { selection: new Selection(position, position) });
|
||||||
}
|
}
|
||||||
|
|
||||||
private refresh() {
|
public refresh() {
|
||||||
this.taskTree = null;
|
this.taskTree = null;
|
||||||
this._onDidChangeTreeData.fire();
|
this._onDidChangeTreeData.fire();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,12 @@ export interface NpmTaskDefinition extends TaskDefinition {
|
|||||||
|
|
||||||
type AutoDetect = 'on' | 'off';
|
type AutoDetect = 'on' | 'off';
|
||||||
|
|
||||||
|
let cachedTasks: Task[] | undefined = undefined;
|
||||||
|
|
||||||
|
export function invalidateScriptsCache() {
|
||||||
|
cachedTasks = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
const buildNames: string[] = ['build', 'compile', 'watch'];
|
const buildNames: string[] = ['build', 'compile', 'watch'];
|
||||||
function isBuildTask(name: string): boolean {
|
function isBuildTask(name: string): boolean {
|
||||||
for (let buildName of buildNames) {
|
for (let buildName of buildNames) {
|
||||||
@@ -40,8 +46,24 @@ function isTestTask(name: string): boolean {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isNotPreOrPostScript(script: string): boolean {
|
function getPrePostScripts(scripts: any): Set<string> {
|
||||||
return !(script.startsWith('pre') || script.startsWith('post'));
|
const prePostScripts: Set<string> = new Set([
|
||||||
|
'preuninstall', 'postuninstall', 'prepack', 'postpack', 'preinstall', 'postinstall',
|
||||||
|
'prepack', 'postpack', 'prepublish', 'postpublish', 'preversion', 'postversion',
|
||||||
|
'prestop', 'poststop', 'prerestart', 'postrestart', 'preshrinkwrap', 'postshrinkwrap',
|
||||||
|
'pretest', 'postest', 'prepublishOnly'
|
||||||
|
]);
|
||||||
|
let keys = Object.keys(scripts);
|
||||||
|
for (let i = 0; i < keys.length; i++) {
|
||||||
|
const script = keys[i];
|
||||||
|
const prepost = ['pre' + script, 'post' + script];
|
||||||
|
prepost.forEach(each => {
|
||||||
|
if (scripts[each]) {
|
||||||
|
prePostScripts.add(each);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return prePostScripts;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isWorkspaceFolder(value: any): value is WorkspaceFolder {
|
export function isWorkspaceFolder(value: any): value is WorkspaceFolder {
|
||||||
@@ -74,7 +96,8 @@ export async function hasNpmScripts(): Promise<boolean> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function provideNpmScripts(): Promise<Task[]> {
|
async function detectNpmScripts(): Promise<Task[]> {
|
||||||
|
|
||||||
let emptyTasks: Task[] = [];
|
let emptyTasks: Task[] = [];
|
||||||
let allTasks: Task[] = [];
|
let allTasks: Task[] = [];
|
||||||
|
|
||||||
@@ -102,6 +125,13 @@ export async function provideNpmScripts(): Promise<Task[]> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function provideNpmScripts(): Promise<Task[]> {
|
||||||
|
if (!cachedTasks) {
|
||||||
|
cachedTasks = await detectNpmScripts();
|
||||||
|
}
|
||||||
|
return cachedTasks;
|
||||||
|
}
|
||||||
|
|
||||||
function isAutoDetectionEnabled(folder: WorkspaceFolder): boolean {
|
function isAutoDetectionEnabled(folder: WorkspaceFolder): boolean {
|
||||||
return workspace.getConfiguration('npm', folder.uri).get<AutoDetect>('autoDetect') === 'on';
|
return workspace.getConfiguration('npm', folder.uri).get<AutoDetect>('autoDetect') === 'on';
|
||||||
}
|
}
|
||||||
@@ -140,7 +170,10 @@ async function provideNpmScriptsForFolder(packageJsonUri: Uri): Promise<Task[]>
|
|||||||
}
|
}
|
||||||
|
|
||||||
const result: Task[] = [];
|
const result: Task[] = [];
|
||||||
Object.keys(scripts).filter(isNotPreOrPostScript).forEach(each => {
|
|
||||||
|
const filterPrePost = workspace.getConfiguration('npm', folder.uri).get<boolean>('filterPrePostScripts');
|
||||||
|
const prePostScripts = filterPrePost ? getPrePostScripts(scripts) : new Set<String>();
|
||||||
|
Object.keys(scripts).filter(each => !prePostScripts.has(each)).forEach(each => {
|
||||||
const task = createTask(each, `run ${each}`, folder!, packageJsonUri);
|
const task = createTask(each, `run ${each}`, folder!, packageJsonUri);
|
||||||
const lowerCaseTaskName = each.toLowerCase();
|
const lowerCaseTaskName = each.toLowerCase();
|
||||||
if (isBuildTask(lowerCaseTaskName)) {
|
if (isBuildTask(lowerCaseTaskName)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user