Skip to content

Commit 48b3c66

Browse files
committed
refactor: extract assertOptions
1 parent b8f0252 commit 48b3c66

File tree

2 files changed

+59
-55
lines changed

2 files changed

+59
-55
lines changed

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

Lines changed: 5 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { spawn } from './utils/spawn';
1616
import { version } from '../package.json';
1717
import { addCodegenBuildScript } from './exampleApp/addCodegenBuildScript';
1818
import { createInitialGitCommit } from './utils/initialCommit';
19-
import { assertNpx } from './utils/assert';
19+
import { assertNpx, assertOptions } from './utils/assert';
2020
import { resolveBobVersionWithFallback } from './utils/promiseWithFallback';
2121
import { generateTemplateConfiguration } from './template/config';
2222

@@ -218,7 +218,10 @@ const TYPE_CHOICES: {
218218
},
219219
];
220220

221-
type Question = Omit<PromptObject<keyof Answers>, 'validate' | 'name'> & {
221+
export type Question = Omit<
222+
PromptObject<keyof Answers>,
223+
'validate' | 'name'
224+
> & {
222225
validate?: (value: string) => boolean | string;
223226
name: keyof Answers;
224227
};
@@ -843,56 +846,3 @@ yargs
843846
'strip-dashed': true,
844847
})
845848
.strict().argv;
846-
847-
/**
848-
* Makes sure the answers are in expected form and ends the process with error if they are not
849-
*/
850-
export function assertOptions(questions: Question[], answers: Answers) {
851-
for (const [key, value] of Object.entries(answers)) {
852-
if (value == null) {
853-
continue;
854-
}
855-
856-
const question = questions.find((q) => q.name === key);
857-
858-
if (question == null) {
859-
continue;
860-
}
861-
862-
let valid = question.validate ? question.validate(String(value)) : true;
863-
864-
// We also need to guard against invalid choices
865-
// If we don't already have a validation message to provide a better error
866-
if (typeof valid !== 'string' && 'choices' in question) {
867-
const choices =
868-
typeof question.choices === 'function'
869-
? question.choices(
870-
undefined,
871-
// @ts-expect-error: it complains about optional values, but it should be fine
872-
answers,
873-
question
874-
)
875-
: question.choices;
876-
877-
if (choices && !choices.some((choice) => choice.value === value)) {
878-
valid = `Supported values are - ${choices.map((c) =>
879-
kleur.green(c.value)
880-
)}`;
881-
}
882-
}
883-
884-
if (valid !== true) {
885-
let message = `Invalid value ${kleur.red(
886-
String(value)
887-
)} passed for ${kleur.blue(key)}`;
888-
889-
if (typeof valid === 'string') {
890-
message += `: ${valid}`;
891-
}
892-
893-
console.log(message);
894-
895-
process.exit(1);
896-
}
897-
}
898-
}

packages/create-react-native-library/src/utils/assert.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import kleur from 'kleur';
22
import { spawn } from './spawn';
3+
import type { Answers, Question } from 'create-react-native-library';
34

45
export async function assertNpx() {
56
try {
@@ -19,3 +20,56 @@ export async function assertNpx() {
1920
}
2021
}
2122
}
23+
24+
/**
25+
* Makes sure the answers are in expected form and ends the process with error if they are not
26+
*/
27+
export function assertAnswers(questions: Question[], answers: Answers) {
28+
for (const [key, value] of Object.entries(answers)) {
29+
if (value == null) {
30+
continue;
31+
}
32+
33+
const question = questions.find((q) => q.name === key);
34+
35+
if (question == null) {
36+
continue;
37+
}
38+
39+
let valid = question.validate ? question.validate(String(value)) : true;
40+
41+
// We also need to guard against invalid choices
42+
// If we don't already have a validation message to provide a better error
43+
if (typeof valid !== 'string' && 'choices' in question) {
44+
const choices =
45+
typeof question.choices === 'function'
46+
? question.choices(
47+
undefined,
48+
// @ts-expect-error: it complains about optional values, but it should be fine
49+
answers,
50+
question
51+
)
52+
: question.choices;
53+
54+
if (choices && !choices.some((choice) => choice.value === value)) {
55+
valid = `Supported values are - ${choices.map((c) =>
56+
kleur.green(c.value)
57+
)}`;
58+
}
59+
}
60+
61+
if (valid !== true) {
62+
let message = `Invalid value ${kleur.red(
63+
String(value)
64+
)} passed for ${kleur.blue(key)}`;
65+
66+
if (typeof valid === 'string') {
67+
message += `: ${valid}`;
68+
}
69+
70+
console.log(message);
71+
72+
process.exit(1);
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)