mirror of
https://github.com/FranP-code/create-better-t-stack.git
synced 2025-10-12 23:52:15 +00:00
Prompt to overwrite non-empty dirs before config
This commit is contained in:
@@ -8,7 +8,6 @@ const INVALID_CHARS = ["<", ">", ":", '"', "|", "?", "*"];
|
||||
const MAX_LENGTH = 255;
|
||||
|
||||
function validateDirectoryName(name: string): string | undefined {
|
||||
// Allow "." as it represents current directory
|
||||
if (name === ".") return undefined;
|
||||
|
||||
if (!name) return "Project name cannot be empty";
|
||||
@@ -30,22 +29,12 @@ function validateDirectoryName(name: string): string | undefined {
|
||||
export async function getProjectName(initialName?: string): Promise<string> {
|
||||
if (initialName) {
|
||||
if (initialName === ".") {
|
||||
const projectDir = process.cwd();
|
||||
if (fs.readdirSync(projectDir).length === 0) {
|
||||
return initialName;
|
||||
}
|
||||
} else {
|
||||
const finalDirName = path.basename(initialName);
|
||||
const validationError = validateDirectoryName(finalDirName);
|
||||
if (!validationError) {
|
||||
const projectDir = path.resolve(process.cwd(), initialName);
|
||||
if (
|
||||
!fs.pathExistsSync(projectDir) ||
|
||||
fs.readdirSync(projectDir).length === 0
|
||||
) {
|
||||
return initialName;
|
||||
}
|
||||
}
|
||||
return initialName;
|
||||
}
|
||||
const finalDirName = path.basename(initialName);
|
||||
const validationError = validateDirectoryName(finalDirName);
|
||||
if (!validationError) {
|
||||
return initialName;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +43,10 @@ export async function getProjectName(initialName?: string): Promise<string> {
|
||||
let defaultName = DEFAULT_CONFIG.projectName;
|
||||
let counter = 1;
|
||||
|
||||
while (fs.pathExistsSync(path.resolve(process.cwd(), defaultName))) {
|
||||
while (
|
||||
fs.pathExistsSync(path.resolve(process.cwd(), defaultName)) &&
|
||||
fs.readdirSync(path.resolve(process.cwd(), defaultName)).length > 0
|
||||
) {
|
||||
defaultName = `${DEFAULT_CONFIG.projectName}-${counter}`;
|
||||
counter++;
|
||||
}
|
||||
@@ -69,33 +61,17 @@ export async function getProjectName(initialName?: string): Promise<string> {
|
||||
validate: (value) => {
|
||||
const nameToUse = value.trim() || defaultName;
|
||||
|
||||
if (nameToUse === ".") {
|
||||
const dirContents = fs.readdirSync(process.cwd());
|
||||
if (dirContents.length > 0) {
|
||||
return "Current directory is not empty. Please choose a different directory.";
|
||||
}
|
||||
isValid = true;
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const projectDir = path.resolve(process.cwd(), nameToUse);
|
||||
const finalDirName = path.basename(projectDir);
|
||||
|
||||
const finalDirName = path.basename(nameToUse);
|
||||
const validationError = validateDirectoryName(finalDirName);
|
||||
if (validationError) return validationError;
|
||||
|
||||
if (!projectDir.startsWith(process.cwd())) {
|
||||
return "Project path must be within current directory";
|
||||
}
|
||||
|
||||
if (fs.pathExistsSync(projectDir)) {
|
||||
const dirContents = fs.readdirSync(projectDir);
|
||||
if (dirContents.length > 0) {
|
||||
return `Directory "${nameToUse}" already exists and is not empty. Please choose a different name or path.`;
|
||||
if (nameToUse !== ".") {
|
||||
const projectDir = path.resolve(process.cwd(), nameToUse);
|
||||
if (!projectDir.startsWith(process.cwd())) {
|
||||
return "Project path must be within current directory";
|
||||
}
|
||||
}
|
||||
|
||||
isValid = true;
|
||||
return undefined;
|
||||
},
|
||||
});
|
||||
@@ -106,6 +82,7 @@ export async function getProjectName(initialName?: string): Promise<string> {
|
||||
}
|
||||
|
||||
projectPath = response || defaultName;
|
||||
isValid = true;
|
||||
}
|
||||
|
||||
return projectPath;
|
||||
|
||||
Reference in New Issue
Block a user