From 122d38d595c1652b9d0f82cccc285bd73f59d989 Mon Sep 17 00:00:00 2001 From: mshanemc Date: Thu, 23 May 2024 10:41:47 -0500 Subject: [PATCH 1/3] refactor: without undefined, defers npm run --- src/hooks/diagnostics.ts | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/hooks/diagnostics.ts b/src/hooks/diagnostics.ts index 7ec5b9c1..ed42038d 100644 --- a/src/hooks/diagnostics.ts +++ b/src/hooks/diagnostics.ts @@ -10,25 +10,22 @@ import { NpmModule } from '../shared/npmCommand.js'; type HookFunction = (options: { doctor: SfDoctor }) => Promise<[void]>; export const hook: HookFunction = (options) => Promise.all([registryCheck(options)]); -// 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', - customRegistry, - ] - // incase customRegistry is undefined, prevent printing an extra line - .filter((u) => u) - .map(async (url) => { + + await Promise.all( + [ + ...new Set([ + // npm and yarn registries + 'https://registry.npmjs.org', + 'https://registry.yarnpkg.com', + process.env.npm_config_registry ?? + process.env.NPM_CONFIG_REGISTRY ?? + npm.run('config get registry').stdout.trim() ?? + 'https://registry.npmjs.org', + ]), + ].map(async (url) => { try { const results = npm.ping(url); @@ -44,5 +41,6 @@ const registryCheck = async (options: { doctor: SfDoctor }): Promise => { `Cannot reach ${url} - potential network configuration error, check proxies, firewalls, environment variables` ); } - }); + }) + ); }; From 342bd84d788f81dafbce42b7567e91cbbfa07c97 Mon Sep 17 00:00:00 2001 From: mshanemc Date: Thu, 23 May 2024 10:55:14 -0500 Subject: [PATCH 2/3] refactor: change conditional --- src/hooks/diagnostics.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/hooks/diagnostics.ts b/src/hooks/diagnostics.ts index ed42038d..9f466e5c 100644 --- a/src/hooks/diagnostics.ts +++ b/src/hooks/diagnostics.ts @@ -13,7 +13,7 @@ export const hook: HookFunction = (options) => Promise.all([registryCheck(option const registryCheck = async (options: { doctor: SfDoctor }): Promise => { // find npm install const npm = new NpmModule(''); - + const config = npm.run('config get registry').stdout.trim(); await Promise.all( [ ...new Set([ @@ -21,9 +21,10 @@ const registryCheck = async (options: { doctor: SfDoctor }): Promise => { 'https://registry.npmjs.org', 'https://registry.yarnpkg.com', process.env.npm_config_registry ?? - process.env.NPM_CONFIG_REGISTRY ?? - npm.run('config get registry').stdout.trim() ?? - 'https://registry.npmjs.org', + process.env.NPM_CONFIG_REGISTRY ?? + npm.run('config get registry').stdout.trim() + ? config + : 'https://registry.npmjs.org', ]), ].map(async (url) => { try { From 62b1fef5f89aed0ac32f86bece0caa50edb0ebcd Mon Sep 17 00:00:00 2001 From: mshanemc Date: Thu, 23 May 2024 10:59:00 -0500 Subject: [PATCH 3/3] refactor: env, env, or config --- src/hooks/diagnostics.ts | 41 ++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/hooks/diagnostics.ts b/src/hooks/diagnostics.ts index 9f466e5c..d25e3f02 100644 --- a/src/hooks/diagnostics.ts +++ b/src/hooks/diagnostics.ts @@ -13,7 +13,7 @@ export const hook: HookFunction = (options) => Promise.all([registryCheck(option const registryCheck = async (options: { doctor: SfDoctor }): Promise => { // find npm install const npm = new NpmModule(''); - const config = npm.run('config get registry').stdout.trim(); + await Promise.all( [ ...new Set([ @@ -21,27 +21,28 @@ const registryCheck = async (options: { doctor: SfDoctor }): Promise => { 'https://registry.npmjs.org', 'https://registry.yarnpkg.com', process.env.npm_config_registry ?? - process.env.NPM_CONFIG_REGISTRY ?? - npm.run('config get registry').stdout.trim() - ? config - : 'https://registry.npmjs.org', + process.env.NPM_CONFIG_REGISTRY ?? + npm.run('config get registry').stdout.trim(), ]), - ].map(async (url) => { - try { - const results = npm.ping(url); + ] + // incase customRegistry is undefined, prevent printing an extra line + .filter((u) => u) + .map(async (url) => { + try { + const results = npm.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` - ); - } - }) + }) ); };