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

types.ts

216 lines5.2 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 AddOnBaseSchema = z.object({
13 id: z.string(),
14 name: z.string(),
15 description: z.string(),
16 author: z.string().optional(),
17 version: z.string().optional(),
18 link: z.string().optional(),
19 license: z.string().optional(),
20 warning: z.string().optional(),
21 type: z.enum(['add-on', 'example', 'starter', 'toolchain']),
22 command: z
23 .object({
24 command: z.string(),
25 args: z.array(z.string()).optional(),
26 })
27 .optional(),
28 routes: z
29 .array(
30 z.object({
31 url: z.string().optional(),
32 name: z.string().optional(),
33 path: z.string(),
34 jsName: z.string(),
35 }),
36 )
37 .optional(),
38 packageAdditions: z
39 .object({
40 dependencies: z.record(z.string(), z.string()).optional(),
41 devDependencies: z.record(z.string(), z.string()).optional(),
42 scripts: z.record(z.string(), z.string()).optional(),
43 })
44 .optional(),
45 shadcnComponents: z.array(z.string()).optional(),
46 dependsOn: z.array(z.string()).optional(),
47 smallLogo: z.string().optional(),
48 logo: z.string().optional(),
49 addOnSpecialSteps: z.array(z.string()).optional(),
50 createSpecialSteps: z.array(z.string()).optional(),
51})
52​
53export const StarterSchema = AddOnBaseSchema.extend({
54 framework: z.string(),
55 mode: z.string(),
56 typescript: z.boolean(),
57 tailwind: z.boolean(),
58 banner: z.string().optional(),
59})
60​
61export const StarterCompiledSchema = StarterSchema.extend({
62 files: z.record(z.string(), z.string()),
63 deletedFiles: z.array(z.string()),
64})
65​
66export const IntegrationSchema = z.object({
67 type: z.string(),
68 path: z.string(),
69 jsName: z.string(),
70})
71​
72export const AddOnInfoSchema = AddOnBaseSchema.extend({
73 modes: z.array(z.string()),
74 integrations: z.array(IntegrationSchema).optional(),
75 phase: z.enum(['setup', 'add-on']),
76 readme: z.string().optional(),
77})
78​
79export const AddOnCompiledSchema = AddOnInfoSchema.extend({
80 files: z.record(z.string(), z.string()),
81 deletedFiles: z.array(z.string()),
82})
83​
84export type Integration = z.infer<typeof IntegrationSchema>
85​
86export type AddOnBase = z.infer<typeof AddOnBaseSchema>
87​
88export type StarterInfo = z.infer<typeof StarterSchema>
89​
90export type StarterCompiled = z.infer<typeof StarterCompiledSchema>
91​
92export type AddOnInfo = z.infer<typeof AddOnInfoSchema>
93​
94export type AddOnCompiled = z.infer<typeof AddOnCompiledSchema>
95​
96export type FileBundleHandler = {
97 getFiles: () => Promise<Array<string>>
98 getFileContents: (path: string) => Promise<string>
99 getDeletedFiles: () => Promise<Array<string>>
101​
102export type AddOn = AddOnInfo & FileBundleHandler
103​
104export type Starter = StarterCompiled & FileBundleHandler
105​
106export type FrameworkDefinition = {
107 id: string
108 name: string
109 description: string
110 version: string
111​
112 base: Record<string, string>
113 addOns: Array<AddOn>
114 basePackageJSON: Record<string, any>
115 optionalPackages: Record<string, any>
116​
117 supportedModes: Record<
118 string,
119 {
120 displayName: string
121 description: string
122 forceTypescript: boolean
123 }
124 >
126​
127export type Framework = Omit<FrameworkDefinition, 'base' | 'addOns'> &
128 FileBundleHandler & {
129 getAddOns: () => Array<AddOn>
130 }
131​
132export interface Options {
133 projectName: string
134 targetDir: string
135​
136 framework: Framework
137 mode: string
138​
139 typescript: boolean
140 tailwind: boolean
141​
142 packageManager: PackageManager
143 git: boolean
144​
145 chosenAddOns: Array<AddOn>
146 starter?: Starter | undefined
148​
149export type SerializedOptions = Omit<
150 Options,
151 'chosenAddOns' | 'starter' | 'framework'
152> & {
153 chosenAddOns: Array<string>
154 starter?: string | undefined
155 framework: string
157​
158type ProjectEnvironment = {
159 startRun: () => void
160 finishRun: () => void
161 getErrors: () => Array<string>
163​
164type FileEnvironment = {
165 appendFile: (path: string, contents: string) => Promise<void>
166 copyFile: (from: string, to: string) => Promise<void>
167 writeFile: (path: string, contents: string) => Promise<void>
168 writeFileBase64: (path: string, base64Contents: string) => Promise<void>
169 execute: (
170 command: string,
171 args: Array<string>,
172 cwd: string,
173 ) => Promise<{ stdout: string }>
174 deleteFile: (path: string) => Promise<void>
175​
176 exists: (path: string) => boolean
177 isDirectory: (path: string) => boolean
178 readFile: (path: string) => Promise<string>
179 readdir: (path: string) => Promise<Array<string>>
180 rimraf: (path: string) => Promise<void>
182​
183export type StatusEvent = {
184 id: string
185 type: StatusStepType
186 message: string
188export type StopEvent = {
189 id: string
191​
192type UIEnvironment = {
193 appName: string
194​
195 startStep: (info: {
196 id: string
197 type: StatusStepType
198 message: string
199 }) => void
200 finishStep: (id: string, finalMessage: string) => void
201​
202 intro: (message: string) => void
203 outro: (message: string) => void
204​
205 info: (title?: string, message?: string) => void
206 error: (title?: string, message?: string) => void
207 warn: (title?: string, message?: string) => void
208​
209 spinner: () => {
210 start: (message: string) => void
211 stop: (message: string) => void
212 }
213 confirm: (message: string) => Promise<boolean>
215​
216export type Environment = ProjectEnvironment & FileEnvironment & UIEnvironment