|
| 1 | +import { readFileSync } from 'node:fs' |
| 2 | +import { resolve } from 'node:path' |
| 3 | + |
| 4 | +import { |
| 5 | + addToApp, |
| 6 | + createDefaultEnvironment, |
| 7 | + createMemoryEnvironment, |
| 8 | + recursivelyGatherFiles, |
| 9 | +} from '@tanstack/cta-engine' |
| 10 | + |
| 11 | +import { createAppWrapper } from './create-app-wrapper.js' |
| 12 | + |
| 13 | +import { getProjectPath } from '@/engine-handling/server-environment.js' |
| 14 | +import { |
| 15 | + cleanUpFileArray, |
| 16 | + cleanUpFiles, |
| 17 | +} from '@/engine-handling/file-helpers.js' |
| 18 | + |
| 19 | +export async function addToAppWrapper( |
| 20 | + addOns: Array<string>, |
| 21 | + opts: { |
| 22 | + dryRun?: boolean |
| 23 | + stream?: boolean |
| 24 | + }, |
| 25 | +) { |
| 26 | + const projectPath = getProjectPath() |
| 27 | + |
| 28 | + const persistedOptions = JSON.parse( |
| 29 | + readFileSync(resolve(projectPath, '.cta.json')), |
| 30 | + ) |
| 31 | + |
| 32 | + const newAddons: Array<string> = [] |
| 33 | + for (const addOn of addOns) { |
| 34 | + if (!persistedOptions.existingAddOns.includes(addOn)) { |
| 35 | + newAddons.push(addOn) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + if (newAddons.length === 0) { |
| 40 | + return await createAppWrapper(persistedOptions, opts) |
| 41 | + } |
| 42 | + |
| 43 | + async function createEnvironment() { |
| 44 | + if (opts.dryRun) { |
| 45 | + const { environment, output } = createMemoryEnvironment(projectPath) |
| 46 | + environment.writeFile( |
| 47 | + resolve(projectPath, '.cta.json'), |
| 48 | + JSON.stringify(persistedOptions, null, 2), |
| 49 | + ) |
| 50 | + |
| 51 | + const localFiles = await cleanUpFiles( |
| 52 | + await recursivelyGatherFiles(projectPath, false), |
| 53 | + ) |
| 54 | + for (const file of Object.keys(localFiles)) { |
| 55 | + environment.writeFile(resolve(projectPath, file), localFiles[file]) |
| 56 | + } |
| 57 | + return { environment, output } |
| 58 | + } |
| 59 | + return { |
| 60 | + environment: createDefaultEnvironment(), |
| 61 | + output: { files: {}, deletedFiles: [], commands: [] }, |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + const { environment, output } = await createEnvironment() |
| 66 | + |
| 67 | + if (opts.stream) { |
| 68 | + return new ReadableStream({ |
| 69 | + start(controller) { |
| 70 | + environment.startStep = (message) => { |
| 71 | + console.log(message) |
| 72 | + controller.enqueue(new TextEncoder().encode(`${message}\n`)) |
| 73 | + } |
| 74 | + environment.finishStep = (message) => { |
| 75 | + console.log(message) |
| 76 | + controller.enqueue(new TextEncoder().encode(`${message}\n`)) |
| 77 | + } |
| 78 | + |
| 79 | + environment.startRun() |
| 80 | + addToApp(environment, newAddons, projectPath, { |
| 81 | + forced: true, |
| 82 | + }).then(() => { |
| 83 | + environment.finishRun() |
| 84 | + controller.close() |
| 85 | + }) |
| 86 | + }, |
| 87 | + }) |
| 88 | + } else { |
| 89 | + environment.startRun() |
| 90 | + await addToApp(environment, newAddons, projectPath, { |
| 91 | + forced: true, |
| 92 | + }) |
| 93 | + environment.finishRun() |
| 94 | + |
| 95 | + output.files = cleanUpFiles(output.files, projectPath) |
| 96 | + output.deletedFiles = cleanUpFileArray(output.deletedFiles, projectPath) |
| 97 | + return output |
| 98 | + } |
| 99 | +} |
0 commit comments