|
| 1 | +import path from "node:path"; |
| 2 | +import { cancel, log } from "@clack/prompts"; |
| 3 | +import pc from "picocolors"; |
| 4 | +import type { AddInput, Addons, ProjectConfig } from "../../types"; |
| 5 | +import { validateAddonCompatibility } from "../../utils/addon-compatibility"; |
| 6 | +import { updateBtsConfig } from "../../utils/bts-config"; |
| 7 | +import { setupAddons } from "../setup/addons-setup"; |
| 8 | +import { |
| 9 | + detectProjectConfig, |
| 10 | + isBetterTStackProject, |
| 11 | +} from "./detect-project-config"; |
| 12 | +import { installDependencies } from "./install-dependencies"; |
| 13 | +import { setupAddonsTemplate } from "./template-manager"; |
| 14 | + |
| 15 | +function exitWithError(message: string): never { |
| 16 | + cancel(pc.red(message)); |
| 17 | + process.exit(1); |
| 18 | +} |
| 19 | + |
| 20 | +export async function addAddonsToProject( |
| 21 | + input: AddInput & { addons: Addons[] }, |
| 22 | +): Promise<void> { |
| 23 | + try { |
| 24 | + const projectDir = input.projectDir || process.cwd(); |
| 25 | + |
| 26 | + const isBetterTStack = await isBetterTStackProject(projectDir); |
| 27 | + if (!isBetterTStack) { |
| 28 | + exitWithError( |
| 29 | + "This doesn't appear to be a Better-T Stack project. Please run this command from the root of a Better-T Stack project.", |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + const detectedConfig = await detectProjectConfig(projectDir); |
| 34 | + if (!detectedConfig) { |
| 35 | + exitWithError( |
| 36 | + "Could not detect the project configuration. Please ensure this is a valid Better-T Stack project.", |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + const config: ProjectConfig = { |
| 41 | + projectName: detectedConfig.projectName || path.basename(projectDir), |
| 42 | + projectDir, |
| 43 | + relativePath: ".", |
| 44 | + database: detectedConfig.database || "none", |
| 45 | + orm: detectedConfig.orm || "none", |
| 46 | + backend: detectedConfig.backend || "none", |
| 47 | + runtime: detectedConfig.runtime || "none", |
| 48 | + frontend: detectedConfig.frontend || [], |
| 49 | + addons: input.addons, |
| 50 | + examples: detectedConfig.examples || [], |
| 51 | + auth: detectedConfig.auth || false, |
| 52 | + git: false, |
| 53 | + packageManager: |
| 54 | + input.packageManager || detectedConfig.packageManager || "npm", |
| 55 | + install: input.install || false, |
| 56 | + dbSetup: detectedConfig.dbSetup || "none", |
| 57 | + api: detectedConfig.api || "none", |
| 58 | + }; |
| 59 | + |
| 60 | + for (const addon of input.addons) { |
| 61 | + const { isCompatible, reason } = validateAddonCompatibility( |
| 62 | + addon, |
| 63 | + config.frontend, |
| 64 | + ); |
| 65 | + if (!isCompatible) { |
| 66 | + exitWithError( |
| 67 | + reason || |
| 68 | + `${addon} addon is not compatible with current frontend configuration`, |
| 69 | + ); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + log.info( |
| 74 | + pc.green( |
| 75 | + `Adding ${input.addons.join(", ")} to ${config.frontend.join("/")}`, |
| 76 | + ), |
| 77 | + ); |
| 78 | + |
| 79 | + await setupAddonsTemplate(projectDir, config); |
| 80 | + await setupAddons(config, true); |
| 81 | + |
| 82 | + const currentAddons = detectedConfig.addons || []; |
| 83 | + const mergedAddons = [...new Set([...currentAddons, ...input.addons])]; |
| 84 | + await updateBtsConfig(projectDir, { addons: mergedAddons }); |
| 85 | + |
| 86 | + if (config.install) { |
| 87 | + await installDependencies({ |
| 88 | + projectDir, |
| 89 | + packageManager: config.packageManager, |
| 90 | + }); |
| 91 | + } else { |
| 92 | + log.info( |
| 93 | + pc.yellow( |
| 94 | + `Run ${pc.bold( |
| 95 | + `${config.packageManager} install`, |
| 96 | + )} to install dependencies`, |
| 97 | + ), |
| 98 | + ); |
| 99 | + } |
| 100 | + } catch (error) { |
| 101 | + const message = error instanceof Error ? error.message : String(error); |
| 102 | + exitWithError(`Error adding addons: ${message}`); |
| 103 | + } |
| 104 | +} |
0 commit comments