Skip to content

Commit

Permalink
chore: add custom registry check too
Browse files Browse the repository at this point in the history
  • Loading branch information
WillieRuemmele committed May 22, 2024
1 parent 14f086d commit b5bca46
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
44 changes: 26 additions & 18 deletions src/hooks/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
// 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`
);
}
});
});
};
6 changes: 5 additions & 1 deletion src/shared/npmCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit b5bca46

Please sign in to comment.