|
| 1 | +import { createServerFn } from '@tanstack/react-start' |
| 2 | +import { readFileSync } from 'node:fs' |
| 3 | +import { basename, resolve } from 'node:path' |
| 4 | + |
| 5 | +import { |
| 6 | + getAllAddOns, |
| 7 | + createApp, |
| 8 | + createMemoryEnvironment, |
| 9 | + createAppOptionsFromPersisted, |
| 10 | +} from '@tanstack/cta-engine' |
| 11 | + |
| 12 | +import type { |
| 13 | + AddOn, |
| 14 | + PersistedOptions, |
| 15 | + Framework, |
| 16 | + Mode, |
| 17 | +} from '@tanstack/cta-engine' |
| 18 | + |
| 19 | +export const getAddons = createServerFn({ |
| 20 | + method: 'GET', |
| 21 | +}) |
| 22 | + .validator((data: unknown) => { |
| 23 | + return data as { platform: Framework; mode: Mode } |
| 24 | + }) |
| 25 | + .handler(({ data: { platform, mode } }) => { |
| 26 | + return getAllAddOns(platform, mode) |
| 27 | + }) |
| 28 | + |
| 29 | +export const getAddonInfo = createServerFn({ |
| 30 | + method: 'GET', |
| 31 | +}).handler(async () => { |
| 32 | + const addOnInfo = readFileSync( |
| 33 | + resolve(process.env.PROJECT_PATH, 'add-on.json'), |
| 34 | + ) |
| 35 | + return JSON.parse(addOnInfo.toString()) |
| 36 | +}) |
| 37 | + |
| 38 | +export const getOriginalOptions = createServerFn({ |
| 39 | + method: 'GET', |
| 40 | +}).handler(async () => { |
| 41 | + const addOnInfo = readFileSync(resolve(process.env.PROJECT_PATH, '.cta.json')) |
| 42 | + return JSON.parse(addOnInfo.toString()) as PersistedOptions |
| 43 | +}) |
| 44 | + |
| 45 | +export const runCreateApp = createServerFn({ |
| 46 | + method: 'POST', |
| 47 | +}) |
| 48 | + .validator((data: unknown) => { |
| 49 | + return data as { withAddOn: boolean; options: PersistedOptions } |
| 50 | + }) |
| 51 | + .handler( |
| 52 | + async ({ |
| 53 | + data: { withAddOn, options: persistedOptions }, |
| 54 | + }: { |
| 55 | + data: { withAddOn: boolean; options: PersistedOptions } |
| 56 | + }) => { |
| 57 | + const { output, environment } = createMemoryEnvironment() |
| 58 | + const options = await createAppOptionsFromPersisted(persistedOptions) |
| 59 | + options.chosenAddOns = withAddOn |
| 60 | + ? [...options.chosenAddOns, (await getAddonInfo()) as AddOn] |
| 61 | + : [...options.chosenAddOns] |
| 62 | + await createApp( |
| 63 | + { |
| 64 | + ...options, |
| 65 | + }, |
| 66 | + { |
| 67 | + silent: true, |
| 68 | + environment, |
| 69 | + cwd: process.env.PROJECT_PATH, |
| 70 | + }, |
| 71 | + ) |
| 72 | + |
| 73 | + output.files = Object.keys(output.files).reduce<Record<string, string>>( |
| 74 | + (acc, file) => { |
| 75 | + if (basename(file) !== '.cta.json') { |
| 76 | + acc[file] = output.files[file] |
| 77 | + } |
| 78 | + return acc |
| 79 | + }, |
| 80 | + {}, |
| 81 | + ) |
| 82 | + |
| 83 | + return output |
| 84 | + }, |
| 85 | + ) |
0 commit comments