@@ -196,6 +196,33 @@ export async function createQuestions({
196196 // Ignore error
197197 }
198198
199+ const validateDirectory = ( input : string ) => {
200+ if ( ! input ) {
201+ return 'Cannot be empty' ;
202+ }
203+
204+ const targetPath = path . join ( process . cwd ( ) , input ) ;
205+
206+ if ( fs . pathExistsSync ( targetPath ) ) {
207+ const stat = fs . statSync ( targetPath ) ;
208+
209+ if ( ! stat . isDirectory ( ) ) {
210+ return 'Path exists and is not a directory' ;
211+ }
212+
213+ const files = fs . readdirSync ( targetPath ) ;
214+
215+ const isEmpty =
216+ files . length === 0 || ( files . length === 1 && files [ 0 ] === '.git' ) ;
217+
218+ if ( ! isEmpty ) {
219+ return 'Directory already exists' ;
220+ }
221+ }
222+
223+ return true ;
224+ } ;
225+
199226 const questions : Question < keyof PromptAnswers > [ ] = [
200227 {
201228 type :
@@ -209,7 +236,17 @@ export async function createQuestions({
209236 default : false ,
210237 } ,
211238 {
212- type : ( _ , answers ) => ( name && ! ( answers . local ?? local ) ? null : 'text' ) ,
239+ type : ( _ , answers ) => {
240+ if (
241+ name &&
242+ ! ( answers . local ?? local ) &&
243+ validateDirectory ( name ) === true
244+ ) {
245+ return null ;
246+ }
247+
248+ return 'text' ;
249+ } ,
213250 name : 'directory' ,
214251 message : `Where do you want to create the library?` ,
215252 initial : ( _ , answers ) => {
@@ -219,32 +256,7 @@ export async function createQuestions({
219256
220257 return name ?? '' ;
221258 } ,
222- validate : ( input ) => {
223- if ( ! input ) {
224- return 'Cannot be empty' ;
225- }
226-
227- const targetPath = path . join ( process . cwd ( ) , input ) ;
228-
229- if ( fs . pathExistsSync ( targetPath ) ) {
230- const stat = fs . statSync ( targetPath ) ;
231-
232- if ( ! stat . isDirectory ( ) ) {
233- return 'Path exists and is not a directory' ;
234- }
235-
236- const files = fs . readdirSync ( targetPath ) ;
237-
238- const isEmpty =
239- files . length === 0 || ( files . length === 1 && files [ 0 ] === '.git' ) ;
240-
241- if ( ! isEmpty ) {
242- return 'Directory already exists' ;
243- }
244- }
245-
246- return true ;
247- } ,
259+ validate : validateDirectory ,
248260 default : name ,
249261 } ,
250262 {
0 commit comments