Skip to content

Commit ecd7db9

Browse files
feat: add project name validation
1 parent 432f916 commit ecd7db9

File tree

3 files changed

+59
-18
lines changed

3 files changed

+59
-18
lines changed

.changeset/mean-pans-invite.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+
feat: add project name validation

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ export async function setupTurso(projectDir: string) {
170170
const dbNameResponse = await text({
171171
message: "Enter database name:",
172172
defaultValue: suggestedName,
173+
initialValue: suggestedName,
174+
placeholder: suggestedName,
173175
});
174176

175177
if (isCancel(dbNameResponse)) {

apps/cli/src/index.ts

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from "node:path";
12
import {
23
cancel,
34
confirm,
@@ -12,6 +13,7 @@ import {
1213
} from "@clack/prompts";
1314
import chalk from "chalk";
1415
import { Command } from "commander";
16+
import fs from "fs-extra";
1517
import { DEFAULT_CONFIG } from "./consts";
1618
import { createProject } from "./helpers/create-project";
1719
import { renderTitle } from "./render-title";
@@ -35,19 +37,51 @@ const program = new Command();
3537
async function gatherConfig(
3638
flags: Partial<ProjectConfig>,
3739
): Promise<ProjectConfig> {
38-
const shouldAskGit = flags.git !== false;
39-
4040
const result = await group(
4141
{
42-
projectName: () =>
43-
text({
44-
message: "📝 Project name",
45-
placeholder: "my-better-t-app",
46-
initialValue: flags.projectName,
47-
validate: (value) => {
48-
if (!value) return "Project name is required";
49-
},
50-
}),
42+
projectName: async () => {
43+
let isValid = false;
44+
let projectName: string | symbol = "";
45+
let defaultName = DEFAULT_CONFIG.projectName;
46+
let counter = 1;
47+
48+
while (fs.pathExistsSync(path.resolve(process.cwd(), defaultName))) {
49+
defaultName = `${DEFAULT_CONFIG.projectName}-${counter}`;
50+
counter++;
51+
}
52+
53+
while (!isValid) {
54+
const response = await text({
55+
message: "📝 Project name",
56+
placeholder: defaultName,
57+
initialValue: flags.projectName || defaultName,
58+
defaultValue: defaultName,
59+
validate: (value) => {
60+
const nameToUse = value.trim() || defaultName;
61+
const projectDir = path.resolve(process.cwd(), nameToUse);
62+
63+
if (fs.pathExistsSync(projectDir)) {
64+
const dirContents = fs.readdirSync(projectDir);
65+
if (dirContents.length > 0) {
66+
return `Directory "${nameToUse}" already exists and is not empty. Please choose a different name.`;
67+
}
68+
}
69+
70+
isValid = true;
71+
return undefined;
72+
},
73+
});
74+
75+
if (typeof response === "symbol") {
76+
cancel("Operation cancelled.");
77+
process.exit(0);
78+
}
79+
80+
projectName = response || defaultName;
81+
}
82+
83+
return projectName as string;
84+
},
5185
database: () =>
5286
!flags.database
5387
? select<ProjectDatabase>({
@@ -96,7 +130,7 @@ async function gatherConfig(
96130
})
97131
: Promise.resolve(flags.features),
98132
git: () =>
99-
shouldAskGit
133+
flags.git !== false
100134
? confirm({
101135
message: "🗃️ Initialize Git repository?",
102136
initialValue: true,
@@ -143,12 +177,12 @@ async function gatherConfig(
143177
);
144178

145179
return {
146-
projectName: result.projectName as string,
147-
database: (result.database as ProjectDatabase) ?? "libsql",
148-
auth: (result.auth as boolean) ?? true,
149-
features: (result.features as ProjectFeature[]) ?? [],
150-
git: (result.git as boolean) ?? true,
151-
packageManager: (result.packageManager as PackageManager) ?? "npm",
180+
projectName: result.projectName ?? "",
181+
database: result.database ?? "libsql",
182+
auth: result.auth ?? true,
183+
features: result.features ?? [],
184+
git: result.git ?? true,
185+
packageManager: result.packageManager ?? "npm",
152186
};
153187
}
154188

0 commit comments

Comments
 (0)