@@ -16,33 +16,30 @@ import {
16
16
createMetadata ,
17
17
createQuestions ,
18
18
type Answers ,
19
- type Args ,
20
19
} from './input' ;
21
20
import { applyTemplates , generateTemplateConfiguration } from './template' ;
22
- import { assertNpxExists , assertUserInput } from './utils/assert' ;
21
+ import { assertNpxExists } from './utils/assert' ;
23
22
import { createInitialGitCommit } from './utils/initialCommit' ;
24
23
import { prompt } from './utils/prompt' ;
25
24
import { resolveNpmPackageVersion } from './utils/resolveNpmPackageVersion' ;
26
25
import {
27
26
addNitroDependencyToLocalLibrary ,
28
27
linkLocalLibrary ,
29
- promptLocalLibrary ,
30
28
} from './utils/local' ;
31
29
import { determinePackageManager } from './utils/packageManager' ;
32
30
33
31
const FALLBACK_BOB_VERSION = '0.40.5' ;
34
32
const FALLBACK_NITRO_MODULES_VERSION = '0.22.1' ;
35
33
const SUPPORTED_REACT_NATIVE_VERSION = '0.78.2' ;
36
34
35
+ type Args = Partial < Answers > & {
36
+ $0 : string ;
37
+ [ key : string ] : unknown ;
38
+ } ;
39
+
37
40
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
38
41
yargs
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 )
46
43
. demandCommand ( )
47
44
. recommendCommands ( )
48
45
. fail ( printErrorHelp )
52
49
} )
53
50
. strict ( ) . argv ;
54
51
55
- async function create ( _argv : yargs . Arguments < Args > ) {
52
+ async function create ( _argv : Args ) {
56
53
// eslint-disable-next-line @typescript-eslint/no-unused-vars
57
54
const { _, $0, ...argv } = _argv ;
58
55
@@ -66,27 +63,20 @@ async function create(_argv: yargs.Arguments<Args>) {
66
63
FALLBACK_NITRO_MODULES_VERSION
67
64
) ;
68
65
69
- const local = await promptLocalLibrary ( argv ) ;
70
- const folder = await promptPath ( argv , local ) ;
71
-
72
66
await assertNpxExists ( ) ;
73
67
74
- const basename = path . basename ( folder ) ;
68
+ const questions = await createQuestions ( argv ) ;
75
69
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
+ } ) ;
79
73
80
- const promptAnswers = await prompt ( questions , argv ) ;
81
- const answers : Answers = {
74
+ const answers = {
82
75
...promptAnswers ,
83
76
reactNativeVersion :
84
77
promptAnswers . reactNativeVersion ?? SUPPORTED_REACT_NATIVE_VERSION ,
85
- local,
86
78
} ;
87
79
88
- assertUserInput ( questions , answers ) ;
89
-
90
80
const bobVersion = await bobVersionPromise ;
91
81
92
82
const nitroModulesVersion =
@@ -101,10 +91,12 @@ async function create(_argv: yargs.Arguments<Args>) {
101
91
// Nitro codegen's version is always the same as nitro modules version.
102
92
nitroCodegen : nitroModulesVersion ,
103
93
} ,
104
- basename,
94
+ basename : path . basename ( answers . name ) ,
105
95
answers,
106
96
} ) ;
107
97
98
+ const folder = path . resolve ( process . cwd ( ) , answers . name ) ;
99
+
108
100
await fs . mkdirp ( folder ) ;
109
101
110
102
if ( answers . reactNativeVersion !== SUPPORTED_REACT_NATIVE_VERSION ) {
@@ -148,76 +140,35 @@ async function create(_argv: yargs.Arguments<Args>) {
148
140
) } !\n`
149
141
) ;
150
142
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 ( ) ;
166
145
167
- const linkedLocalLibrary = await linkLocalLibrary (
168
- config ,
169
- folder ,
170
- packageManager
171
- ) ;
146
+ let addedNitro = false ;
172
147
173
- printSuccessMessage ( ) ;
148
+ if ( config . project . moduleConfig === 'nitro-modules' ) {
149
+ addedNitro = await addNitroDependencyToLocalLibrary ( config ) ;
150
+ }
174
151
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
+ ) ;
183
157
184
- async function promptPath ( argv : Args , local : boolean ) {
185
- let folder : string ;
158
+ printSuccessMessage ( ) ;
186
159
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,
209
166
} ) ;
167
+ } else {
168
+ await createInitialGitCommit ( folder ) ;
210
169
211
- folder = path . join ( process . cwd ( ) , answers . folder ) ;
212
- }
170
+ printSuccessMessage ( ) ;
213
171
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 ) ;
220
173
}
221
-
222
- return folder ;
223
174
}
0 commit comments