Skip to content

Commit 03571ca

Browse files
allow specifying project path in prompt
1 parent ff4e6aa commit 03571ca

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

.changeset/eleven-pugs-obey.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+
allow specifying project path in prompt

apps/cli/src/prompts/project-name.ts

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import pc from "picocolors";
55
import { DEFAULT_CONFIG } from "../constants";
66

77
const INVALID_CHARS = ["<", ">", ":", '"', "|", "?", "*"];
8-
98
const MAX_LENGTH = 255;
109

1110
function validateDirectoryName(name: string): string | undefined {
11+
// Allow "." as it represents current directory
12+
if (name === ".") return undefined;
13+
1214
if (!name) return "Project name cannot be empty";
1315
if (name.length > MAX_LENGTH) {
1416
return `Project name must be less than ${MAX_LENGTH} characters`;
@@ -30,21 +32,28 @@ function validateDirectoryName(name: string): string | undefined {
3032

3133
export async function getProjectName(initialName?: string): Promise<string> {
3234
if (initialName) {
33-
const finalDirName = path.basename(initialName);
34-
const validationError = validateDirectoryName(finalDirName);
35-
if (!validationError) {
36-
const projectDir = path.resolve(process.cwd(), initialName);
37-
if (
38-
!fs.pathExistsSync(projectDir) ||
39-
fs.readdirSync(projectDir).length === 0
40-
) {
35+
if (initialName === ".") {
36+
const projectDir = process.cwd();
37+
if (fs.readdirSync(projectDir).length === 0) {
4138
return initialName;
4239
}
40+
} else {
41+
const finalDirName = path.basename(initialName);
42+
const validationError = validateDirectoryName(finalDirName);
43+
if (!validationError) {
44+
const projectDir = path.resolve(process.cwd(), initialName);
45+
if (
46+
!fs.pathExistsSync(projectDir) ||
47+
fs.readdirSync(projectDir).length === 0
48+
) {
49+
return initialName;
50+
}
51+
}
4352
}
4453
}
4554

4655
let isValid = false;
47-
let projectName = "";
56+
let projectPath = "";
4857
let defaultName = DEFAULT_CONFIG.projectName;
4958
let counter = 1;
5059

@@ -55,26 +64,37 @@ export async function getProjectName(initialName?: string): Promise<string> {
5564

5665
while (!isValid) {
5766
const response = await text({
58-
message: "What is your project named? (directory name or path)",
67+
message:
68+
"Enter your project name or path (relative to current directory)",
5969
placeholder: defaultName,
6070
initialValue: initialName,
6171
defaultValue: defaultName,
6272
validate: (value) => {
6373
const nameToUse = value.trim() || defaultName;
6474

65-
const finalDirName = path.basename(nameToUse);
75+
if (nameToUse === ".") {
76+
const dirContents = fs.readdirSync(process.cwd());
77+
if (dirContents.length > 0) {
78+
return "Current directory is not empty. Please choose a different directory.";
79+
}
80+
isValid = true;
81+
return undefined;
82+
}
83+
84+
const projectDir = path.resolve(process.cwd(), nameToUse);
85+
const finalDirName = path.basename(projectDir);
86+
6687
const validationError = validateDirectoryName(finalDirName);
6788
if (validationError) return validationError;
6889

69-
const projectDir = path.resolve(process.cwd(), nameToUse);
7090
if (!projectDir.startsWith(process.cwd())) {
7191
return "Project path must be within current directory";
7292
}
7393

7494
if (fs.pathExistsSync(projectDir)) {
7595
const dirContents = fs.readdirSync(projectDir);
7696
if (dirContents.length > 0) {
77-
return `Directory "${nameToUse}" already exists and is not empty. Please choose a different name.`;
97+
return `Directory "${nameToUse}" already exists and is not empty. Please choose a different name or path.`;
7898
}
7999
}
80100

@@ -88,8 +108,8 @@ export async function getProjectName(initialName?: string): Promise<string> {
88108
process.exit(0);
89109
}
90110

91-
projectName = response || defaultName;
111+
projectPath = response || defaultName;
92112
}
93113

94-
return projectName;
114+
return projectPath;
95115
}

0 commit comments

Comments
 (0)