@@ -17,10 +17,21 @@ import {
1717 acceptedArgs ,
1818} from './input' ;
1919import { getDependencyVersionsFromExample } from './exampleApp/dependencies' ;
20- import { printNextSteps } from './nextSteps ' ;
20+ import { printErrorHelp , printNextSteps } from './inform ' ;
2121
2222const FALLBACK_BOB_VERSION = '0.32.0' ;
2323
24+ yargs
25+ . command ( '$0 [name]' , 'create a react native library' , acceptedArgs , create )
26+ . demandCommand ( )
27+ . recommendCommands ( )
28+ . fail ( printErrorHelp )
29+ . parserConfiguration ( {
30+ // don't pass kebab-case args to handler.
31+ 'strip-dashed' : true ,
32+ } )
33+ . strict ( ) . argv ;
34+
2435// FIXME: fix the type
2536// eslint-disable-next-line @typescript-eslint/no-explicit-any
2637async function create ( _argv : yargs . Arguments < any > ) {
@@ -29,66 +40,9 @@ async function create(_argv: yargs.Arguments<any>) {
2940
3041 // Prefetch bob version in background while asking questions
3142 const resolveBobVersion = resolveBobVersionWithFallback ( FALLBACK_BOB_VERSION ) ;
32- let local = false ;
33-
34- if ( typeof argv . local === 'boolean' ) {
35- local = argv . local ;
36- } else {
37- const hasPackageJson = await fs . pathExists (
38- path . join ( process . cwd ( ) , 'package.json' )
39- ) ;
4043
41- if ( hasPackageJson ) {
42- // If we're under a project with package.json, ask the user if they want to create a local library
43- const answers = await prompts ( {
44- type : 'confirm' ,
45- name : 'local' ,
46- message : `Looks like you're under a project folder. Do you want to create a local library?` ,
47- initial : true ,
48- } ) ;
49-
50- local = answers . local ;
51- }
52- }
53-
54- let folder : string ;
55-
56- if ( argv . name && ! local ) {
57- folder = path . join ( process . cwd ( ) , argv . name ) ;
58- } else {
59- const answers = await prompts ( {
60- type : 'text' ,
61- name : 'folder' ,
62- message : `Where do you want to create the library?` ,
63- initial :
64- local && argv . name && ! argv . name . includes ( '/' )
65- ? `modules/${ argv . name } `
66- : argv . name ,
67- validate : ( input ) => {
68- if ( ! input ) {
69- return 'Cannot be empty' ;
70- }
71-
72- if ( fs . pathExistsSync ( path . join ( process . cwd ( ) , input ) ) ) {
73- return 'Folder already exists' ;
74- }
75-
76- return true ;
77- } ,
78- } ) ;
79-
80- folder = path . join ( process . cwd ( ) , answers . folder ) ;
81- }
82-
83- if ( await fs . pathExists ( folder ) ) {
84- console . log (
85- `A folder already exists at ${ kleur . blue (
86- folder
87- ) } ! Please specify another folder name or delete the existing one.`
88- ) ;
89-
90- process . exit ( 1 ) ;
91- }
44+ const local = await promptLocalLibrary ( argv ) ;
45+ const folder = await promptPath ( argv , local ) ;
9246
9347 await assertNpx ( ) ;
9448
@@ -201,30 +155,71 @@ async function create(_argv: yargs.Arguments<any>) {
201155 await printNextSteps ( local , folder , config ) ;
202156}
203157
204- yargs
205- . command ( '$0 [name]' , 'create a react native library' , acceptedArgs , create )
206- . demandCommand ( )
207- . recommendCommands ( )
208- . fail ( ( message , error ) => {
209- console . log ( '\n' ) ;
158+ async function promptLocalLibrary ( argv : Record < string , string > ) {
159+ let local = false ;
210160
211- if ( error ) {
212- console . log ( kleur . red ( error . message ) ) ;
213- throw error ;
214- }
161+ if ( typeof argv . local === 'boolean' ) {
162+ local = argv . local ;
163+ } else {
164+ const hasPackageJson = await fs . pathExists (
165+ path . join ( process . cwd ( ) , 'package.json' )
166+ ) ;
215167
216- if ( message ) {
217- console . log ( kleur . red ( message ) ) ;
218- } else {
219- console . log (
220- kleur . red ( `An unknown error occurred. See '--help' for usage guide.` )
221- ) ;
168+ if ( hasPackageJson ) {
169+ // If we're under a project with package.json, ask the user if they want to create a local library
170+ const answers = await prompts ( {
171+ type : 'confirm' ,
172+ name : 'local' ,
173+ message : `Looks like you're under a project folder. Do you want to create a local library?` ,
174+ initial : true ,
175+ } ) ;
176+
177+ local = answers . local ;
222178 }
179+ }
180+
181+ return local ;
182+ }
183+
184+ async function promptPath ( argv : Record < string , string > , local : boolean ) {
185+ let folder : string ;
186+
187+ if ( argv . name && ! local ) {
188+ folder = path . join ( process . cwd ( ) , argv . name ) ;
189+ } else {
190+ const answers = await prompts ( {
191+ type : 'text' ,
192+ name : 'folder' ,
193+ message : `Where do you want to create the library?` ,
194+ initial :
195+ local && argv . name && ! argv . name . includes ( '/' )
196+ ? `modules/${ argv . name } `
197+ : argv . name ,
198+ validate : ( input ) => {
199+ if ( ! input ) {
200+ return 'Cannot be empty' ;
201+ }
202+
203+ if ( fs . pathExistsSync ( path . join ( process . cwd ( ) , input ) ) ) {
204+ return 'Folder already exists' ;
205+ }
206+
207+ return true ;
208+ } ,
209+ } ) ;
210+
211+ folder = path . join ( process . cwd ( ) , answers . folder ) ;
212+ }
213+
214+ if ( await fs . pathExists ( folder ) ) {
215+ console . log (
216+ `A folder already exists at ${ kleur . blue (
217+ folder
218+ ) } ! Please specify another folder name or delete the existing one.`
219+ ) ;
223220
224221 process . exit ( 1 ) ;
225- } )
226- . parserConfiguration ( {
227- // don't pass kebab-case args to handler.
228- 'strip-dashed' : true ,
229- } )
230- . strict ( ) . argv ;
222+ }
223+
224+ return folder ;
225+ }
0 commit comments