Skip to content

Commit 03defcf

Browse files
committed
fix: validate arguments passed to the CLI
1 parent 2744c0a commit 03defcf

File tree

1 file changed

+85
-6
lines changed
  • packages/create-react-native-library/src

1 file changed

+85
-6
lines changed

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

Lines changed: 85 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,32 @@ type Answers = {
107107
const LANGUAGE_CHOICES: {
108108
title: string;
109109
value: ProjectLanguages;
110-
types?: ProjectType[];
110+
types: ProjectType[];
111111
}[] = [
112-
{ title: 'Java & Objective-C', value: 'java-objc' },
113-
{ title: 'Kotlin & Objective-C', value: 'kotlin-objc' },
112+
{
113+
title: 'Java & Objective-C',
114+
value: 'java-objc',
115+
types: [
116+
'module-legacy',
117+
'module-new',
118+
'module-mixed',
119+
'view-mixed',
120+
'view-new',
121+
'view-legacy',
122+
],
123+
},
124+
{
125+
title: 'Kotlin & Objective-C',
126+
value: 'kotlin-objc',
127+
types: [
128+
'module-legacy',
129+
'module-new',
130+
'module-mixed',
131+
'view-mixed',
132+
'view-new',
133+
'view-legacy',
134+
],
135+
},
114136
{
115137
title: 'Java & Swift',
116138
value: 'java-swift',
@@ -366,6 +388,49 @@ async function create(argv: yargs.Arguments<any>) {
366388
},
367389
};
368390

391+
// Validate arguments passed to the CLI
392+
for (const [key, value] of Object.entries(argv)) {
393+
if (value == null) {
394+
continue;
395+
}
396+
397+
const question = questions[key as ArgName];
398+
399+
if (question == null) {
400+
continue;
401+
}
402+
403+
let valid = question.validate ? question.validate(String(value)) : true;
404+
405+
// We also need to guard against invalid choices
406+
if (valid && 'choices' in question) {
407+
const choices =
408+
typeof question.choices === 'function'
409+
? question.choices(undefined, argv, question)
410+
: question.choices;
411+
412+
if (choices && !choices.some((choice) => choice.value === value)) {
413+
valid = `Supported values are - ${choices.map((c) =>
414+
kleur.green(c.value)
415+
)}`;
416+
}
417+
}
418+
419+
if (valid !== true) {
420+
let message = `Invalid value ${kleur.red(
421+
String(value)
422+
)} passed for ${kleur.blue(key)}`;
423+
424+
if (typeof valid === 'string') {
425+
message += `: ${valid}`;
426+
}
427+
428+
console.log(message);
429+
430+
process.exit(1);
431+
}
432+
}
433+
369434
const {
370435
slug,
371436
description,
@@ -388,7 +453,7 @@ async function create(argv: yargs.Arguments<any>) {
388453

389454
// Skip questions with a single choice
390455
if (Array.isArray(v.choices) && v.choices.length === 1) {
391-
return v;
456+
return false;
392457
}
393458

394459
return true;
@@ -400,8 +465,8 @@ async function create(argv: yargs.Arguments<any>) {
400465
if (type === 'select' && typeof choices === 'function') {
401466
return {
402467
...v,
403-
type: (...args) => {
404-
const result = choices(...args);
468+
type: (prev, values, prompt) => {
469+
const result = choices(prev, { ...argv, ...values }, prompt);
405470

406471
if (result && result.length === 1) {
407472
return null;
@@ -535,6 +600,20 @@ async function create(argv: yargs.Arguments<any>) {
535600

536601
await fs.mkdirp(folder);
537602

603+
if (reactNativeVersion != null) {
604+
if (example === 'expo') {
605+
console.warn(
606+
`${kleur.yellow('⚠')} Ignoring --react-native-version for Expo example`
607+
);
608+
} else {
609+
console.log(
610+
`${kleur.blue('ℹ')} Using ${kleur.cyan(
611+
`react-native@${reactNativeVersion}`
612+
)} for the example`
613+
);
614+
}
615+
}
616+
538617
const spinner = ora('Generating example').start();
539618

540619
await generateExampleApp({

0 commit comments

Comments
 (0)