@@ -16,33 +16,30 @@ 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+ $0 : string ;
37+ [ key : string ] : unknown ;
38+ } ;
39+
3740// eslint-disable-next-line @typescript-eslint/no-unused-expressions
3841yargs
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- )
42+ . command ( '$0 [name]' , 'create a react native library' , acceptedArgs , create )
4643 . demandCommand ( )
4744 . recommendCommands ( )
4845 . fail ( printErrorHelp )
5249 } )
5350 . strict ( ) . argv ;
5451
55- async function create ( _argv : yargs . Arguments < Args > ) {
52+ async function create ( _argv : Args ) {
5653 // eslint-disable-next-line @typescript-eslint/no-unused-vars
5754 const { _, $0, ...argv } = _argv ;
5855
@@ -66,27 +63,20 @@ async function create(_argv: yargs.Arguments<Args>) {
6663 FALLBACK_NITRO_MODULES_VERSION
6764 ) ;
6865
69- const local = await promptLocalLibrary ( argv ) ;
70- const folder = await promptPath ( argv , local ) ;
71-
7266 await assertNpxExists ( ) ;
7367
74- const basename = path . basename ( folder ) ;
68+ const questions = await createQuestions ( argv ) ;
7569
76- const questions = await createQuestions ( { basename , local } ) ;
77-
78- assertUserInput ( questions , argv ) ;
70+ const promptAnswers = await prompt < keyof Answers > ( questions , argv , {
71+ interactive : argv . interactive ,
72+ } ) ;
7973
80- const promptAnswers = await prompt ( questions , argv ) ;
81- const answers : Answers = {
74+ const answers = {
8275 ...promptAnswers ,
8376 reactNativeVersion :
8477 promptAnswers . reactNativeVersion ?? SUPPORTED_REACT_NATIVE_VERSION ,
85- local,
8678 } ;
8779
88- assertUserInput ( questions , answers ) ;
89-
9080 const bobVersion = await bobVersionPromise ;
9181
9282 const nitroModulesVersion =
@@ -101,10 +91,12 @@ async function create(_argv: yargs.Arguments<Args>) {
10191 // Nitro codegen's version is always the same as nitro modules version.
10292 nitroCodegen : nitroModulesVersion ,
10393 } ,
104- basename,
94+ basename : path . basename ( answers . name ) ,
10595 answers,
10696 } ) ;
10797
98+ const folder = path . resolve ( process . cwd ( ) , answers . name ) ;
99+
108100 await fs . mkdirp ( folder ) ;
109101
110102 if ( answers . reactNativeVersion !== SUPPORTED_REACT_NATIVE_VERSION ) {
@@ -148,76 +140,35 @@ async function create(_argv: yargs.Arguments<Args>) {
148140 ) } !\n`
149141 ) ;
150142
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- }
143+ if ( answers . local ) {
144+ const packageManager = await determinePackageManager ( ) ;
166145
167- const linkedLocalLibrary = await linkLocalLibrary (
168- config ,
169- folder ,
170- packageManager
171- ) ;
146+ let addedNitro = false ;
172147
173- printSuccessMessage ( ) ;
148+ if ( config . project . moduleConfig === 'nitro-modules' ) {
149+ addedNitro = await addNitroDependencyToLocalLibrary ( config ) ;
150+ }
174151
175- printLocalLibNextSteps ( {
176- config,
177- packageManager,
178- linkedLocalLibrary,
179- addedNitro,
180- folder,
181- } ) ;
182- }
152+ const linkedLocalLibrary = await linkLocalLibrary (
153+ config ,
154+ folder ,
155+ packageManager
156+ ) ;
183157
184- async function promptPath ( argv : Args , local : boolean ) {
185- let folder : string ;
158+ printSuccessMessage ( ) ;
186159
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- } ,
160+ printLocalLibNextSteps ( {
161+ config,
162+ packageManager,
163+ linkedLocalLibrary,
164+ addedNitro,
165+ folder,
209166 } ) ;
167+ } else {
168+ await createInitialGitCommit ( folder ) ;
210169
211- folder = path . join ( process . cwd ( ) , answers . folder ) ;
212- }
170+ printSuccessMessage ( ) ;
213171
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- ) ;
172+ printNonLocalLibNextSteps ( config ) ;
220173 }
221-
222- return folder ;
223174}
0 commit comments