|
| 1 | +import path from "node:path"; |
| 2 | +import { cancel, intro, log, outro } from "@clack/prompts"; |
| 3 | +import fs from "fs-extra"; |
| 4 | +import pc from "picocolors"; |
| 5 | +import { DEFAULT_CONFIG } from "../../constants"; |
| 6 | +import { getAddonsToAdd } from "../../prompts/addons"; |
| 7 | +import { gatherConfig } from "../../prompts/config-prompts"; |
| 8 | +import { getProjectName } from "../../prompts/project-name"; |
| 9 | +import type { AddInput, CreateInput, ProjectConfig } from "../../types"; |
| 10 | +import { trackProjectCreation } from "../../utils/analytics"; |
| 11 | +import { displayConfig } from "../../utils/display-config"; |
| 12 | +import { generateReproducibleCommand } from "../../utils/generate-reproducible-command"; |
| 13 | +import { |
| 14 | + handleDirectoryConflict, |
| 15 | + setupProjectDirectory, |
| 16 | +} from "../../utils/project-directory"; |
| 17 | +import { renderTitle } from "../../utils/render-title"; |
| 18 | +import { getProvidedFlags, processAndValidateFlags } from "../../validation"; |
| 19 | +import { addAddonsToProject } from "./add-addons"; |
| 20 | +import { createProject } from "./create-project"; |
| 21 | +import { detectProjectConfig } from "./detect-project-config"; |
| 22 | + |
| 23 | +export async function createProjectHandler( |
| 24 | + input: CreateInput & { projectName?: string }, |
| 25 | +) { |
| 26 | + const startTime = Date.now(); |
| 27 | + |
| 28 | + try { |
| 29 | + renderTitle(); |
| 30 | + intro(pc.magenta("Creating a new Better-T Stack project")); |
| 31 | + |
| 32 | + let currentPathInput: string; |
| 33 | + if (input.yes && input.projectName) { |
| 34 | + currentPathInput = input.projectName; |
| 35 | + } else if (input.yes) { |
| 36 | + let defaultName = DEFAULT_CONFIG.relativePath; |
| 37 | + let counter = 1; |
| 38 | + while ( |
| 39 | + fs.pathExistsSync(path.resolve(process.cwd(), defaultName)) && |
| 40 | + fs.readdirSync(path.resolve(process.cwd(), defaultName)).length > 0 |
| 41 | + ) { |
| 42 | + defaultName = `${DEFAULT_CONFIG.projectName}-${counter}`; |
| 43 | + counter++; |
| 44 | + } |
| 45 | + currentPathInput = defaultName; |
| 46 | + } else { |
| 47 | + currentPathInput = await getProjectName(input.projectName); |
| 48 | + } |
| 49 | + |
| 50 | + const { finalPathInput, shouldClearDirectory } = |
| 51 | + await handleDirectoryConflict(currentPathInput); |
| 52 | + |
| 53 | + const { finalResolvedPath, finalBaseName } = await setupProjectDirectory( |
| 54 | + finalPathInput, |
| 55 | + shouldClearDirectory, |
| 56 | + ); |
| 57 | + |
| 58 | + const cliInput = { |
| 59 | + ...input, |
| 60 | + projectDirectory: input.projectName, |
| 61 | + }; |
| 62 | + |
| 63 | + const providedFlags = getProvidedFlags(cliInput); |
| 64 | + const flagConfig = processAndValidateFlags( |
| 65 | + cliInput, |
| 66 | + providedFlags, |
| 67 | + finalBaseName, |
| 68 | + ); |
| 69 | + const { projectName: _projectNameFromFlags, ...otherFlags } = flagConfig; |
| 70 | + |
| 71 | + if (!input.yes && Object.keys(otherFlags).length > 0) { |
| 72 | + log.info(pc.yellow("Using these pre-selected options:")); |
| 73 | + log.message(displayConfig(otherFlags)); |
| 74 | + log.message(""); |
| 75 | + } |
| 76 | + |
| 77 | + let config: ProjectConfig; |
| 78 | + if (input.yes) { |
| 79 | + config = { |
| 80 | + ...DEFAULT_CONFIG, |
| 81 | + ...flagConfig, |
| 82 | + projectName: finalBaseName, |
| 83 | + projectDir: finalResolvedPath, |
| 84 | + relativePath: finalPathInput, |
| 85 | + }; |
| 86 | + |
| 87 | + if (config.backend === "convex") { |
| 88 | + log.info( |
| 89 | + "Due to '--backend convex' flag, the following options have been automatically set: auth=false, database=none, orm=none, api=none, runtime=none, dbSetup=none, examples=todo", |
| 90 | + ); |
| 91 | + } else if (config.backend === "none") { |
| 92 | + log.info( |
| 93 | + "Due to '--backend none', the following options have been automatically set: --auth=false, --database=none, --orm=none, --api=none, --runtime=none, --db-setup=none, --examples=none", |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + log.info( |
| 98 | + pc.yellow("Using default/flag options (config prompts skipped):"), |
| 99 | + ); |
| 100 | + log.message(displayConfig(config)); |
| 101 | + log.message(""); |
| 102 | + } else { |
| 103 | + config = await gatherConfig( |
| 104 | + flagConfig, |
| 105 | + finalBaseName, |
| 106 | + finalResolvedPath, |
| 107 | + finalPathInput, |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + await createProject(config); |
| 112 | + |
| 113 | + const reproducibleCommand = generateReproducibleCommand(config); |
| 114 | + log.success( |
| 115 | + pc.blue( |
| 116 | + `You can reproduce this setup with the following command:\n${reproducibleCommand}`, |
| 117 | + ), |
| 118 | + ); |
| 119 | + |
| 120 | + await trackProjectCreation(config); |
| 121 | + |
| 122 | + const elapsedTimeInSeconds = ((Date.now() - startTime) / 1000).toFixed(2); |
| 123 | + outro( |
| 124 | + pc.magenta( |
| 125 | + `Project created successfully in ${pc.bold( |
| 126 | + elapsedTimeInSeconds, |
| 127 | + )} seconds!`, |
| 128 | + ), |
| 129 | + ); |
| 130 | + } catch (error) { |
| 131 | + console.error(error); |
| 132 | + process.exit(1); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +export async function addAddonsHandler(input: AddInput): Promise<void> { |
| 137 | + try { |
| 138 | + if (!input.addons || input.addons.length === 0) { |
| 139 | + const projectDir = input.projectDir || process.cwd(); |
| 140 | + const detectedConfig = await detectProjectConfig(projectDir); |
| 141 | + |
| 142 | + if (!detectedConfig) { |
| 143 | + cancel( |
| 144 | + pc.red( |
| 145 | + "Could not detect project configuration. Please ensure this is a valid Better-T Stack project.", |
| 146 | + ), |
| 147 | + ); |
| 148 | + process.exit(1); |
| 149 | + } |
| 150 | + |
| 151 | + const addonsPrompt = await getAddonsToAdd( |
| 152 | + detectedConfig.frontend || [], |
| 153 | + detectedConfig.addons || [], |
| 154 | + ); |
| 155 | + |
| 156 | + if (addonsPrompt.length === 0) { |
| 157 | + outro( |
| 158 | + pc.yellow( |
| 159 | + "No addons to add or all compatible addons are already present.", |
| 160 | + ), |
| 161 | + ); |
| 162 | + return; |
| 163 | + } |
| 164 | + |
| 165 | + input.addons = addonsPrompt; |
| 166 | + } |
| 167 | + |
| 168 | + if (!input.addons || input.addons.length === 0) { |
| 169 | + outro(pc.yellow("No addons specified to add.")); |
| 170 | + return; |
| 171 | + } |
| 172 | + |
| 173 | + await addAddonsToProject({ |
| 174 | + ...input, |
| 175 | + addons: input.addons, |
| 176 | + }); |
| 177 | + } catch (error) { |
| 178 | + console.error(error); |
| 179 | + process.exit(1); |
| 180 | + } |
| 181 | +} |
0 commit comments