feat: add command (#337)

This commit is contained in:
Aman Varshney
2025-06-22 03:20:05 +05:30
committed by GitHub
parent 198d0e7434
commit 9c7a0f0110
29 changed files with 1015 additions and 255 deletions

View File

@@ -13,10 +13,13 @@ import fs from "fs-extra";
import pc from "picocolors";
import { createCli, trpcServer, zod as z } from "trpc-cli";
import { DEFAULT_CONFIG } from "./constants";
import { addAddonsToProject } from "./helpers/project-generation/add-addons";
import { createProject } from "./helpers/project-generation/create-project";
import { detectProjectConfig } from "./helpers/project-generation/detect-project-config";
import { getAddonsToAdd } from "./prompts/addons";
import { gatherConfig } from "./prompts/config-prompts";
import { getProjectName } from "./prompts/project-name";
import type { CreateInput, ProjectConfig } from "./types";
import type { AddInput, CreateInput, ProjectConfig } from "./types";
import {
AddonsSchema,
APISchema,
@@ -259,6 +262,53 @@ async function createProjectHandler(
}
}
async function addAddonsHandler(input: AddInput): Promise<void> {
try {
if (!input.addons || input.addons.length === 0) {
const projectDir = input.projectDir || process.cwd();
const detectedConfig = await detectProjectConfig(projectDir);
if (!detectedConfig) {
cancel(
pc.red(
"Could not detect project configuration. Please ensure this is a valid Better-T Stack project.",
),
);
process.exit(1);
}
const addonsPrompt = await getAddonsToAdd(
detectedConfig.frontend || [],
detectedConfig.addons || [],
);
if (addonsPrompt.length === 0) {
outro(
pc.yellow(
"No addons to add or all compatible addons are already present.",
),
);
return;
}
input.addons = addonsPrompt;
}
if (!input.addons || input.addons.length === 0) {
outro(pc.yellow("No addons specified to add."));
return;
}
await addAddonsToProject({
...input,
addons: input.addons,
});
} catch (error) {
console.error(error);
process.exit(1);
}
}
const router = t.router({
init: t.procedure
.meta({
@@ -301,6 +351,31 @@ const router = t.router({
};
await createProjectHandler(combinedInput);
}),
add: t.procedure
.meta({
description: "Add addons to an existing Better-T Stack project",
})
.input(
z.tuple([
z
.object({
addons: z.array(AddonsSchema).optional().default([]),
projectDir: z.string().optional(),
install: z
.boolean()
.optional()
.default(false)
.describe("Install dependencies after adding addons"),
packageManager: PackageManagerSchema.optional(),
})
.optional()
.default({}),
]),
)
.mutation(async ({ input }) => {
const [options] = input;
await addAddonsHandler(options);
}),
sponsors: t.procedure
.meta({ description: "Show Better-T Stack sponsors" })
.mutation(async () => {