Skip to content

Commit 4e9fcec

Browse files
committed
refactor: properly type args
1 parent 528d931 commit 4e9fcec

File tree

3 files changed

+32
-27
lines changed

3 files changed

+32
-27
lines changed

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,21 @@ import {
1515
createMetadata,
1616
type Answers,
1717
acceptedArgs,
18+
type Args,
1819
} from './input';
1920
import { getDependencyVersionsFromExample } from './exampleApp/dependencies';
2021
import { printErrorHelp, printNextSteps } from './inform';
2122

2223
const FALLBACK_BOB_VERSION = '0.32.0';
2324

2425
yargs
25-
.command('$0 [name]', 'create a react native library', acceptedArgs, create)
26+
.command(
27+
'$0 [name]',
28+
'create a react native library',
29+
acceptedArgs,
30+
// @ts-expect-error Some types are still incompatible
31+
create
32+
)
2633
.demandCommand()
2734
.recommendCommands()
2835
.fail(printErrorHelp)
@@ -32,10 +39,7 @@ yargs
3239
})
3340
.strict().argv;
3441

35-
// FIXME: fix the type
36-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
37-
async function create(_argv: yargs.Arguments<any>) {
38-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
42+
async function create(_argv: yargs.Arguments<Args>) {
3943
const { _, $0, ...argv } = _argv;
4044

4145
// Prefetch bob version in background while asking questions
@@ -155,7 +159,7 @@ async function create(_argv: yargs.Arguments<any>) {
155159
await printNextSteps(local, folder, config);
156160
}
157161

158-
async function promptLocalLibrary(argv: Record<string, string>) {
162+
async function promptLocalLibrary(argv: Args) {
159163
let local = false;
160164

161165
if (typeof argv.local === 'boolean') {
@@ -181,7 +185,7 @@ async function promptLocalLibrary(argv: Record<string, string>) {
181185
return local;
182186
}
183187

184-
async function promptPath(argv: Record<string, string>, local: boolean) {
188+
async function promptPath(argv: Args, local: boolean) {
185189
let folder: string;
186190

187191
if (argv.name && !local) {

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

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ import { spawn } from './utils/spawn';
88
export type ArgName =
99
| 'slug'
1010
| 'description'
11-
| 'author-name'
12-
| 'author-email'
13-
| 'author-url'
14-
| 'repo-url'
11+
| 'authorName'
12+
| 'authorEmail'
13+
| 'authorUrl'
14+
| 'repoUrl'
1515
| 'languages'
1616
| 'type'
1717
| 'local'
1818
| 'example'
19-
| 'react-native-version';
19+
| 'reactNativeVersion';
2020

2121
export type ProjectLanguages = 'kotlin-objc' | 'kotlin-swift' | 'cpp' | 'js';
2222

@@ -142,53 +142,54 @@ export type Question = Omit<
142142
};
143143

144144
export const acceptedArgs: Record<ArgName, yargs.Options> = {
145-
'slug': {
145+
slug: {
146146
description: 'Name of the npm package',
147147
type: 'string',
148148
},
149-
'description': {
149+
description: {
150150
description: 'Description of the npm package',
151151
type: 'string',
152152
},
153-
'author-name': {
153+
authorName: {
154154
description: 'Name of the package author',
155155
type: 'string',
156156
},
157-
'author-email': {
157+
authorEmail: {
158158
description: 'Email address of the package author',
159159
type: 'string',
160160
},
161-
'author-url': {
161+
authorUrl: {
162162
description: 'URL for the package author',
163163
type: 'string',
164164
},
165-
'repo-url': {
165+
repoUrl: {
166166
description: 'URL for the repository',
167167
type: 'string',
168168
},
169-
'languages': {
169+
languages: {
170170
description: 'Languages you want to use',
171171
choices: LANGUAGE_CHOICES.map(({ value }) => value),
172172
},
173-
'type': {
173+
type: {
174174
description: 'Type of library you want to develop',
175175
choices: TYPE_CHOICES.map(({ value }) => value),
176176
},
177-
'react-native-version': {
177+
reactNativeVersion: {
178178
description: 'Version of React Native to use, uses latest if not specified',
179179
type: 'string',
180180
},
181-
'local': {
181+
local: {
182182
description: 'Whether to create a local library',
183183
type: 'boolean',
184184
},
185-
'example': {
185+
example: {
186186
description: 'Type of the example app to create',
187187
type: 'string',
188188
choices: EXAMPLE_CHOICES.map(({ value }) => value),
189189
},
190-
};
190+
} as const;
191191

192+
export type Args = Record<ArgName | 'name', string>;
192193
export type SupportedArchitecture = 'new' | 'mixed' | 'legacy';
193194
export type ExampleApp = 'none' | 'test-app' | 'expo' | 'vanilla';
194195

@@ -214,7 +215,7 @@ export async function createQuestions({
214215
}: {
215216
basename: string;
216217
local: boolean;
217-
argv: Record<string, string>;
218+
argv: Args;
218219
}) {
219220
let name, email;
220221

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import kleur from 'kleur';
22
import { spawn } from './spawn';
3-
import type { Answers, Question } from '../input';
3+
import type { Answers, Args, Question } from '../input';
44

55
export async function assertNpx() {
66
try {
@@ -24,7 +24,7 @@ export async function assertNpx() {
2424
/**
2525
* Makes sure the answers are in expected form and ends the process with error if they are not
2626
*/
27-
export function assertAnswers(questions: Question[], answers: Answers) {
27+
export function assertAnswers(questions: Question[], answers: Answers | Args) {
2828
for (const [key, value] of Object.entries(answers)) {
2929
if (value == null) {
3030
continue;

0 commit comments

Comments
 (0)