generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add network diagnostic check (#828)
* feat: add network diagnostic check * docs: update ping check message * chore: add custom registry check too * refactor: without undefined, defers npm run * refactor: change conditional * refactor: env, env, or config * chore: update output with env/config, npm ping suggestion --------- Co-authored-by: mshanemc <[email protected]>
- Loading branch information
1 parent
97d3b9a
commit 4577086
Showing
4 changed files
with
245 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright (c) 2022, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
import { SfDoctor } from '@salesforce/plugin-info'; | ||
import { Lifecycle } from '@salesforce/core'; | ||
import { NpmModule } from '../shared/npmCommand.js'; | ||
type HookFunction = (options: { doctor: SfDoctor }) => Promise<[void]>; | ||
export const hook: HookFunction = (options) => Promise.all([registryCheck(options)]); | ||
|
||
const registryCheck = async (options: { doctor: SfDoctor }): Promise<void> => { | ||
const pluginName = '@salesforce/plugin-trust'; | ||
// find npm install | ||
const npm = new NpmModule(''); | ||
const env = process.env.npm_config_registry ?? process.env.NPM_CONFIG_REGISTRY; | ||
if (env) { | ||
options.doctor.addSuggestion(`using npm registry ${env} from environment variable`); | ||
} | ||
|
||
const config = npm.run('config get registry').stdout.trim(); | ||
if (config) { | ||
options.doctor.addSuggestion(`using npm registry ${config} from npm config`); | ||
} | ||
|
||
await Promise.all( | ||
[ | ||
...new Set([ | ||
// npm and yarn registries | ||
'https://registry.npmjs.org', | ||
'https://registry.yarnpkg.com', | ||
env ?? config, | ||
]), | ||
] | ||
// 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; | ||
} | ||
await Lifecycle.getInstance().emit('Doctor:diagnostic', { | ||
testName: `[${pluginName}] can ping: ${url}`, | ||
status: 'pass', | ||
}); | ||
} catch (e) { | ||
await Lifecycle.getInstance().emit('Doctor:diagnostic', { | ||
testName: `[${pluginName}] can't ping: ${url}`, | ||
status: 'fail', | ||
}); | ||
options.doctor.addSuggestion( | ||
`Cannot ping ${url} - potential network configuration error, check proxies, firewalls, environment variables. Verify this by running 'npm ping ${url}'` | ||
); | ||
} | ||
}) | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.