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'
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 warning: '',
44 type: 'starter',
45 packageAdditions: {
46 scripts: {},
47 dependencies: {},
48 devDependencies: {},
49 },
50 dependsOn: options.chosenAddOns,
51 typescript: options.typescript!,
52 tailwind: options.tailwind!,
53 }
54}
55
56async function loadCurrentStarterInfo(environment: Environment) {
57 const persistedOptions = await readCurrentProjectOptions(environment)
58 const info = await readOrGenerateStarterInfo(persistedOptions)
59
60 const output = await runCreateApp(
61 (await createAppOptionsFromPersisted(
62 persistedOptions,
63 )) as Required<Options>,
64 )
65
66 return { info, output }
67}
68
69export async function updateStarterInfo(environment: Environment) {
70 const { info, output } = await loadCurrentStarterInfo(environment)
71
72 info.packageAdditions = createPackageAdditions(
73 JSON.parse(output.files['./package.json']),
74 JSON.parse((await readFile('package.json')).toString()),
75 )
76
77 writeFileSync(INFO_FILE, JSON.stringify(info, null, 2))
78}
79
80export async function compileStarter(environment: Environment) {
81 const { info, output } = await loadCurrentStarterInfo(environment)
82
83 const ignore = createIgnore(process.cwd())
84 const changedFiles: Record<string, string> = {}
85 await compareFilesRecursively('.', ignore, output.files, changedFiles)
86
87 const deletedFiles: Array<string> = []
88 for (const file of Object.keys(output.files)) {
89 if (!existsSync(resolve(process.cwd(), file))) {
90 deletedFiles.push(file)
91 }
92 }
93
94 const compiledInfo: StarterCompiled = {
95 ...info,
96 files: changedFiles,
97 deletedFiles,
98 }
99
100 writeFileSync(COMPILED_FILE, JSON.stringify(compiledInfo, null, 2))
101}
102
103export async function initStarter(environment: Environment) {
104 await updateStarterInfo(environment)
105 await compileStarter(environment)
106}
107
108export async function loadStarter(url: string): Promise<Starter> {
109 const response = await fetch(url)
110 const jsonContent = await response.json()
111
112 const checked = StarterCompiledSchema.safeParse(jsonContent)
113 if (!checked.success) {
114 throw new Error(`Invalid starter: ${url}`)
115 }
116
117 const starter = checked.data
118 starter.id = url
119 return {
120 ...starter,
121 getFiles: () => Promise.resolve(Object.keys(starter.files)),
122 getFileContents: (path: string) => Promise.resolve(starter.files[path]),
123 getDeletedFiles: () => Promise.resolve(starter.deletedFiles),
124 }
125}