Open History to see the last commit
1import z from 'zod'
2
3import type { PackageManager } from './package-manager.js'
4
5export type StatusStepType =
6 | 'file'
7 | 'command'
8 | 'info'
9 | 'package-manager'
10 | 'other'
11
12export const AddOnSelectOptionSchema = z.object({
13 type: z.literal('select'),
14 label: z.string(),
15 description: z.string().optional(),
16 default: z.string(),
17 options: z.array(
18 z.object({
19 value: z.string(),
20 label: z.string(),
21 }),
22 ),
23})
24
25export const AddOnOptionSchema = z.discriminatedUnion('type', [
26 AddOnSelectOptionSchema,
27])
28
29export const AddOnOptionsSchema = z.record(z.string(), AddOnOptionSchema)
30
31export const AddOnBaseSchema = z.object({
32 id: z.string(),
33 name: z.string(),
34 description: z.string(),
35 author: z.string().optional(),
36 version: z.string().optional(),
37 link: z.string().optional(),
38 license: z.string().optional(),
39 warning: z.string().optional(),
40 type: z.enum(['add-on', 'example', 'starter', 'toolchain', 'deployment']),
41 priority: z.number().optional(),
42 command: z
43 .object({
44 command: z.string(),
45 args: z.array(z.string()).optional(),
46 })
47 .optional(),
48 routes: z
49 .array(
50 z.object({
51 url: z.string().optional(),
52 name: z.string().optional(),
53 path: z.string(),
54 jsName: z.string(),
55 }),
56 )
57 .optional(),
58 packageAdditions: z
59 .object({
60 dependencies: z.record(z.string(), z.string()).optional(),
61 devDependencies: z.record(z.string(), z.string()).optional(),
62 scripts: z.record(z.string(), z.string()).optional(),
63 })
64 .optional(),
65 shadcnComponents: z.array(z.string()).optional(),
66 dependsOn: z.array(z.string()).optional(),
67 smallLogo: z.string().optional(),
68 logo: z.string().optional(),
69 addOnSpecialSteps: z.array(z.string()).optional(),
70 createSpecialSteps: z.array(z.string()).optional(),
71 postInitSpecialSteps: z.array(z.string()).optional(),
72 options: AddOnOptionsSchema.optional(),
73 default: z.boolean().optional(),
74})
75
76export const StarterSchema = AddOnBaseSchema.extend({
77 framework: z.string(),
78 mode: z.string(),
79 typescript: z.boolean(),
80 tailwind: z.boolean(),
81 banner: z.string().optional(),
82})
83
84export const StarterCompiledSchema = StarterSchema.extend({
85 files: z.record(z.string(), z.string()),
86 deletedFiles: z.array(z.string()),
87})
88
89export const IntegrationSchema = z.object({
90 type: z.string().optional(),
91 path: z.string().optional(),
92 jsName: z.string().optional(),
93 import: z.string().optional(),
94 code: z.string().optional(),
95})
96
97export const AddOnInfoSchema = AddOnBaseSchema.extend({
98 modes: z.array(z.string()),
99 integrations: z.array(IntegrationSchema).optional(),
100 phase: z.enum(['setup', 'add-on']),
101 readme: z.string().optional(),
102})
103
104export const AddOnCompiledSchema = AddOnInfoSchema.extend({
105 files: z.record(z.string(), z.string()),
106 deletedFiles: z.array(z.string()),
107 packageTemplate: z.string().optional(),
108})
109
110export type AddOnSelectOption = z.infer<typeof AddOnSelectOptionSchema>
111
112export type AddOnOption = z.infer<typeof AddOnOptionSchema>
113
114export type AddOnOptions = z.infer<typeof AddOnOptionsSchema>
115
116export type Integration = z.infer<typeof IntegrationSchema>
117
118export type AddOnBase = z.infer<typeof AddOnBaseSchema>
119
120export type StarterInfo = z.infer<typeof StarterSchema>
121
122export type StarterCompiled = z.infer<typeof StarterCompiledSchema>
123
124export type AddOnInfo = z.infer<typeof AddOnInfoSchema>
125
126export type AddOnCompiled = z.infer<typeof AddOnCompiledSchema>
127
128export interface AddOnSelection {
129 id: string
130 enabled: boolean
131 options: Record<string, any>
132}
133
134export type FileBundleHandler = {
135 getFiles: () => Promise<Array<string>>
136 getFileContents: (path: string) => Promise<string>
137 getDeletedFiles: () => Promise<Array<string>>
138}
139
140export type AddOn = AddOnCompiled & FileBundleHandler
141
142export type Starter = StarterCompiled & FileBundleHandler
143
144export type FrameworkDefinition = {
145 id: string
146 name: string
147 description: string
148 version: string
149
150 base: Record<string, string>
151 addOns: Array<AddOn>
152 basePackageJSON: Record<string, any>
153 optionalPackages: Record<string, any>
154
155 supportedModes: Record<
156 string,
157 {
158 displayName: string
159 description: string
160 forceTypescript: boolean
161 }
162 >
163 contentChecksum?: string
164}
165
166export type Framework = Omit<FrameworkDefinition, 'base' | 'addOns'> &
167 FileBundleHandler & {
168 getAddOns: () => Array<AddOn>
169 }
170
171export interface Options {
172 projectName: string
173 targetDir: string
174
175 framework: Framework
176 mode: string
177
178 typescript: boolean
179 tailwind: boolean
180
181 packageManager: PackageManager
182 git: boolean
183
184 chosenAddOns: Array<AddOn>
185 addOnOptions: Record<string, Record<string, any>>
186 starter?: Starter | undefined
187}
188
189export type SerializedOptions = Omit<
190 Options,
191 'chosenAddOns' | 'starter' | 'framework'
192> & {
193 chosenAddOns: Array<string>
194 starter?: string | undefined
195 framework: string
196}
197
198type ProjectEnvironment = {
199 startRun: () => void
200 finishRun: () => void
201 getErrors: () => Array<string>
202}
203
204type FileEnvironment = {
205 appendFile: (path: string, contents: string) => Promise<void>
206 copyFile: (from: string, to: string) => Promise<void>
207 writeFile: (path: string, contents: string) => Promise<void>
208 writeFileBase64: (path: string, base64Contents: string) => Promise<void>
209 execute: (
210 command: string,
211 args: Array<string>,
212 cwd: string,
213 options?: { inherit?: boolean },
214 ) => Promise<{ stdout: string }>
215 deleteFile: (path: string) => Promise<void>
216
217 exists: (path: string) => boolean
218 isDirectory: (path: string) => boolean
219 readFile: (path: string) => Promise<string>
220 readdir: (path: string) => Promise<Array<string>>
221 rimraf: (path: string) => Promise<void>
222}
223
224export type StatusEvent = {
225 id: string
226 type: StatusStepType
227 message: string
228}
229export type StopEvent = {
230 id: string
231}
232
233type UIEnvironment = {
234 appName: string
235
236 startStep: (info: {
237 id: string
238 type: StatusStepType
239 message: string
240 }) => void
241 finishStep: (id: string, finalMessage: string) => void
242
243 intro: (message: string) => void
244 outro: (message: string) => void
245
246 info: (title?: string, message?: string) => void
247 error: (title?: string, message?: string) => void
248 warn: (title?: string, message?: string) => void
249
250 spinner: () => {
251 start: (message: string) => void
252 stop: (message: string) => void
253 }
254 confirm: (message: string) => Promise<boolean>
255}
256
257export type Environment = ProjectEnvironment & FileEnvironment & UIEnvironment