@@ -146,7 +146,7 @@ export const acceptedArgs = {
146
146
147
147
export type ExampleApp = 'none' | 'test-app' | 'expo' | 'vanilla' ;
148
148
149
- export type Answers = {
149
+ type PromptAnswers = {
150
150
directory : string ;
151
151
slug : string ;
152
152
description : string ;
@@ -157,8 +157,12 @@ export type Answers = {
157
157
languages : ProjectLanguages ;
158
158
type : ProjectType ;
159
159
example : ExampleApp ;
160
- reactNativeVersion : string ;
161
- local ?: boolean ;
160
+ local : boolean ;
161
+ } ;
162
+
163
+ export type Answers = PromptAnswers & {
164
+ name ?: string ;
165
+ reactNativeVersion ?: string ;
162
166
interactive ?: boolean ;
163
167
} ;
164
168
@@ -178,7 +182,7 @@ export async function createQuestions({
178
182
// Ignore error
179
183
}
180
184
181
- const questions : Question < keyof Answers > [ ] = [
185
+ const questions : Question < keyof PromptAnswers > [ ] = [
182
186
{
183
187
type :
184
188
local == null &&
@@ -194,7 +198,7 @@ export async function createQuestions({
194
198
type : ( _ , answers ) => ( name && ! ( answers . local ?? local ) ? null : 'text' ) ,
195
199
name : 'directory' ,
196
200
message : `Where do you want to create the library?` ,
197
- initial : ( _ : string , answers : Answers ) => {
201
+ initial : ( _ , answers ) => {
198
202
if ( ( answers . local ?? local ) && name && ! name ?. includes ( '/' ) ) {
199
203
return `modules/${ name } ` ;
200
204
}
@@ -207,7 +211,7 @@ export async function createQuestions({
207
211
}
208
212
209
213
if ( fs . pathExistsSync ( path . join ( process . cwd ( ) , input ) ) ) {
210
- return 'Folder already exists' ;
214
+ return 'Directory already exists' ;
211
215
}
212
216
213
217
return true ;
@@ -218,8 +222,10 @@ export async function createQuestions({
218
222
type : 'text' ,
219
223
name : 'slug' ,
220
224
message : 'What is the name of the npm package?' ,
221
- initial : ( _ : string , answers : Answers ) => {
222
- const basename = path . basename ( answers . directory ?? name ?? '' ) ;
225
+ initial : ( _ , answers ) => {
226
+ const basename = path . basename (
227
+ typeof answers . directory === 'string' ? answers . directory : name ?? ''
228
+ ) ;
223
229
224
230
if ( validateNpmPackage ( basename ) . validForNewPackages ) {
225
231
if ( / ^ ( @ | r e a c t - n a t i v e ) / . test ( basename ) ) {
@@ -260,8 +266,11 @@ export async function createQuestions({
260
266
type : ( _ , answers ) => ( answers . local ?? local ? null : 'text' ) ,
261
267
name : 'authorUrl' ,
262
268
message : 'What is the URL for the package author?' ,
263
- // @ts -expect-error this is supported, but types are wrong
264
- initial : async ( _ : string , answers : Answers ) => {
269
+ initial : async ( _ , answers ) => {
270
+ if ( typeof answers . authorEmail !== 'string' ) {
271
+ return '' ;
272
+ }
273
+
265
274
try {
266
275
const username = await githubUsername ( answers . authorEmail ) ;
267
276
@@ -270,16 +279,20 @@ export async function createQuestions({
270
279
// Ignore error
271
280
}
272
281
273
- return undefined ;
282
+ return '' ;
274
283
} ,
275
284
validate : ( input ) => / ^ h t t p s ? : \/ \/ / . test ( input ) || 'Must be a valid URL' ,
276
285
} ,
277
286
{
278
287
type : ( _ , answers ) => ( answers . local ?? local ? null : 'text' ) ,
279
288
name : 'repoUrl' ,
280
289
message : 'What is the URL for the repository?' ,
281
- initial : ( _ : string , answers : Answers ) => {
282
- if ( / ^ h t t p s ? : \/ \/ g i t h u b .c o m \/ [ ^ / ] + / . test ( answers . authorUrl ) ) {
290
+ initial : ( _ , answers ) => {
291
+ if (
292
+ typeof answers . authorUrl === 'string' &&
293
+ typeof answers . slug === 'string' &&
294
+ / ^ h t t p s ? : \/ \/ g i t h u b .c o m \/ [ ^ / ] + / . test ( answers . authorUrl )
295
+ ) {
283
296
return `${ answers . authorUrl } /${ answers . slug
284
297
. replace ( / ^ @ / , '' )
285
298
. replace ( / \/ / g, '-' ) } `;
@@ -301,7 +314,8 @@ export async function createQuestions({
301
314
message : 'Which languages do you want to use?' ,
302
315
choices : ( _ , values ) => {
303
316
return LANGUAGE_CHOICES . filter ( ( choice ) => {
304
- if ( choice . types ) {
317
+ if ( choice . types && typeof values . type === 'string' ) {
318
+ // @ts -expect-error `includes doesn't support checking arbitrary types
305
319
return choice . types . includes ( values . type ) ;
306
320
}
307
321
@@ -339,9 +353,9 @@ export async function createQuestions({
339
353
return questions ;
340
354
}
341
355
342
- export function createMetadata ( answers : Answers ) {
356
+ export function createMetadata ( answers : Partial < PromptAnswers > ) {
343
357
// Some of the passed args can already be derived from the generated package.json file.
344
- const ignoredAnswers : ( keyof Answers | 'name' ) [ ] = [
358
+ const ignoredAnswers : ( keyof Answers ) [ ] = [
345
359
'name' ,
346
360
'directory' ,
347
361
'slug' ,
@@ -356,9 +370,9 @@ export function createMetadata(answers: Answers) {
356
370
'interactive' ,
357
371
] ;
358
372
359
- type AnswerEntries < T extends keyof Answers = keyof Answers > = [
373
+ type AnswerEntries < T extends keyof PromptAnswers = keyof PromptAnswers > = [
360
374
T ,
361
- Answers [ T ] ,
375
+ PromptAnswers [ T ] ,
362
376
] [ ] ;
363
377
364
378
const libraryMetadata = Object . fromEntries (
0 commit comments