-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.ts
More file actions
317 lines (284 loc) · 7.94 KB
/
create.ts
File metadata and controls
317 lines (284 loc) · 7.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import {
cancel,
intro,
isCancel,
log,
select,
spinner,
text,
} from "@clack/prompts";
import fs from "fs-extra";
import path from "node:path";
import {
runInitCommand,
shouldInitCurrentProject,
} from "./init";
import {
scaffoldCreateTemplate,
} from "../templates/render-create-template";
import {
CreateCommandInputSchema,
CreateTemplateSchema,
type CreatePromptContext,
type CreateTargetPathState,
type CreateCommandInput,
type CreateTemplate,
type SchemaPreset,
} from "../types";
import {
collectPrismaSetupContext,
executePrismaSetupContext,
} from "../tasks/setup-prisma";
import {
collectCreateAddonSetupContext,
executeCreateAddonSetupContext,
} from "../tasks/setup-addons";
import { getCreatePrismaIntro } from "../ui/branding";
const DEFAULT_PROJECT_NAME = "my-app";
const DEFAULT_TEMPLATE: CreateTemplate = "hono";
const DEFAULT_SCHEMA_PRESET: SchemaPreset = "basic";
function toPackageName(projectName: string): string {
return (
projectName
.toLowerCase()
.replace(/[^a-z0-9._-]/g, "-")
.replace(/^-+/, "")
.replace(/-+$/, "") || "app"
);
}
function formatPathForDisplay(filePath: string): string {
return path.relative(process.cwd(), filePath) || ".";
}
function validateProjectName(value: string | undefined): string | undefined {
const trimmed = String(value ?? "").trim();
if (trimmed.length === 0) {
return "Please enter a project name.";
}
if (trimmed === "." || trimmed === "..") {
return "Project name cannot be '.' or '..'.";
}
if (path.isAbsolute(trimmed)) {
return "Use a relative project name instead of an absolute path.";
}
return undefined;
}
async function promptForProjectName(): Promise<string | undefined> {
const projectName = await text({
message: "Project name",
placeholder: DEFAULT_PROJECT_NAME,
initialValue: DEFAULT_PROJECT_NAME,
validate: validateProjectName,
});
if (isCancel(projectName)) {
cancel("Operation cancelled.");
return undefined;
}
return String(projectName).trim();
}
async function promptForCreateTemplate(): Promise<CreateTemplate | undefined> {
const template = await select({
message: "Select template",
initialValue: DEFAULT_TEMPLATE,
options: [
{
value: "hono",
label: "Hono",
hint: "TypeScript API starter",
},
{
value: "next",
label: "Next.js",
hint: "App Router + TypeScript starter",
},
{
value: "svelte",
label: "SvelteKit",
hint: "Official minimal SvelteKit + TypeScript starter",
},
{
value: "astro",
label: "Astro",
hint: "Official minimal Astro starter with API route example",
},
{
value: "nuxt",
label: "Nuxt",
hint: "Official minimal Nuxt starter with Nitro API route example",
},
{
value: "tanstack-start",
label: "TanStack Start",
hint: "TanStack Start React app with file routes and server functions",
},
{
value: "turborepo",
label: "Turborepo",
hint: "Monorepo starter with apps + packages/db Prisma package",
},
],
});
if (isCancel(template)) {
cancel("Operation cancelled.");
return undefined;
}
return CreateTemplateSchema.parse(template);
}
async function inspectTargetPath(targetPath: string): Promise<CreateTargetPathState> {
if (!(await fs.pathExists(targetPath))) {
return {
exists: false,
isDirectory: true,
isEmptyDirectory: true,
};
}
const stats = await fs.stat(targetPath);
if (!stats.isDirectory()) {
return {
exists: true,
isDirectory: false,
isEmptyDirectory: false,
};
}
const entries = await fs.readdir(targetPath);
return {
exists: true,
isDirectory: true,
isEmptyDirectory: entries.length === 0,
};
}
export async function runCreateCommand(
rawInput: CreateCommandInput = {},
options: {
allowInitCurrentProject?: boolean;
} = {}
): Promise<void> {
try {
const input = CreateCommandInputSchema.parse(rawInput);
const allowInitCurrentProject = options.allowInitCurrentProject === true;
if (
allowInitCurrentProject &&
(await shouldInitCurrentProject(input))
) {
await runInitCommand(input);
return;
}
intro(getCreatePrismaIntro());
const context = await collectCreateContext(input);
if (!context) {
return;
}
await executeCreateContext(context);
} catch (error) {
cancel(
`Create command failed: ${
error instanceof Error ? error.message : String(error)
}`
);
}
}
async function collectCreateContext(
input: CreateCommandInput
): Promise<CreatePromptContext | undefined> {
const useDefaults = input.yes === true;
const force = input.force === true;
const projectName =
input.name ?? (useDefaults ? DEFAULT_PROJECT_NAME : await promptForProjectName());
if (!projectName) {
return;
}
const template =
input.template ??
(useDefaults ? DEFAULT_TEMPLATE : await promptForCreateTemplate());
if (!template) {
return;
}
const targetDirectory = path.resolve(process.cwd(), projectName);
const targetPathState = await inspectTargetPath(targetDirectory);
if (targetPathState.exists && !targetPathState.isDirectory) {
cancel(
`Target path ${formatPathForDisplay(
targetDirectory
)} already exists and is not a directory. Choose a different project name.`
);
return;
}
if (
targetPathState.exists &&
!targetPathState.isEmptyDirectory &&
!force
) {
cancel(
`Target directory ${formatPathForDisplay(
targetDirectory
)} is not empty. Use --force to continue.`
);
return;
}
const prismaSetupContext = await collectPrismaSetupContext(input, {
projectDir: targetDirectory,
defaultSchemaPreset: DEFAULT_SCHEMA_PRESET,
});
if (!prismaSetupContext) {
return;
}
const addonSetupContext = await collectCreateAddonSetupContext(input, {
useDefaults,
provider: prismaSetupContext.databaseProvider,
shouldUsePrismaPostgres: prismaSetupContext.shouldUsePrismaPostgres,
});
if (addonSetupContext === undefined) {
return;
}
return {
targetDirectory,
targetPathState,
force,
template,
schemaPreset: prismaSetupContext.schemaPreset,
projectPackageName: toPackageName(path.basename(targetDirectory)),
prismaSetupContext,
addonSetupContext: addonSetupContext ?? undefined,
};
}
async function executeCreateContext(context: CreatePromptContext): Promise<void> {
const scaffoldSpinner = spinner();
scaffoldSpinner.start(`Scaffolding ${context.template} project...`);
try {
await scaffoldCreateTemplate({
projectDir: context.targetDirectory,
projectName: context.projectPackageName,
template: context.template,
schemaPreset: context.schemaPreset,
provider: context.prismaSetupContext.databaseProvider,
packageManager: context.prismaSetupContext.packageManager,
});
scaffoldSpinner.stop("Project files scaffolded.");
} catch (error) {
scaffoldSpinner.stop("Could not scaffold project files.");
cancel(error instanceof Error ? error.message : String(error));
return;
}
if (
context.targetPathState.exists &&
!context.targetPathState.isEmptyDirectory &&
context.force
) {
log.warn(
`Used --force in non-empty directory ${formatPathForDisplay(context.targetDirectory)}.`
);
}
const cdStep = `- cd ${formatPathForDisplay(context.targetDirectory)}`;
if (context.addonSetupContext) {
await executeCreateAddonSetupContext({
context: context.addonSetupContext,
packageManager: context.prismaSetupContext.packageManager,
projectDir: context.targetDirectory,
verbose: context.prismaSetupContext.verbose,
});
}
await executePrismaSetupContext(context.prismaSetupContext, {
prependNextSteps: [cdStep],
projectDir: context.targetDirectory,
includeDevNextStep: true,
});
}