From dedaa795a77c546f8540ad1c3b51bb6e0f62531f Mon Sep 17 00:00:00 2001 From: bluwy Date: Tue, 7 Jan 2025 22:51:59 +0800 Subject: [PATCH 1/2] feat(create-vite): add hint for external CLIs --- packages/create-vite/src/index.ts | 79 +++++++++++++++++++------------ 1 file changed, 50 insertions(+), 29 deletions(-) diff --git a/packages/create-vite/src/index.ts b/packages/create-vite/src/index.ts index 1a0a628d6e2956..44630ce51a5703 100755 --- a/packages/create-vite/src/index.ts +++ b/packages/create-vite/src/index.ts @@ -10,6 +10,7 @@ const { blue, blueBright, cyan, + dim, green, greenBright, magenta, @@ -311,6 +312,7 @@ async function init() { } let targetDir = argTargetDir || defaultTargetDir + const pkgInfo = pkgFromUserAgent(process.env.npm_config_user_agent) const getProjectName = () => path.basename(path.resolve(targetDir)) let result: prompts.Answers< @@ -402,9 +404,16 @@ async function init() { choices: (framework: Framework) => framework.variants.map((variant) => { const variantColor = variant.color + const command = variant.customCommand + ? getFullCustomCommand(variant.customCommand, pkgInfo).replace( + / TARGET_DIR$/, + '', + ) + : undefined return { title: variantColor(variant.display || variant.name), value: variant.name, + description: command ? dim(command) : undefined, } }), }, @@ -439,40 +448,13 @@ async function init() { template = template.replace('-swc', '') } - const pkgInfo = pkgFromUserAgent(process.env.npm_config_user_agent) const pkgManager = pkgInfo ? pkgInfo.name : 'npm' - const isYarn1 = pkgManager === 'yarn' && pkgInfo?.version.startsWith('1.') const { customCommand } = FRAMEWORKS.flatMap((f) => f.variants).find((v) => v.name === template) ?? {} if (customCommand) { - const fullCustomCommand = customCommand - .replace(/^npm create /, () => { - // `bun create` uses it's own set of templates, - // the closest alternative is using `bun x` directly on the package - if (pkgManager === 'bun') { - return 'bun x create-' - } - return `${pkgManager} create ` - }) - // Only Yarn 1.x doesn't support `@version` in the `create` command - .replace('@latest', () => (isYarn1 ? '' : '@latest')) - .replace(/^npm exec/, () => { - // Prefer `pnpm dlx`, `yarn dlx`, or `bun x` - if (pkgManager === 'pnpm') { - return 'pnpm dlx' - } - if (pkgManager === 'yarn' && !isYarn1) { - return 'yarn dlx' - } - if (pkgManager === 'bun') { - return 'bun x' - } - // Use `npm exec` in all other cases, - // including Yarn 1.x and other custom npm clients. - return 'npm exec' - }) + const fullCustomCommand = getFullCustomCommand(customCommand, pkgInfo) const [command, ...args] = fullCustomCommand.split(' ') // we replace TARGET_DIR here because targetDir may include a space @@ -595,7 +577,12 @@ function emptyDir(dir: string) { } } -function pkgFromUserAgent(userAgent: string | undefined) { +interface PkgInfo { + name: string + version: string +} + +function pkgFromUserAgent(userAgent: string | undefined): PkgInfo | undefined { if (!userAgent) return undefined const pkgSpec = userAgent.split(' ')[0] const pkgSpecArr = pkgSpec.split('/') @@ -625,6 +612,40 @@ function editFile(file: string, callback: (content: string) => string) { fs.writeFileSync(file, callback(content), 'utf-8') } +function getFullCustomCommand(customCommand: string, pkgInfo?: PkgInfo) { + const pkgManager = pkgInfo ? pkgInfo.name : 'npm' + const isYarn1 = pkgManager === 'yarn' && pkgInfo?.version.startsWith('1.') + + return ( + customCommand + .replace(/^npm create /, () => { + // `bun create` uses it's own set of templates, + // the closest alternative is using `bun x` directly on the package + if (pkgManager === 'bun') { + return 'bun x create-' + } + return `${pkgManager} create ` + }) + // Only Yarn 1.x doesn't support `@version` in the `create` command + .replace('@latest', () => (isYarn1 ? '' : '@latest')) + .replace(/^npm exec/, () => { + // Prefer `pnpm dlx`, `yarn dlx`, or `bun x` + if (pkgManager === 'pnpm') { + return 'pnpm dlx' + } + if (pkgManager === 'yarn' && !isYarn1) { + return 'yarn dlx' + } + if (pkgManager === 'bun') { + return 'bun x' + } + // Use `npm exec` in all other cases, + // including Yarn 1.x and other custom npm clients. + return 'npm exec' + }) + ) +} + init().catch((e) => { console.error(e) }) From d9ac6700b925737f4df1a3ce87725edc727cae6d Mon Sep 17 00:00:00 2001 From: bluwy Date: Wed, 8 Jan 2025 00:23:31 +0800 Subject: [PATCH 2/2] chore: use gray --- packages/create-vite/src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/create-vite/src/index.ts b/packages/create-vite/src/index.ts index 44630ce51a5703..d3d827584cfcb5 100755 --- a/packages/create-vite/src/index.ts +++ b/packages/create-vite/src/index.ts @@ -10,7 +10,7 @@ const { blue, blueBright, cyan, - dim, + gray, green, greenBright, magenta, @@ -413,7 +413,7 @@ async function init() { return { title: variantColor(variant.display || variant.name), value: variant.name, - description: command ? dim(command) : undefined, + description: command ? gray(command) : undefined, } }), },