npm: use which (#121209)

This commit is contained in:
Martin Aeschlimann 2021-04-13 19:34:43 +02:00 committed by Alexandru Dima
parent 83f13c70ff
commit f0a344a76e
No known key found for this signature in database
GPG key ID: 6E58D7B045760DA0
7 changed files with 45 additions and 13 deletions

View file

@ -18,6 +18,7 @@ module.exports = withBrowserDefaults({
filename: 'npmBrowserMain.js'
},
node: {
'child_process': 'empty'
'child_process': 'empty',
'which': 'empty'
}
});

View file

@ -24,11 +24,13 @@
"minimatch": "^3.0.4",
"request-light": "^0.4.0",
"vscode-nls": "^4.1.1",
"which": "^2.0.2",
"which-pm": "^2.0.0"
},
"devDependencies": {
"@types/minimatch": "^3.0.3",
"@types/node": "^12.19.9"
"@types/node": "^12.19.9",
"@types/which": "^2.0.0"
},
"resolutions": {
"which-pm/load-yaml-file/**/argparse": "1.0.9"

View file

@ -29,8 +29,8 @@ export interface IJSONContribution {
resolveSuggestion?(resourceUri: Uri | undefined, item: CompletionItem): Thenable<CompletionItem | null> | null;
}
export function addJSONProviders(xhr: XHRRequest, canRunNPM: boolean): Disposable {
const contributions = [new PackageJSONContribution(xhr, canRunNPM), new BowerJSONContribution(xhr)];
export function addJSONProviders(xhr: XHRRequest, npmCommandPath: string | undefined): Disposable {
const contributions = [new PackageJSONContribution(xhr, npmCommandPath), new BowerJSONContribution(xhr)];
const subscriptions: Disposable[] = [];
contributions.forEach(contribution => {
const selector = contribution.getDocumentSelector();

View file

@ -32,7 +32,7 @@ export class PackageJSONContribution implements IJSONContribution {
return [{ language: 'json', scheme: '*', pattern: '**/package.json' }];
}
public constructor(private xhr: XHRRequest, private canRunNPM: boolean) {
public constructor(private xhr: XHRRequest, private npmCommandPath: string | undefined) {
}
public collectDefaultSuggestions(_resource: Uri, result: ISuggestionsCollector): Thenable<any> {
@ -52,7 +52,7 @@ export class PackageJSONContribution implements IJSONContribution {
}
private isEnabled() {
return this.canRunNPM || this.onlineEnabled();
return this.npmCommandPath || this.onlineEnabled();
}
private onlineEnabled() {
@ -268,8 +268,8 @@ export class PackageJSONContribution implements IJSONContribution {
return undefined; // avoid unnecessary lookups
}
let info: ViewPackageInfo | undefined;
if (this.canRunNPM) {
info = await this.npmView(pack, resource);
if (this.npmCommandPath) {
info = await this.npmView(this.npmCommandPath, pack, resource);
}
if (!info && this.onlineEnabled()) {
info = await this.npmjsView(pack);
@ -277,11 +277,11 @@ export class PackageJSONContribution implements IJSONContribution {
return info;
}
private npmView(pack: string, resource: Uri | undefined): Promise<ViewPackageInfo | undefined> {
private npmView(npmCommandPath: string, pack: string, resource: Uri | undefined): Promise<ViewPackageInfo | undefined> {
return new Promise((resolve, _reject) => {
const args = ['view', '--json', pack, 'description', 'dist-tags.latest', 'homepage', 'version'];
let cwd = resource && resource.scheme === 'file' ? dirname(resource.fsPath) : undefined;
cp.execFile(process.platform === 'win32' ? 'npm.cmd' : 'npm', args, { cwd }, (error, stdout) => {
cp.execFile(npmCommandPath, args, { cwd }, (error, stdout) => {
if (!error) {
try {
const content = JSON.parse(stdout);

View file

@ -8,7 +8,7 @@ import * as vscode from 'vscode';
import { addJSONProviders } from './features/jsonContributions';
export async function activate(context: vscode.ExtensionContext): Promise<void> {
context.subscriptions.push(addJSONProviders(httpRequest.xhr, false));
context.subscriptions.push(addJSONProviders(httpRequest.xhr, undefined));
}
export function deactivate(): void {

View file

@ -11,6 +11,7 @@ import { NpmScriptsTreeDataProvider } from './npmView';
import { getPackageManager, invalidateTasksCache, NpmTaskProvider, hasPackageJson } from './tasks';
import { invalidateHoverScriptsCache, NpmScriptHoverProvider } from './scriptHover';
import { NpmScriptLensProvider } from './npmScriptLens';
import * as which from 'which';
let treeDataProvider: NpmScriptsTreeDataProvider | undefined;
@ -30,8 +31,8 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
}
}));
const canRunNPM = canRunNpmInCurrentWorkspace();
context.subscriptions.push(addJSONProviders(httpRequest.xhr, canRunNPM));
const npmCommandPath = await getNPMCommandPath();
context.subscriptions.push(addJSONProviders(httpRequest.xhr, npmCommandPath));
registerTaskProvider(context);
treeDataProvider = registerExplorer(context);
@ -71,6 +72,17 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
context.subscriptions.push(new NpmScriptLensProvider());
}
async function getNPMCommandPath(): Promise<string | undefined> {
if (canRunNpmInCurrentWorkspace()) {
try {
return await which(process.platform === 'win32' ? 'npm.cmd' : 'npm');
} catch (e) {
return undefined;
}
}
return undefined;
}
function canRunNpmInCurrentWorkspace() {
if (vscode.workspace.workspaceFolders) {
return vscode.workspace.workspaceFolders.some(f => f.uri.scheme === 'file');

View file

@ -12,6 +12,11 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.19.9.tgz#990ad687ad8b26ef6dcc34a4f69c33d40c95b679"
integrity sha512-yj0DOaQeUrk3nJ0bd3Y5PeDRJ6W0r+kilosLA+dzF3dola/o9hxhMSg2sFvVcA2UHS5JSOsZp4S0c1OEXc4m1Q==
"@types/which@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@types/which/-/which-2.0.0.tgz#446d35586611dee657120de8e0457382a658fc25"
integrity sha512-JHTNOEpZnACQdsTojWggn+SQ8IucfqEhtz7g8Z0G67WdSj4x3F0X5I2c/CVcl8z/QukGrIHeQ/N49v1au74XFQ==
agent-base@4:
version "4.2.0"
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-4.2.0.tgz#9838b5c3392b962bad031e6a4c5e1024abec45ce"
@ -130,6 +135,11 @@ is-number@^7.0.0:
resolved "https://registry.yarnpkg.com/is-number/-/is-number-7.0.0.tgz#7535345b896734d5f80c4d06c50955527a14f12b"
integrity sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==
isexe@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"
integrity sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=
js-yaml@^3.13.0:
version "3.14.0"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.14.0.tgz#a7a34170f26a21bb162424d8adacb4113a69e482"
@ -257,3 +267,10 @@ which-pm@^2.0.0:
dependencies:
load-yaml-file "^0.2.0"
path-exists "^4.0.0"
which@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/which/-/which-2.0.2.tgz#7c6a8dd0a636a0327e10b59c9286eee93f3f51b1"
integrity sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==
dependencies:
isexe "^2.0.0"