Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持自定义 Registry #11024

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
41 changes: 38 additions & 3 deletions packages/create-umi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export default async ({
let pluginName = `umi-plugin-${name || 'demo'}`;

const target = name ? join(cwd, name) : cwd;
const customRegistry = await detectUserRegistry();

const { isCancel, text, select, intro, outro } = clackPrompts;
const exitPrompt = () => {
Expand Down Expand Up @@ -143,15 +144,20 @@ export default async ({
message: 'Pick Npm Registry',
options: [
{
label: 'npm',
label: 'Npm',
value: ERegistry.npm,
},
{
label: 'taobao',
label: 'Taobao',
value: ERegistry.taobao,
hint: 'recommended for China',
},
],
customRegistry && {
label: 'Your npm config',
value: customRegistry,
hint: customRegistry,
},
].filter(Boolean) as Parameters<typeof select>[0]['options'],
initialValue: ERegistry.npm,
})) as ERegistry;
};
Expand Down Expand Up @@ -383,3 +389,32 @@ async function getPnpmMajorVersion() {
throw new Error('Please install pnpm first', { cause: e });
}
}

async function detectUserRegistry() {
const isChinaRegistry = (registry: string) => {
return [
'registry.npm.taobao.org',
'registry.npmmirror.com',
'r.cnpmjs.org',
// What's this? https://cnodejs.org/topic/61405b76fe0c5109a7aea0ed
'registry.nlark.com',
].some((i) => registry.includes(i));
};
const isNpmRegistry = (registry: string) => {
return [
'registry.npmjs.org',
'registry.npmjs.com',
'registry.yarnpkg.com',
].some((i) => registry.includes(i));
};

try {
const { stdout } = await execa.execa('npm', ['config', 'get', 'registry']);
const registry = stdout.trim();
const hasCustomRegistry =
!isChinaRegistry(registry) && !isNpmRegistry(registry);
return hasCustomRegistry ? registry : undefined;
} catch {
return;
}
}
Loading