Repository navigation
feat: add command #337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: add command #337
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "create-better-t-stack": minor | ||
| --- | ||
|
|
||
| Add 'add' command for adding addons to existing projects |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import path from "node:path"; | ||
| import { cancel, log } from "@clack/prompts"; | ||
| import pc from "picocolors"; | ||
| import type { AddInput, Addons, ProjectConfig } from "../../types"; | ||
| import { validateAddonCompatibility } from "../../utils/addon-compatibility"; | ||
| import { updateBtsConfig } from "../../utils/bts-config"; | ||
| import { setupAddons } from "../setup/addons-setup"; | ||
| import { | ||
| detectProjectConfig, | ||
| isBetterTStackProject, | ||
| } from "./detect-project-config"; | ||
| import { installDependencies } from "./install-dependencies"; | ||
| import { setupAddonsTemplate } from "./template-manager"; | ||
|
|
||
| function exitWithError(message: string): never { | ||
| cancel(pc.red(message)); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| export async function addAddonsToProject( | ||
| input: AddInput & { addons: Addons[] }, | ||
| ): Promise<void> { | ||
| try { | ||
| const projectDir = input.projectDir || process.cwd(); | ||
|
|
||
| const isBetterTStack = await isBetterTStackProject(projectDir); | ||
| if (!isBetterTStack) { | ||
| exitWithError( | ||
| "This doesn't appear to be a Better-T Stack project. Please run this command from the root of a Better-T Stack project.", | ||
| ); | ||
| } | ||
|
|
||
| const detectedConfig = await detectProjectConfig(projectDir); | ||
| if (!detectedConfig) { | ||
| exitWithError( | ||
| "Could not detect the project configuration. Please ensure this is a valid Better-T Stack project.", | ||
| ); | ||
| } | ||
|
|
||
| const config: ProjectConfig = { | ||
| projectName: detectedConfig.projectName || path.basename(projectDir), | ||
| projectDir, | ||
| relativePath: ".", | ||
| database: detectedConfig.database || "none", | ||
| orm: detectedConfig.orm || "none", | ||
| backend: detectedConfig.backend || "none", | ||
| runtime: detectedConfig.runtime || "none", | ||
| frontend: detectedConfig.frontend || [], | ||
| addons: input.addons, | ||
| examples: detectedConfig.examples || [], | ||
| auth: detectedConfig.auth || false, | ||
| git: false, | ||
| packageManager: | ||
| input.packageManager || detectedConfig.packageManager || "npm", | ||
| install: input.install || false, | ||
| dbSetup: detectedConfig.dbSetup || "none", | ||
| api: detectedConfig.api || "none", | ||
| }; | ||
|
|
||
| for (const addon of input.addons) { | ||
| const { isCompatible, reason } = validateAddonCompatibility( | ||
| addon, | ||
| config.frontend, | ||
| ); | ||
| if (!isCompatible) { | ||
| exitWithError( | ||
| reason || | ||
| `${addon} addon is not compatible with current frontend configuration`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| log.info( | ||
| pc.green( | ||
| `Adding ${input.addons.join(", ")} to ${config.frontend.join("/")}`, | ||
| ), | ||
| ); | ||
|
|
||
| await setupAddonsTemplate(projectDir, config); | ||
| await setupAddons(config, true); | ||
|
|
||
| const currentAddons = detectedConfig.addons || []; | ||
| const mergedAddons = [...new Set([...currentAddons, ...input.addons])]; | ||
| await updateBtsConfig(projectDir, { addons: mergedAddons }); | ||
|
|
||
| if (config.install) { | ||
| await installDependencies({ | ||
| projectDir, | ||
| packageManager: config.packageManager, | ||
| }); | ||
| } else { | ||
| log.info( | ||
| pc.yellow( | ||
| `Run ${pc.bold( | ||
| `${config.packageManager} install`, | ||
| )} to install dependencies`, | ||
| ), | ||
| ); | ||
| } | ||
| } catch (error) { | ||
| const message = error instanceof Error ? error.message : String(error); | ||
| exitWithError(`Error adding addons: ${message}`); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
apps/cli/src/helpers/project-generation/detect-project-config.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import path from "node:path"; | ||
| import fs from "fs-extra"; | ||
| import type { ProjectConfig } from "../../types"; | ||
| import { readBtsConfig } from "../../utils/bts-config"; | ||
|
|
||
| export async function detectProjectConfig( | ||
| projectDir: string, | ||
| ): Promise<Partial<ProjectConfig> | null> { | ||
| try { | ||
| const btsConfig = await readBtsConfig(projectDir); | ||
| if (btsConfig) { | ||
| return { | ||
| projectDir, | ||
| projectName: path.basename(projectDir), | ||
| database: btsConfig.database, | ||
| orm: btsConfig.orm, | ||
| backend: btsConfig.backend, | ||
| runtime: btsConfig.runtime, | ||
| frontend: btsConfig.frontend, | ||
| addons: btsConfig.addons, | ||
| examples: btsConfig.examples, | ||
| auth: btsConfig.auth, | ||
| packageManager: btsConfig.packageManager, | ||
| dbSetup: btsConfig.dbSetup, | ||
| api: btsConfig.api, | ||
| }; | ||
| } | ||
|
|
||
| return null; | ||
| } catch (_error) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| export async function isBetterTStackProject( | ||
| projectDir: string, | ||
| ): Promise<boolean> { | ||
| try { | ||
| return await fs.pathExists(path.join(projectDir, "bts.jsonc")); | ||
| } catch (_error) { | ||
| return false; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.