Skip to content
Merged
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
66 changes: 66 additions & 0 deletions packages/cli/src/create/__tests__/builtin.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { describe, expect, it, vi } from 'vitest';

import { executeBuiltinTemplate } from '../templates/builtin.js';

const { mockLogError } = vi.hoisted(() => ({ mockLogError: vi.fn() }));

vi.mock('../templates/remote.js', () => ({
runRemoteTemplateCommand: vi.fn(),
}));

vi.mock('@voidzero-dev/vite-plus-prompts', () => ({
log: { error: mockLogError },
}));

const workspaceInfo = {
rootDir: '/tmp/workspace',
} as any;

const baseTemplateInfo = {
packageName: 'wage-meeting',
targetDir: 'wage-meeting',
args: [],
envs: {},
type: 'builtin' as any,
interactive: false,
};

describe('executeBuiltinTemplate', () => {
it('returns exitCode 1 for unknown vite: template', async () => {
const { runRemoteTemplateCommand } = await import('../templates/remote.js');

const result = await executeBuiltinTemplate(workspaceInfo, {
...baseTemplateInfo,
command: 'vite:test',
});

expect(result.exitCode).toBe(1);
expect(runRemoteTemplateCommand).not.toHaveBeenCalled();
});

it('shows error message with template name and --list hint', async () => {
mockLogError.mockClear();

await executeBuiltinTemplate(workspaceInfo, {
...baseTemplateInfo,
command: 'vite:unknown',
});

expect(mockLogError).toHaveBeenCalledOnce();
const message = mockLogError.mock.calls[0][0] as string;
expect(message).toContain('vite:unknown');
expect(message).toContain('vp create --list');
});

it('does not show error message in silent mode', async () => {
mockLogError.mockClear();

await executeBuiltinTemplate(
workspaceInfo,
{ ...baseTemplateInfo, command: 'vite:test' },
{ silent: true },
);

expect(mockLogError).not.toHaveBeenCalled();
});
});
19 changes: 19 additions & 0 deletions packages/cli/src/create/templates/builtin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import assert from 'node:assert';
import path from 'node:path';

import * as prompts from '@voidzero-dev/vite-plus-prompts';
import colors from 'picocolors';

import type { WorkspaceInfo } from '../../types/index.js';
import type { ExecutionResult } from '../command.js';
import { discoverTemplate } from '../discovery.js';
Expand Down Expand Up @@ -41,11 +44,24 @@ export async function executeBuiltinTemplate(
false,
options?.silent ?? false,
);
if (result.exitCode !== 0) {
return { exitCode: result.exitCode };
}
const fullPath = path.join(workspaceInfo.rootDir, templateInfo.targetDir);
setPackageName(fullPath, templateInfo.packageName);
return { ...result, projectDir: templateInfo.targetDir };
}

// Unknown vite: template (e.g. vite:test) — application was already rewritten to create-vite@latest
if (templateInfo.command.startsWith('vite:')) {
if (!options?.silent) {
prompts.log.error(
`Unknown builtin template "${templateInfo.command}". Run ${colors.yellow('vp create --list')} to see available templates.`,
);
}
return { exitCode: 1 };
}

// Handle remote/external templates with fspy monitoring
const result = await runRemoteTemplateCommand(
workspaceInfo,
Expand All @@ -54,6 +70,9 @@ export async function executeBuiltinTemplate(
false,
options?.silent ?? false,
);
if (result.exitCode !== 0) {
return { exitCode: result.exitCode };
}
const fullPath = path.join(workspaceInfo.rootDir, templateInfo.targetDir);
// set package name in the project directory
setPackageName(fullPath, templateInfo.packageName);
Expand Down
Loading