Open History to see the last commit
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.js'
16import type {
17 Environment,
18 Options,
19 Starter,
20 StarterCompiled,
21 StarterInfo,
22} from '../types.js'
23
24const INFO_FILE = 'template-info.json'
25const LEGACY_INFO_FILE = 'starter-info.json'
26const COMPILED_FILE = 'template.json'
27const LEGACY_COMPILED_FILE = 'starter.json'
28
29export async function readOrGenerateStarterInfo(
30 options: PersistedOptions,
31): Promise<StarterInfo> {
32 const info = existsSync(INFO_FILE)
33 ? JSON.parse((await readFile(INFO_FILE)).toString())
34 : existsSync(LEGACY_INFO_FILE)
35 ? JSON.parse((await readFile(LEGACY_INFO_FILE)).toString())
36 : {
37 id: `${options.projectName}-template`,
38 name: `${options.projectName} Template`,
39 version: '0.0.1',
40 description: 'Project template',
41 author: 'Jane Smith <jane.smith@example.com>',
42 license: 'MIT',
43 link: `https://github.com/jane-smith/${options.projectName}-template`,
44 shadcnComponents: [],
45 framework: options.framework,
46 mode: options.mode!,
47 routes: [],
48 warning: '',
49 type: 'starter',
50 packageAdditions: {
51 scripts: {},
52 dependencies: {},
53 devDependencies: {},
54 },
55 dependsOn: options.chosenAddOns,
56 typescript: true,
57 }
58
59 if (!info.version) {
60 info.version = '0.0.1'
61 }
62
63 return info
64}
65
66async function loadCurrentStarterInfo(environment: Environment) {
67 const persistedOptions = await readCurrentProjectOptions(environment)
68 const info = await readOrGenerateStarterInfo(persistedOptions)
69
70 const output = await runCreateApp(
71 (await createAppOptionsFromPersisted(
72 persistedOptions,
73 )) as Required<Options>,
74 )
75
76 return { info, output }
77}
78
79export async function updateStarterInfo(environment: Environment) {
80 const { info, output } = await loadCurrentStarterInfo(environment)
81
82 const generatedPackageJson =
83 output.files['./package.json'] ?? output.files['package.json']
84 if (!generatedPackageJson) {
85 throw new Error('Unable to find generated package.json in template output')
86 }
87
88 info.packageAdditions = createPackageAdditions(
89 JSON.parse(generatedPackageJson),
90 JSON.parse((await readFile('package.json')).toString()),
91 )
92
93 writeFileSync(INFO_FILE, JSON.stringify(info, null, 2))
94 writeFileSync(LEGACY_INFO_FILE, JSON.stringify(info, null, 2))
95}
96
97export async function compileStarter(environment: Environment) {
98 const { info, output } = await loadCurrentStarterInfo(environment)
99
100 const ignore = createIgnore(process.cwd())
101 const changedFiles: Record<string, string> = {}
102 await compareFilesRecursively('.', ignore, output.files, changedFiles)
103
104 const deletedFiles: Array<string> = []
105 for (const file of Object.keys(output.files)) {
106 if (!existsSync(resolve(process.cwd(), file))) {
107 deletedFiles.push(file)
108 }
109 }
110
111 const compiledInfo: StarterCompiled = {
112 ...info,
113 files: changedFiles,
114 deletedFiles,
115 }
116
117 writeFileSync(COMPILED_FILE, JSON.stringify(compiledInfo, null, 2))
118 writeFileSync(LEGACY_COMPILED_FILE, JSON.stringify(compiledInfo, null, 2))
119}
120
121export async function initStarter(environment: Environment) {
122 await updateStarterInfo(environment)
123 await compileStarter(environment)
124}
125
126export async function loadStarter(url: string): Promise<Starter> {
127 const absoluteLocalPath = resolve(process.cwd(), url)
128 const localPath = existsSync(url)
129 ? url
130 : existsSync(absoluteLocalPath)
131 ? absoluteLocalPath
132 : undefined
133
134 const jsonContent = localPath
135 ? JSON.parse((await readFile(localPath)).toString())
136 : await (await fetch(url)).json()
137
138 const checked = StarterCompiledSchema.safeParse(jsonContent)
139 if (!checked.success) {
140 throw new Error(`Invalid starter: ${url}`)
141 }
142
143 const starter = checked.data
144 starter.id = localPath ?? url
145 return {
146 ...starter,
147 getFiles: () => Promise.resolve(Object.keys(starter.files)),
148 getFileContents: (path: string) => Promise.resolve(starter.files[path]),
149 getDeletedFiles: () => Promise.resolve(starter.deletedFiles),
150 }
151}