diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/create_react_router_no_git_init/snapshots/create_react_router_no_git_init.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/create_react_router_no_git_init/snapshots/create_react_router_no_git_init.md index fc57206bc7..6ea7c2302d 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/create_react_router_no_git_init/snapshots/create_react_router_no_git_init.md +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/create_react_router_no_git_init/snapshots/create_react_router_no_git_init.md @@ -10,7 +10,7 @@ Vite+ passes --no-git-init to the generator and completes integration ● pnpm@10.18.0 installing... ● pnpm@10.18.0 installed ◇ Generating project… -● Running: pnpm dlx create-react-router@8.5.1 app --template ./template --yes --no-install --no-agent-skills --no-motion --no-git-init +● Running: vp dlx -- create-react-router@8.5.1 app --template ./template --yes --no-install --no-agent-skills --no-motion --no-git-init create-react-router ◼ Directory: Using app as project directory diff --git a/packages/cli/src/create/__tests__/command.spec.ts b/packages/cli/src/create/__tests__/command.spec.ts index 607aeecfac..89c9fdb05e 100644 --- a/packages/cli/src/create/__tests__/command.spec.ts +++ b/packages/cli/src/create/__tests__/command.spec.ts @@ -12,12 +12,7 @@ vi.mock('../../../binding/index.js', () => ({ runCommand: mockRunCommand, })); -const { formatDlxCommand, runCommandAndDetectProjectDir } = await import('../command.js'); - -type DlxWorkspaceInfo = Parameters[2]; - -const yarn = (packageManagerVersion: string) => - ({ packageManager: 'yarn', packageManagerVersion }) as DlxWorkspaceInfo; +const { runCommandAndDetectProjectDir } = await import('../command.js'); const tempDirs: string[] = []; @@ -27,22 +22,6 @@ function makeTempDir() { return dir; } -describe('formatDlxCommand', () => { - it('falls back to npx for Yarn 1, which has no dlx command', () => { - expect(formatDlxCommand('create-vite@latest', ['app'], yarn('1.22.22'))).toEqual({ - command: 'npx', - args: ['--yes', 'create-vite@latest', 'app'], - }); - }); - - it('uses yarn dlx for Yarn 2 and later', () => { - expect(formatDlxCommand('create-vite@latest', ['app'], yarn('4.18.1'))).toEqual({ - command: 'yarn', - args: ['dlx', 'create-vite@latest', 'app'], - }); - }); -}); - describe('runCommandAndDetectProjectDir', () => { beforeEach(() => { mockRunCommand.mockReset(); diff --git a/packages/cli/src/create/command.ts b/packages/cli/src/create/command.ts index 9f21db5192..85fcb39879 100644 --- a/packages/cli/src/create/command.ts +++ b/packages/cli/src/create/command.ts @@ -1,10 +1,7 @@ import fs from 'node:fs/promises'; import path from 'node:path'; -import { coerce } from 'verkit'; - import { runCommand as runCommandWithFspy } from '../../binding/index.js'; -import type { WorkspaceInfo } from '../types/index.ts'; import type { ExecutionResult, RunCommandOptions } from '../utils/command.ts'; /** Set by `runCommandAndDetectProjectDir` and the template executors @@ -89,44 +86,6 @@ export async function runCommandAndDetectProjectDir( }; } -// Get the package runner command for each package manager -export function getPackageRunner(workspaceInfo: WorkspaceInfo) { - switch (workspaceInfo.packageManager) { - case 'pnpm': - return { - command: 'pnpm', - args: ['dlx'], - }; - case 'yarn': - // Yarn 1 has no `dlx`; same npx fallback as `vp dlx` in vp_pm_cli. - if (coerce(workspaceInfo.packageManagerVersion)?.major === 1) { - return { command: 'npx', args: ['--yes'] }; - } - return { - command: 'yarn', - args: ['dlx'], - }; - case 'bun': - return { command: 'bun', args: ['x'] }; - case 'npm': - default: - return { command: 'npx', args: [] }; - } -} - -// TODO: will use `vp dlx` instead, see https://github.com/voidzero-dev/vite-task/issues/27 -export function formatDlxCommand( - packageName: string, - args: string[], - workspaceInfo: WorkspaceInfo, -) { - const runner = getPackageRunner(workspaceInfo); - return { - command: runner.command, - args: [...runner.args, packageName, ...args], - }; -} - export function prependToPathToEnvs(extraPath: string, envs: NodeJS.ProcessEnv) { const delimiter = path.delimiter; const pathKey = Object.keys(envs).find((key) => key.toLowerCase() === 'path') ?? 'PATH'; diff --git a/packages/cli/src/create/templates/remote.ts b/packages/cli/src/create/templates/remote.ts index 62c8c640a2..0f6904f927 100644 --- a/packages/cli/src/create/templates/remote.ts +++ b/packages/cli/src/create/templates/remote.ts @@ -2,13 +2,9 @@ import * as prompts from '@voidzero-dev/vite-plus-prompts'; import colors from 'picocolors'; import type { WorkspaceInfo } from '../../types/index.ts'; -import { runCommand, runCommandSilently } from '../../utils/command.ts'; +import { getVpCommand, runCommand, runCommandSilently } from '../../utils/command.ts'; import { checkNpmPackageExists } from '../../utils/package.ts'; -import { - type ExecutionWithProjectDir, - formatDlxCommand, - runCommandAndDetectProjectDir, -} from '../command.ts'; +import { type ExecutionWithProjectDir, runCommandAndDetectProjectDir } from '../command.ts'; import type { TemplateInfo } from './types.ts'; const { gray, yellow } = colors; @@ -85,12 +81,19 @@ export async function runRemoteTemplateCommand( silent = false, ): Promise { autoFixRemoteTemplateCommand(templateInfo, workspaceInfo); - const remotePackageName = templateInfo.command; - const execArgs = [...templateInfo.args]; - const envs = templateInfo.envs; - const { command, args } = formatDlxCommand(remotePackageName, execArgs, workspaceInfo); + // `--` keeps the global CLI from reading a template's `--help` as its own. + const dlxArgs = ['dlx', '--', templateInfo.command, ...templateInfo.args]; + // `vp dlx` detects the package manager from cwd and defaults to pnpm outside a project. + // A user override is kept as given: its hash selects the verified install create downloaded. + const envs = { + ...templateInfo.envs, + VP_PACKAGE_MANAGER: + templateInfo.envs.VP_PACKAGE_MANAGER?.trim() || + `${workspaceInfo.packageManager}@${workspaceInfo.packageManagerVersion}`, + }; + const { command, args } = getVpCommand(dlxArgs); if (!silent) { - prompts.log.info(`Running: ${gray(`${command} ${args.join(' ')}`)}`); + prompts.log.info(`Running: ${gray(`vp ${dlxArgs.join(' ')}`)}`); } if (detectCreatedProjectDir) { return await runCommandAndDetectProjectDir(