Skip to content

Commit ef0953b

Browse files
committed
fix: properly handle example argument
1 parent 8278040 commit ef0953b

File tree

1 file changed

+94
-51
lines changed
  • packages/create-react-native-library/src

1 file changed

+94
-51
lines changed

packages/create-react-native-library/src/index.ts

Lines changed: 94 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,24 @@ const TYPE_CHOICES: {
217217
},
218218
];
219219

220+
const EXAMPLE_CHOICES = [
221+
{
222+
title: 'Test app',
223+
value: 'test-app',
224+
description: "app's native code is abstracted away",
225+
},
226+
{
227+
title: 'Vanilla',
228+
value: 'vanilla',
229+
description: "provides access to app's native code",
230+
},
231+
{
232+
title: 'Expo',
233+
value: 'expo',
234+
description: 'managed expo project with web support',
235+
},
236+
] as const;
237+
220238
const args: Record<ArgName, yargs.Options> = {
221239
'slug': {
222240
description: 'Name of the npm package',
@@ -261,8 +279,7 @@ const args: Record<ArgName, yargs.Options> = {
261279
'example': {
262280
description: 'Type of the app to create',
263281
type: 'string',
264-
choices: ['vanilla', 'test-app'],
265-
default: 'vanilla',
282+
choices: EXAMPLE_CHOICES.map(({ value }) => value),
266283
},
267284
};
268285

@@ -451,64 +468,78 @@ async function create(argv: yargs.Arguments<any>) {
451468
});
452469
},
453470
},
454-
};
455-
456-
// Validate arguments passed to the CLI
457-
for (const [key, value] of Object.entries(argv)) {
458-
if (value == null) {
459-
continue;
460-
}
471+
'example': {
472+
type: 'select',
473+
name: 'example',
474+
message: 'What type of example app do you want to create?',
475+
choices: (_, values) => {
476+
return EXAMPLE_CHOICES.filter((choice) => {
477+
if (values.type) {
478+
return values.type === 'library'
479+
? choice.value === 'expo'
480+
: choice.value !== 'expo';
481+
}
461482

462-
const question = questions[key as ArgName];
483+
return true;
484+
});
485+
},
486+
},
487+
};
463488

464-
if (question == null) {
465-
continue;
466-
}
489+
const validate = (answers: Answers) => {
490+
for (const [key, value] of Object.entries(answers)) {
491+
if (value == null) {
492+
continue;
493+
}
467494

468-
let valid = question.validate ? question.validate(String(value)) : true;
495+
const question = questions[key as ArgName];
469496

470-
// We also need to guard against invalid choices
471-
// If we don't already have a validation message to provide a better error
472-
if (typeof valid !== 'string' && 'choices' in question) {
473-
const choices =
474-
typeof question.choices === 'function'
475-
? question.choices(undefined, argv, question)
476-
: question.choices;
497+
if (question == null) {
498+
continue;
499+
}
477500

478-
if (choices && !choices.some((choice) => choice.value === value)) {
479-
valid = `Supported values are - ${choices.map((c) =>
480-
kleur.green(c.value)
481-
)}`;
501+
let valid = question.validate ? question.validate(String(value)) : true;
502+
503+
// We also need to guard against invalid choices
504+
// If we don't already have a validation message to provide a better error
505+
if (typeof valid !== 'string' && 'choices' in question) {
506+
const choices =
507+
typeof question.choices === 'function'
508+
? question.choices(
509+
undefined,
510+
// @ts-expect-error: it complains about optional values, but it should be fine
511+
answers,
512+
question
513+
)
514+
: question.choices;
515+
516+
if (choices && !choices.some((choice) => choice.value === value)) {
517+
valid = `Supported values are - ${choices
518+
.map((c) => kleur.green(c.value))
519+
.join(', ')}`;
520+
}
482521
}
483-
}
484522

485-
if (valid !== true) {
486-
let message = `Invalid value ${kleur.red(
487-
String(value)
488-
)} passed for ${kleur.blue(key)}`;
523+
if (valid !== true) {
524+
let message = `Invalid value ${kleur.red(
525+
String(value)
526+
)} passed for ${kleur.blue(key)}`;
489527

490-
if (typeof valid === 'string') {
491-
message += `: ${valid}`;
492-
}
528+
if (typeof valid === 'string') {
529+
message += `: ${valid}`;
530+
}
493531

494-
console.log(message);
532+
console.log(message);
495533

496-
process.exit(1);
534+
process.exit(1);
535+
}
497536
}
498-
}
537+
};
499538

500-
const {
501-
slug,
502-
description,
503-
authorName,
504-
authorEmail,
505-
authorUrl,
506-
repoUrl,
507-
type = 'module-mixed',
508-
languages = type === 'library' ? 'js' : 'java-objc',
509-
example: hasExample,
510-
reactNativeVersion,
511-
} = {
539+
// Validate arguments passed to the CLI
540+
validate(argv);
541+
542+
const answers = {
512543
...argv,
513544
...(await prompts(
514545
Object.entries(questions)
@@ -557,6 +588,21 @@ async function create(argv: yargs.Arguments<any>) {
557588
)),
558589
} as Answers;
559590

591+
validate(answers);
592+
593+
const {
594+
slug,
595+
description,
596+
authorName,
597+
authorEmail,
598+
authorUrl,
599+
repoUrl,
600+
type = 'module-mixed',
601+
languages = type === 'library' ? 'js' : 'java-objc',
602+
example = local ? null : type === 'library' ? 'expo' : 'test-app',
603+
reactNativeVersion,
604+
} = answers;
605+
560606
// Get latest version of Bob from NPM
561607
let version: string;
562608

@@ -580,9 +626,6 @@ async function create(argv: yargs.Arguments<any>) {
580626
? 'mixed'
581627
: 'legacy';
582628

583-
const example =
584-
hasExample && !local ? (type === 'library' ? 'expo' : hasExample) : null;
585-
586629
const project = slug.replace(/^(react-native-|@[^/]+\/)/, '');
587630

588631
let namespace: string | undefined;

0 commit comments

Comments
 (0)