This commit is contained in:
shadcn
2026-03-16 16:40:08 +04:00
parent 503a895520
commit 70fbec5258
2 changed files with 19 additions and 22 deletions

View File

@@ -623,6 +623,9 @@ export async function runInit(
// Add button component for new template-based projects.
...(selectedTemplate ? ["button"] : []),
]
const templatePostInit = options.isNewProject
? selectedTemplate?.postInit
: undefined
if (selectedTemplate?.init) {
const result = await selectedTemplate.init({
@@ -636,9 +639,9 @@ export async function runInit(
silent: options.silent,
})
if (shouldRunTemplatePostInit(selectedTemplate, options.isNewProject)) {
if (templatePostInit) {
// Run postInit for newly scaffolded projects (e.g. git init).
await selectedTemplate.postInit({ projectPath: options.cwd })
await templatePostInit({ projectPath: options.cwd })
}
return result
@@ -775,8 +778,8 @@ export async function runInit(
})
// Run postInit only for newly scaffolded projects.
if (shouldRunTemplatePostInit(selectedTemplate, options.isNewProject)) {
await selectedTemplate.postInit({ projectPath: options.cwd })
if (templatePostInit) {
await templatePostInit({ projectPath: options.cwd })
}
return fullConfig
@@ -906,17 +909,6 @@ async function promptForConfig(defaultConfig: Config | null = null) {
})
}
function shouldRunTemplatePostInit(
template:
| { postInit?: (options: { projectPath: string }) => Promise<void> }
| undefined,
isNewProject: boolean | undefined
): template is {
postInit: (options: { projectPath: string }) => Promise<void>
} {
return Boolean(template?.postInit && isNewProject)
}
async function promptForMinimalConfig(
defaultConfig: Config,
opts: z.infer<typeof initOptionsSchema>

View File

@@ -12,7 +12,7 @@ const { mockedGetProjectInfo, mockedExistsSync, mockedLogger } = vi.hoisted(
})
)
vi.mock("../../src/commands/init", () => ({
vi.mock("@/src/commands/init", () => ({
initOptionsSchema: z.object({
cwd: z.string(),
force: z.boolean(),
@@ -22,27 +22,27 @@ vi.mock("../../src/commands/init", () => ({
}),
}))
vi.mock("../../src/utils/get-project-info", () => ({
vi.mock("@/src/utils/get-project-info", () => ({
getProjectInfo: mockedGetProjectInfo,
}))
vi.mock("../../src/utils/get-monorepo-info", () => ({
vi.mock("@/src/utils/get-monorepo-info", () => ({
formatMonorepoMessage: vi.fn(),
getMonorepoTargets: vi.fn().mockResolvedValue([]),
isMonorepoRoot: vi.fn().mockResolvedValue(false),
}))
vi.mock("../../src/utils/highlighter", () => ({
vi.mock("@/src/utils/highlighter", () => ({
highlighter: {
info: (value: string) => value,
},
}))
vi.mock("../../src/utils/logger", () => ({
vi.mock("@/src/utils/logger", () => ({
logger: mockedLogger,
}))
vi.mock("../../src/utils/spinner", () => ({
vi.mock("@/src/utils/spinner", () => ({
spinner: vi.fn().mockReturnValue({
start: vi.fn().mockReturnValue({
succeed: vi.fn(),
@@ -58,7 +58,7 @@ vi.mock("fs-extra", () => ({
},
}))
import { preFlightInit } from "../../src/preflights/preflight-init"
import { preFlightInit } from "@/src/preflights/preflight-init"
const baseProjectInfo = {
framework: {
@@ -81,9 +81,14 @@ const baseProjectInfo = {
const baseOptions = {
cwd: "/tmp/project",
cssVariables: true,
defaults: false,
force: false,
installStyleIndex: true,
isNewProject: false,
monorepo: false,
silent: true,
yes: true,
}
afterEach(() => {