1+ import path from "node:path" ;
12import {
23 cancel ,
34 confirm ,
@@ -12,6 +13,7 @@ import {
1213} from "@clack/prompts" ;
1314import chalk from "chalk" ;
1415import { Command } from "commander" ;
16+ import fs from "fs-extra" ;
1517import { DEFAULT_CONFIG } from "./consts" ;
1618import { createProject } from "./helpers/create-project" ;
1719import { renderTitle } from "./render-title" ;
@@ -35,19 +37,51 @@ const program = new Command();
3537async 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