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

starter.ts

126 lines3.6 KB

1import { readFile } from 'node:fs/promises'
2import { existsSync, writeFileSync } from 'node:fs'
3import { resolve } from 'node:path'
4​
5import { StarterCompiledSchema } from '../types.js'
6import { createIgnore } from '../file-helpers.js'
7import {
8 compareFilesRecursively,
9 createAppOptionsFromPersisted,
10 createPackageAdditions,
11 readCurrentProjectOptions,
12 runCreateApp,
13} from './shared.js'
14​
15import type { PersistedOptions } from '../config-file'
16import type {
17 Environment,
18 Options,
19 Starter,
20 StarterCompiled,
21 StarterInfo,
22} from '../types'
23​
24const INFO_FILE = 'starter-info.json'
25const COMPILED_FILE = 'starter.json'
26​
27export async function readOrGenerateStarterInfo(
28 options: PersistedOptions,
29): Promise<StarterInfo> {
30 return existsSync(INFO_FILE)
31 ? JSON.parse((await readFile(INFO_FILE)).toString())
32 : {
33 id: `${options.projectName}-starter`,
34 name: `${options.projectName}-starter`,
35 version: '0.0.1',
36 description: 'Project starter',
37 author: 'Jane Smith <jane.smith@example.com>',
38 license: 'MIT',
39 link: `https://github.com/jane-smith/${options.projectName}-starter`,
40 shadcnComponents: [],
41 framework: options.framework,
42 mode: options.mode!,
43 routes: [],
44 warning: '',
45 type: 'starter',
46 packageAdditions: {
47 scripts: {},
48 dependencies: {},
49 devDependencies: {},
50 },
51 dependsOn: options.chosenAddOns,
52 typescript: options.typescript!,
53 tailwind: options.tailwind!,
54 }
56​
57async function loadCurrentStarterInfo(environment: Environment) {
58 const persistedOptions = await readCurrentProjectOptions(environment)
59 const info = await readOrGenerateStarterInfo(persistedOptions)
60​
61 const output = await runCreateApp(
62 (await createAppOptionsFromPersisted(
63 persistedOptions,
64 )) as Required<Options>,
65 )
66​
67 return { info, output }
69​
70export async function updateStarterInfo(environment: Environment) {
71 const { info, output } = await loadCurrentStarterInfo(environment)
72​
73 info.packageAdditions = createPackageAdditions(
74 JSON.parse(output.files['./package.json']),
75 JSON.parse((await readFile('package.json')).toString()),
76 )
77​
78 writeFileSync(INFO_FILE, JSON.stringify(info, null, 2))
80​
81export async function compileStarter(environment: Environment) {
82 const { info, output } = await loadCurrentStarterInfo(environment)
83​
84 const ignore = createIgnore(process.cwd())
85 const changedFiles: Record<string, string> = {}
86 await compareFilesRecursively('.', ignore, output.files, changedFiles)
87​
88 const deletedFiles: Array<string> = []
89 for (const file of Object.keys(output.files)) {
90 if (!existsSync(resolve(process.cwd(), file))) {
91 deletedFiles.push(file)
92 }
93 }
94​
95 const compiledInfo: StarterCompiled = {
96 ...info,
97 files: changedFiles,
98 deletedFiles,
99 }
100​
101 writeFileSync(COMPILED_FILE, JSON.stringify(compiledInfo, null, 2))
103​
104export async function initStarter(environment: Environment) {
105 await updateStarterInfo(environment)
106 await compileStarter(environment)
108​
109export async function loadStarter(url: string): Promise<Starter> {
110 const response = await fetch(url)
111 const jsonContent = await response.json()
112​
113 const checked = StarterCompiledSchema.safeParse(jsonContent)
114 if (!checked.success) {
115 throw new Error(`Invalid starter: ${url}`)
116 }
117​
118 const starter = checked.data
119 starter.id = url
120 return {
121 ...starter,
122 getFiles: () => Promise.resolve(Object.keys(starter.files)),
123 getFileContents: (path: string) => Promise.resolve(starter.files[path]),
124 getDeletedFiles: () => Promise.resolve(starter.deletedFiles),
125 }