@@ -16,33 +16,31 @@ import {
1616 createMetadata ,
1717 createQuestions ,
1818 type Answers ,
19- type Args ,
2019} from './input' ;
2120import { applyTemplates , generateTemplateConfiguration } from './template' ;
22- import { assertNpxExists , assertUserInput } from './utils/assert' ;
21+ import { assertNpxExists } from './utils/assert' ;
2322import { createInitialGitCommit } from './utils/initialCommit' ;
2423import { prompt } from './utils/prompt' ;
2524import { resolveNpmPackageVersion } from './utils/resolveNpmPackageVersion' ;
2625import {
2726 addNitroDependencyToLocalLibrary ,
2827 linkLocalLibrary ,
29- promptLocalLibrary ,
3028} from './utils/local' ;
3129import { determinePackageManager } from './utils/packageManager' ;
3230
3331const FALLBACK_BOB_VERSION = '0.40.5' ;
3432const FALLBACK_NITRO_MODULES_VERSION = '0.22.1' ;
3533const SUPPORTED_REACT_NATIVE_VERSION = '0.78.2' ;
3634
35+ type Args = Partial < Answers > & {
36+ name ?: string ;
37+ $0 : string ;
38+ [ key : string ] : unknown ;
39+ } ;
40+
3741// eslint-disable-next-line @typescript-eslint/no-unused-expressions
3842yargs
39- . command (
40- '$0 [name]' ,
41- 'create a react native library' ,
42- acceptedArgs ,
43- // @ts -expect-error Some types are still incompatible
44- create
45- )
43+ . command ( '$0 [name]' , 'create a react native library' , acceptedArgs , create )
4644 . demandCommand ( )
4745 . recommendCommands ( )
4846 . fail ( printErrorHelp )
5250 } )
5351 . strict ( ) . argv ;
5452
55- async function create ( _argv : yargs . Arguments < Args > ) {
53+ async function create ( _argv : Args ) {
5654 // eslint-disable-next-line @typescript-eslint/no-unused-vars
5755 const { _, $0, ...argv } = _argv ;
5856
@@ -66,27 +64,20 @@ async function create(_argv: yargs.Arguments<Args>) {
6664 FALLBACK_NITRO_MODULES_VERSION
6765 ) ;
6866
69- const local = await promptLocalLibrary ( argv ) ;
70- const folder = await promptPath ( argv , local ) ;
71-
7267 await assertNpxExists ( ) ;
7368
74- const basename = path . basename ( folder ) ;
69+ const questions = await createQuestions ( argv ) ;
7570
76- const questions = await createQuestions ( { basename , local } ) ;
77-
78- assertUserInput ( questions , argv ) ;
71+ const promptAnswers = await prompt < Answers , typeof argv > ( questions , argv , {
72+ interactive : argv . interactive ,
73+ } ) ;
7974
80- const promptAnswers = await prompt ( questions , argv ) ;
81- const answers : Answers = {
75+ const answers = {
8276 ...promptAnswers ,
8377 reactNativeVersion :
8478 promptAnswers . reactNativeVersion ?? SUPPORTED_REACT_NATIVE_VERSION ,
85- local,
8679 } ;
8780
88- assertUserInput ( questions , answers ) ;
89-
9081 const bobVersion = await bobVersionPromise ;
9182
9283 const nitroModulesVersion =
@@ -101,10 +92,12 @@ async function create(_argv: yargs.Arguments<Args>) {
10192 // Nitro codegen's version is always the same as nitro modules version.
10293 nitroCodegen : nitroModulesVersion ,
10394 } ,
104- basename,
95+ basename : path . basename ( answers . name ?? answers . directory ) ,
10596 answers,
10697 } ) ;
10798
99+ const folder = path . resolve ( process . cwd ( ) , answers . directory ) ;
100+
108101 await fs . mkdirp ( folder ) ;
109102
110103 if ( answers . reactNativeVersion !== SUPPORTED_REACT_NATIVE_VERSION ) {
@@ -148,76 +141,35 @@ async function create(_argv: yargs.Arguments<Args>) {
148141 ) } !\n`
149142 ) ;
150143
151- if ( ! local ) {
152- await createInitialGitCommit ( folder ) ;
153-
154- printSuccessMessage ( ) ;
155-
156- printNonLocalLibNextSteps ( config ) ;
157- return ;
158- }
159-
160- const packageManager = await determinePackageManager ( ) ;
161-
162- let addedNitro = false ;
163- if ( config . project . moduleConfig === 'nitro-modules' ) {
164- addedNitro = await addNitroDependencyToLocalLibrary ( config ) ;
165- }
144+ if ( answers . local ) {
145+ const packageManager = await determinePackageManager ( ) ;
166146
167- const linkedLocalLibrary = await linkLocalLibrary (
168- config ,
169- folder ,
170- packageManager
171- ) ;
147+ let addedNitro = false ;
172148
173- printSuccessMessage ( ) ;
149+ if ( config . project . moduleConfig === 'nitro-modules' ) {
150+ addedNitro = await addNitroDependencyToLocalLibrary ( config ) ;
151+ }
174152
175- printLocalLibNextSteps ( {
176- config,
177- packageManager,
178- linkedLocalLibrary,
179- addedNitro,
180- folder,
181- } ) ;
182- }
153+ const linkedLocalLibrary = await linkLocalLibrary (
154+ config ,
155+ folder ,
156+ packageManager
157+ ) ;
183158
184- async function promptPath ( argv : Args , local : boolean ) {
185- let folder : string ;
159+ printSuccessMessage ( ) ;
186160
187- if ( argv . name && ! local ) {
188- folder = path . join ( process . cwd ( ) , argv . name ) ;
189- } else {
190- const answers = await prompt ( {
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- } ,
161+ printLocalLibNextSteps ( {
162+ config,
163+ packageManager,
164+ linkedLocalLibrary,
165+ addedNitro,
166+ folder,
209167 } ) ;
168+ } else {
169+ await createInitialGitCommit ( folder ) ;
210170
211- folder = path . join ( process . cwd ( ) , answers . folder ) ;
212- }
171+ printSuccessMessage ( ) ;
213172
214- if ( await fs . pathExists ( folder ) ) {
215- throw new Error (
216- `A folder already exists at ${ kleur . blue (
217- folder
218- ) } ! Please specify another folder name or delete the existing one.`
219- ) ;
173+ printNonLocalLibNextSteps ( config ) ;
220174 }
221-
222- return folder ;
223175}
0 commit comments