From adfe96397b5950d49e60b707f95613a935e523f3 Mon Sep 17 00:00:00 2001 From: Erich Gamma Date: Sun, 15 Apr 2018 23:06:33 +0200 Subject: [PATCH] only show the view when there are package.json files --- extensions/npm/package.json | 5 +++-- extensions/npm/src/main.ts | 8 ++++++-- extensions/npm/src/tasks.ts | 22 ++++++++++++++++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/extensions/npm/package.json b/extensions/npm/package.json index 91e8e3e0303..e0a9a124f32 100644 --- a/extensions/npm/package.json +++ b/extensions/npm/package.json @@ -30,7 +30,8 @@ "activationEvents": [ "onCommand:workbench.action.tasks.runTask", "onLanguage:json", - "onView:npm" + "onView:npm", + "workspaceContains:**/package.json" ], "contributes": { "views": { @@ -38,7 +39,7 @@ { "id": "npm", "name": "%view.name%", - "when": "config.npm.explorerVisible" + "when": "hasNpmScripts" } ] }, diff --git a/extensions/npm/src/main.ts b/extensions/npm/src/main.ts index 1cce17efab8..2035766ba8f 100644 --- a/extensions/npm/src/main.ts +++ b/extensions/npm/src/main.ts @@ -12,11 +12,11 @@ const localize = nls.loadMessageBundle(); import { addJSONProviders } from './features/jsonContributions'; import { NpmScriptsTreeDataProvider } from './npmView'; -import { provideNpmScripts } from './tasks'; +import { provideNpmScripts, hasNpmScripts } from './tasks'; let taskProvider: vscode.Disposable | undefined; -export function activate(context: vscode.ExtensionContext): void { +export async function activate(context: vscode.ExtensionContext): Promise { if (vscode.workspace.workspaceFolders) { let provider: vscode.TaskProvider = { @@ -30,6 +30,10 @@ export function activate(context: vscode.ExtensionContext): void { taskProvider = vscode.workspace.registerTaskProvider('npm', provider); let treeDataProvider = vscode.window.registerTreeDataProvider('npm', new NpmScriptsTreeDataProvider(context, provider, localize)); context.subscriptions.push(treeDataProvider); + + if (await hasNpmScripts()) { + vscode.commands.executeCommand('setContext', 'hasNpmScripts', true); + } } configureHttpRequest(); diff --git a/extensions/npm/src/tasks.ts b/extensions/npm/src/tasks.ts index 01a9bcb3346..20036750c13 100644 --- a/extensions/npm/src/tasks.ts +++ b/extensions/npm/src/tasks.ts @@ -48,6 +48,28 @@ export function getPackageManager(folder: WorkspaceFolder): string { return workspace.getConfiguration('npm', folder.uri).get('packageManager', 'npm'); } +export async function hasNpmScripts(): Promise { + let folders = workspace.workspaceFolders; + if (!folders) { + return false; + } + try { + for (let i = 0; i < folders.length; i++) { + let folder = folders[i]; + if (isEnabled(folder)) { + let relativePattern = new RelativePattern(folder, '**/package.json'); + let paths = await workspace.findFiles(relativePattern, '**/node_modules/**'); + if (paths.length > 0) { + return true; + } + } + } + return false; + } catch (error) { + return Promise.reject(error); + } +} + export async function provideNpmScripts(localize: any): Promise { let emptyTasks: Task[] = []; let allTasks: Task[] = [];