Prompt to overwrite non-empty dirs before config

This commit is contained in:
Aman Varshney
2025-05-05 18:54:23 +05:30
parent ff13123bcb
commit 357dfbbbf9
29 changed files with 223 additions and 141 deletions

View File

@@ -25,11 +25,9 @@ import { getGitChoice } from "./git";
import { getinstallChoice } from "./install";
import { getORMChoice } from "./orm";
import { getPackageManagerChoice } from "./package-manager";
import { getProjectName } from "./project-name";
import { getRuntimeChoice } from "./runtime";
type PromptGroupResults = {
projectName: string;
frontend: ProjectFrontend[];
backend: ProjectBackend;
runtime: ProjectRuntime;
@@ -47,12 +45,12 @@ type PromptGroupResults = {
export async function gatherConfig(
flags: Partial<ProjectConfig>,
projectName: string,
projectDir: string,
relativePath: string,
): Promise<ProjectConfig> {
const result = await group<PromptGroupResults>(
{
projectName: async () => {
return getProjectName(flags.projectName);
},
frontend: ({ results }) =>
getFrontendChoice(flags.frontend, flags.backend),
backend: ({ results }) =>
@@ -109,7 +107,9 @@ export async function gatherConfig(
}
return {
projectName: result.projectName,
projectName: projectName,
projectDir: projectDir,
relativePath: relativePath,
frontend: result.frontend,
backend: result.backend,
runtime: result.runtime,

View File

@@ -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;