Skip to content
Draft
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -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 <version>
◼ Directory: Using app as project directory
Expand Down
23 changes: 1 addition & 22 deletions packages/cli/src/create/__tests__/command.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@ vi.mock('../../../binding/index.js', () => ({
runCommand: mockRunCommand,
}));

const { formatDlxCommand, runCommandAndDetectProjectDir } = await import('../command.js');

type DlxWorkspaceInfo = Parameters<typeof formatDlxCommand>[2];

const yarn = (packageManagerVersion: string) =>
({ packageManager: 'yarn', packageManagerVersion }) as DlxWorkspaceInfo;
const { runCommandAndDetectProjectDir } = await import('../command.js');

const tempDirs: string[] = [];

Expand All @@ -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();
Expand Down
41 changes: 0 additions & 41 deletions packages/cli/src/create/command.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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';
Expand Down
25 changes: 14 additions & 11 deletions packages/cli/src/create/templates/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -85,12 +81,19 @@ export async function runRemoteTemplateCommand(
silent = false,
): Promise<ExecutionWithProjectDir> {
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];

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve the npm 6 fallback when delegating templates to dlx

With VP_PACKAGE_MANAGER=npm@6.14.18, both local and global creation now reach Npm::resolve in crates/vp_pm_cli/src/resolution/commands/dlx.rs, which unconditionally constructs npm exec --yes. npm 6 has npx but no exec subcommand; npm's compatibility documentation dates the rewrite to v7.0.0 and explains that “npx uses the npm exec command instead of a separate argument parser and install process.” Consequently remote scaffolding no longer runs the generator with this supported package-manager version, whereas the removed runner invoked npx. Add a version-gated npx fallback to the shared dlx resolver.

AGENTS.md reference: AGENTS.md:L9-L9

Useful? React with 👍 / 👎.

// `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(
Expand Down
Loading