Skip to content

Commit 215bd2f

Browse files
refractor: organize files
1 parent 293dee5 commit 215bd2f

File tree

14 files changed

+317
-299
lines changed

14 files changed

+317
-299
lines changed

.changeset/sharp-days-lead.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-better-t-stack": patch
3+
---
4+
5+
refractor files

.github/workflows/release.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ jobs:
3636
with:
3737
publish: bun run publish-packages
3838
env:
39-
MODE: "prod"
39+
TELEMETRY: "true"
4040
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4141
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
42+
POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY }}
43+
POSTHOG_HOST: ${{ secrets.POSTHOG_HOST }}

apps/cli/src/helpers/database-providers/neon-setup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { execa } from "execa";
55
import fs from "fs-extra";
66
import pc from "picocolors";
77
import type { PackageManager, ProjectConfig } from "../../types";
8-
import { getPackageExecutionCommand } from "../../utils/get-package-execution-command";
8+
import { getPackageExecutionCommand } from "../../utils/package-runner";
99
import {
1010
addEnvVariablesToFile,
1111
type EnvVariable,

apps/cli/src/helpers/database-providers/prisma-postgres-setup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import fs from "fs-extra";
66
import pc from "picocolors";
77
import type { PackageManager } from "../../types";
88
import { addPackageDependency } from "../../utils/add-package-deps";
9-
import { getPackageExecutionCommand } from "../../utils/get-package-execution-command";
9+
import { getPackageExecutionCommand } from "../../utils/package-runner";
1010
import {
1111
addEnvVariablesToFile,
1212
type EnvVariable,

apps/cli/src/helpers/database-providers/supabase-setup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { type ExecaError, execa } from "execa";
55
import fs from "fs-extra";
66
import pc from "picocolors";
77
import type { PackageManager, ProjectConfig } from "../../types";
8-
import { getPackageExecutionCommand } from "../../utils/get-package-execution-command";
8+
import { getPackageExecutionCommand } from "../../utils/package-runner";
99
import {
1010
addEnvVariablesToFile,
1111
type EnvVariable,
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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+
}

apps/cli/src/helpers/project-generation/post-installation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
ProjectConfig,
88
Runtime,
99
} from "../../types";
10-
import { getPackageExecutionCommand } from "../../utils/get-package-execution-command";
10+
import { getPackageExecutionCommand } from "../../utils/package-runner";
1111

1212
export function displayPostInstallInstructions(
1313
config: ProjectConfig & { depsInstalled: boolean },

apps/cli/src/helpers/setup/starlight-setup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import consola from "consola";
44
import { execa } from "execa";
55
import pc from "picocolors";
66
import type { ProjectConfig } from "../../types";
7-
import { getPackageExecutionCommand } from "../../utils/get-package-execution-command";
7+
import { getPackageExecutionCommand } from "../../utils/package-runner";
88

99
export async function setupStarlight(config: ProjectConfig): Promise<void> {
1010
const { packageManager, projectDir } = config;

apps/cli/src/helpers/setup/tauri-setup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import fs from "fs-extra";
66
import pc from "picocolors";
77
import type { ProjectConfig } from "../../types";
88
import { addPackageDependency } from "../../utils/add-package-deps";
9-
import { getPackageExecutionCommand } from "../../utils/get-package-execution-command";
9+
import { getPackageExecutionCommand } from "../../utils/package-runner";
1010

1111
export async function setupTauri(config: ProjectConfig): Promise<void> {
1212
const { packageManager, frontend, projectDir } = config;

0 commit comments

Comments
 (0)