Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions packages/cli/src/tools/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,25 @@ export function isProjectUsingNpm(cwd: string) {
return findUp.sync('package-lock.json', {cwd});
}

const registry = getNpmRegistryUrl();
export const getNpmRegistryUrl = (() => {
// Lazily resolve npm registry url since it is only needed when initializing a
// new project.
let registryUrl = '';
return () => {
if (!registryUrl) {
try {
registryUrl = execSync(
'npm config get registry --workspaces=false --include-workspace-root',
)
.toString()
.trim();
} catch {
registryUrl = 'https://registry.npmjs.org/';
}
}
return registryUrl;
};
})();

/**
* Convert an npm tag to a concrete version, for example:
Expand All @@ -40,7 +58,7 @@ export async function npmResolveConcreteVersion(
packageName: string,
tagOrVersion: string,
): Promise<string> {
const url = new URL(registry);
const url = new URL(getNpmRegistryUrl());
url.pathname = `${packageName}/${tagOrVersion}`;
const resp = await fetch(url);
if (
Expand All @@ -58,11 +76,3 @@ export async function npmResolveConcreteVersion(
const json: any = await resp.json();
return json.version;
}

export function getNpmRegistryUrl(): string {
try {
return execSync('npm config get registry').toString().trim();
} catch {
return 'https://registry.npmjs.org/';
}
}