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