Skip to content
Source
Sign inSign up
TanStack/cli
Open History to see the last commit

types.ts

258 lines6.4 KB

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