Skip to content
Closed
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
89 changes: 47 additions & 42 deletions packages/create-vite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -870,48 +870,53 @@ 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-'
}
// Deno uses `run -A npm:create-` instead of `create` or `init` to also provide needed perms
if (pkgManager === 'deno') {
return 'deno run -A npm:create-'
}
// pnpm doesn't support the -- syntax
if (pkgManager === 'pnpm') {
return 'pnpm create '
}
// For other package managers, preserve the original format
return customCommand.startsWith('npm create -- ')
? `${pkgManager} create -- `
: `${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 '
}
if (pkgManager === 'deno') {
return 'deno run -A npm:'
}
// Use `npm exec` in all other cases,
// including Yarn 1.x and other custom npm clients.
return 'npm exec '
})
)
let command = 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-'
}
// Deno uses `run -A npm:create-` instead of `create` or `init` to also provide needed perms
if (pkgManager === 'deno') {
return 'deno run -A npm:create-'
}
// pnpm doesn't support the -- syntax
if (pkgManager === 'pnpm') {
return 'pnpm create '
}
// For other package managers, preserve the original format
return customCommand.startsWith('npm create -- ')
? `${pkgManager} create -- `
: `${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 '
}
if (pkgManager === 'deno') {
return 'deno run -A npm:'
}
// Use `npm exec` in all other cases,
// including Yarn 1.x and other custom npm clients.
return 'npm exec '
})

if (pkgManager === 'pnpm') {
// `npm exec <pkg> -- <args>` uses `--` to split npm flags from package args. `pnpm dlx` doesn't need this separator, and keeping it can break CLIs.
command = command.replace(/^pnpm dlx (\S+) -- /, 'pnpm dlx $1 ')
}
Comment on lines +914 to +917
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we handle this inside .replace(/^npm exec /, () => { like npm create does?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored the code to fit within the .replace() function like npm create does.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the new PR #21648
With the proposed changes!


return command
}

function getLabel(variant: FrameworkVariant) {
Expand Down