@@ -189,6 +189,33 @@ export async function createQuestions({
189189 // Ignore error
190190 }
191191
192+ const validateDirectory = ( input : string ) => {
193+ if ( ! input ) {
194+ return 'Cannot be empty' ;
195+ }
196+
197+ const targetPath = path . join ( process . cwd ( ) , input ) ;
198+
199+ if ( fs . pathExistsSync ( targetPath ) ) {
200+ const stat = fs . statSync ( targetPath ) ;
201+
202+ if ( ! stat . isDirectory ( ) ) {
203+ return 'Path exists and is not a directory' ;
204+ }
205+
206+ const files = fs . readdirSync ( targetPath ) ;
207+
208+ const isEmpty =
209+ files . length === 0 || ( files . length === 1 && files [ 0 ] === '.git' ) ;
210+
211+ if ( ! isEmpty ) {
212+ return 'Directory already exists' ;
213+ }
214+ }
215+
216+ return true ;
217+ } ;
218+
192219 const questions : Question < keyof PromptAnswers > [ ] = [
193220 {
194221 type :
@@ -202,7 +229,17 @@ export async function createQuestions({
202229 default : false ,
203230 } ,
204231 {
205- type : ( _ , answers ) => ( name && ! ( answers . local ?? local ) ? null : 'text' ) ,
232+ type : ( _ , answers ) => {
233+ if (
234+ name &&
235+ ! ( answers . local ?? local ) &&
236+ validateDirectory ( name ) === true
237+ ) {
238+ return null ;
239+ }
240+
241+ return 'text' ;
242+ } ,
206243 name : 'directory' ,
207244 message : `Where do you want to create the library?` ,
208245 initial : ( _ , answers ) => {
@@ -212,32 +249,7 @@ export async function createQuestions({
212249
213250 return name ?? '' ;
214251 } ,
215- validate : ( input ) => {
216- if ( ! input ) {
217- return 'Cannot be empty' ;
218- }
219-
220- const targetPath = path . join ( process . cwd ( ) , input ) ;
221-
222- if ( fs . pathExistsSync ( targetPath ) ) {
223- const stat = fs . statSync ( targetPath ) ;
224-
225- if ( ! stat . isDirectory ( ) ) {
226- return 'Path exists and is not a directory' ;
227- }
228-
229- const files = fs . readdirSync ( targetPath ) ;
230-
231- const isEmpty =
232- files . length === 0 || ( files . length === 1 && files [ 0 ] === '.git' ) ;
233-
234- if ( ! isEmpty ) {
235- return 'Directory already exists' ;
236- }
237- }
238-
239- return true ;
240- } ,
252+ validate : validateDirectory ,
241253 default : name ,
242254 } ,
243255 {
0 commit comments