diff --git a/src/hooks/diagnostics.ts b/src/hooks/diagnostics.ts index 22c43ea6..7ec5b9c1 100644 --- a/src/hooks/diagnostics.ts +++ b/src/hooks/diagnostics.ts @@ -12,29 +12,37 @@ export const hook: HookFunction = (options) => Promise.all([registryCheck(option // eslint-disable-next-line @typescript-eslint/require-await const registryCheck = async (options: { doctor: SfDoctor }): Promise => { + // find npm install + const npm = new NpmModule(''); + const config = npm.run('config get registry').stdout.trim(); + const customRegistry = + process.env.npm_config_registry ?? process.env.NPM_CONFIG_REGISTRY ?? config !== 'https://registry.npmjs.org/' + ? config + : undefined; // eslint-disable-next-line @typescript-eslint/no-floating-promises [ // npm and yarn registries 'https://registry.npmjs.org', 'https://registry.yarnpkg.com', - ].map(async (url) => { - try { - // find npm install - const module = new NpmModule(''); + customRegistry, + ] + // incase customRegistry is undefined, prevent printing an extra line + .filter((u) => u) + .map(async (url) => { + try { + const results = npm.ping(url); - const results = module.ping(url); - - // timeout after 5000ms, error - if (!results || results.time > 5000) { - // to trigger the catch/fail below - throw Error; + // timeout after 5000ms, error + if (!results || results.time > 5000) { + // to trigger the catch/fail below + throw Error; + } + await Lifecycle.getInstance().emit('Doctor:diagnostic', { testName: `can access: ${url}`, status: 'pass' }); + } catch (e) { + await Lifecycle.getInstance().emit('Doctor:diagnostic', { testName: `can't access: ${url}`, status: 'fail' }); + options.doctor.addSuggestion( + `Cannot reach ${url} - potential network configuration error, check proxies, firewalls, environment variables` + ); } - await Lifecycle.getInstance().emit('Doctor:diagnostic', { testName: `can access: ${url}`, status: 'pass' }); - } catch (e) { - await Lifecycle.getInstance().emit('Doctor:diagnostic', { testName: `can't access: ${url}`, status: 'fail' }); - options.doctor.addSuggestion( - `Cannot reach ${url} - potential network configuration error, check proxies, firewalls, environment variables` - ); - } - }); + }); }; diff --git a/src/shared/npmCommand.ts b/src/shared/npmCommand.ts index b38d85d7..ae5bf9fa 100644 --- a/src/shared/npmCommand.ts +++ b/src/shared/npmCommand.ts @@ -11,7 +11,7 @@ import { createRequire } from 'node:module'; import fs from 'node:fs'; import npmRunPath from 'npm-run-path'; -import shelljs from 'shelljs'; +import shelljs, { ShellString } from 'shelljs'; import { SfError } from '@salesforce/core'; import { sleep, parseJson } from '@salesforce/kit'; import { Ux } from '@salesforce/sf-plugins-core'; @@ -165,6 +165,10 @@ export class NpmModule { }; } + public run(command: string): ShellString { + return NpmCommand.runNpmCmd(command, { cliRoot: this.cliRoot, json: command.includes('--json') }); + } + public show(registry: string): NpmShowResults { const showCmd = NpmCommand.runNpmCmd(`show ${this.module}@${this.version} --json`, { registry,