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

frameworks.ts

206 lines6.1 KB

1import { existsSync, readFileSync, readdirSync } from 'node:fs'
2import { resolve } from 'node:path'
3​
4import {
5 findFilesRecursively,
6 isDirectory,
7 readFileHelper,
8} from './file-helpers.js'
9​
10import type { AddOn, Framework, FrameworkDefinition } from './types.js'
11​
12const frameworks: Array<Framework> = []
13​
14export function scanProjectDirectory(
15 projectDirectory: string,
16 baseDirectory: string,
17) {
18 const absolutePaths: Record<string, string> = {}
19 findFilesRecursively(baseDirectory, absolutePaths)
20​
21 const files = Object.keys(absolutePaths).reduce(
22 (acc, path) => {
23 acc[path.replace(baseDirectory, '.')] = absolutePaths[path]
24 return acc
25 },
26 {} as Record<string, string>,
27 )
28​
29 const basePackageJSON = existsSync(resolve(baseDirectory, 'package.json'))
30 ? JSON.parse(readFileSync(resolve(baseDirectory, 'package.json'), 'utf8'))
31 : {}
32​
33 const optionalPackages = existsSync(
34 resolve(projectDirectory, 'packages.json'),
35 )
36 ? JSON.parse(
37 readFileSync(resolve(projectDirectory, 'packages.json'), 'utf8'),
38 )
39 : {}
40​
41 return {
42 files,
43 basePackageJSON,
44 optionalPackages,
45 }
47​
48export function scanAddOnDirectories(
49 addOnsDirectories: Array<string>,
50 framework?: { customProperties?: Record<string, any> },
51) {
52 const addOns: Array<AddOn> = []
53​
54 for (const addOnsBase of addOnsDirectories) {
55 for (const dir of readdirSync(addOnsBase).filter((file) =>
56 isDirectory(resolve(addOnsBase, file)),
57 )) {
58 const filePath = resolve(addOnsBase, dir, 'info.json')
59 const fileContent = readFileSync(filePath, 'utf-8')
60 const info = JSON.parse(fileContent)
61​
62 let packageAdditions: Record<string, string> = {}
63 if (existsSync(resolve(addOnsBase, dir, 'package.json'))) {
64 packageAdditions = JSON.parse(
65 readFileSync(resolve(addOnsBase, dir, 'package.json'), 'utf-8'),
66 )
67 }
68​
69 let readme: string | undefined
70 if (existsSync(resolve(addOnsBase, dir, 'README.md'))) {
71 readme = readFileSync(resolve(addOnsBase, dir, 'README.md'), 'utf-8')
72 }
73​
74 let smallLogo: string | undefined
75 if (existsSync(resolve(addOnsBase, dir, 'small-logo.svg'))) {
76 smallLogo = readFileSync(
77 resolve(addOnsBase, dir, 'small-logo.svg'),
78 'utf-8',
79 )
80 }
81​
82 const absoluteFiles: Record<string, string> = {}
83 const assetsDir = resolve(addOnsBase, dir, 'assets')
84 if (existsSync(assetsDir)) {
85 findFilesRecursively(assetsDir, absoluteFiles)
86 }
87 const files: Record<string, string> = {}
88 for (const file of Object.keys(absoluteFiles)) {
89 files[file.replace(assetsDir, '.')] = readFileHelper(file)
90 }
91​
92 const getFiles = () => {
93 return Promise.resolve(Object.keys(files))
94 }
95 const getFileContents = (path: string) => {
96 return Promise.resolve(files[path])
97 }
98​
99 // Validate custom properties if framework defines them
100 let validatedCustomProperties: Record<string, unknown> | undefined
101 if (framework?.customProperties && info.customProperties) {
102 validatedCustomProperties = {}
103 for (const [key, schema] of Object.entries(framework.customProperties)) {
104 if (key in info.customProperties) {
105 try {
106 validatedCustomProperties[key] = schema.parse(info.customProperties[key])
107 } catch (error: any) {
108 throw new Error(
109 `Invalid custom property "${key}" in add-on "${dir}": ${error.message}`
110 )
111 }
112 }
113 }
114 } else if (info.customProperties) {
115 // If no framework validation, pass through as-is
116 validatedCustomProperties = info.customProperties
117 }
118​
119 // Also validate routes and integrations that are at root level in info.json
120 // but defined in framework's customProperties
121 let validatedRoutes = info.routes
122 let validatedIntegrations = info.integrations
124 if (framework?.customProperties) {
125 // Validate routes if defined in framework customProperties
126 if (framework.customProperties.routes && info.routes) {
127 try {
128 validatedRoutes = framework.customProperties.routes.parse(info.routes)
129 } catch (error: any) {
130 throw new Error(
131 `Invalid routes in add-on "${dir}": ${error.message}`
132 )
133 }
134 }
136 // Validate integrations if defined in framework customProperties
137 if (framework.customProperties.integrations && info.integrations) {
138 try {
139 validatedIntegrations = framework.customProperties.integrations.parse(info.integrations)
140 } catch (error: any) {
141 throw new Error(
142 `Invalid integrations in add-on "${dir}": ${error.message}`
143 )
144 }
145 }
146 }
147​
148 addOns.push({
149 ...info,
150 id: dir,
151 packageAdditions,
152 readme,
153 files,
154 smallLogo,
155 routes: validatedRoutes,
156 integrations: validatedIntegrations,
157 customProperties: validatedCustomProperties,
158 getFiles,
159 getFileContents,
160 getDeletedFiles: () => Promise.resolve(info.deletedFiles ?? []),
161 })
162 }
163 }
164​
165 return addOns
167​
168export function __testRegisterFramework(framework: Framework) {
169 frameworks.push(framework)
171​
172export function __testClearFrameworks() {
173 frameworks.length = 0
175​
176export function registerFramework(framework: FrameworkDefinition) {
177 const { addOns, base, ...rest } = framework
178​
179 const frameworkWithBundler: Framework = {
180 ...rest,
181 getFiles: () => Promise.resolve(Object.keys(base)),
182 getFileContents: (path: string) => {
183 return Promise.resolve(base[path])
184 },
185 getDeletedFiles: () => {
186 return Promise.resolve([])
187 },
188 getAddOns: () => addOns,
189 }
190​
191 frameworks.push(frameworkWithBundler)
193​
194export function getFrameworkById(id: string) {
195 return frameworks.find((framework) => framework.id === id)
197​
198export function getFrameworkByName(name: string) {
199 return frameworks.find(
200 (framework) => framework.name.toLowerCase() === name.toLowerCase(),
201 )
203​
204export function getFrameworks() {
205 return frameworks