-
Notifications
You must be signed in to change notification settings - Fork 205
/
Copy pathinput.ts
338 lines (314 loc) · 8.2 KB
/
input.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import githubUsername from 'github-username';
import validateNpmPackage from 'validate-npm-package-name';
import type yargs from 'yargs';
import { version } from '../package.json';
import type { Question } from './utils/prompt';
import { spawn } from './utils/spawn';
export type ArgName =
| 'slug'
| 'description'
| 'authorName'
| 'authorEmail'
| 'authorUrl'
| 'repoUrl'
| 'languages'
| 'type'
| 'local'
| 'example'
| 'reactNativeVersion';
export type ProjectLanguages = 'kotlin-objc' | 'kotlin-swift' | 'cpp' | 'js';
export type ProjectType =
| 'turbo-module'
| 'fabric-view'
| 'legacy-module'
| 'legacy-view'
| 'library';
const LANGUAGE_CHOICES: {
title: string;
value: ProjectLanguages;
types: ProjectType[];
}[] = [
{
title: 'Kotlin & Objective-C',
value: 'kotlin-objc',
types: ['turbo-module', 'fabric-view', 'legacy-module', 'legacy-view'],
},
{
title: 'Kotlin & Swift',
value: 'kotlin-swift',
types: ['legacy-module', 'legacy-view'],
},
{
title: 'C++ for Android & iOS',
value: 'cpp',
types: ['turbo-module', 'legacy-module'],
},
{
title: 'JavaScript for Android, iOS & Web',
value: 'js',
types: ['library'],
},
];
const EXAMPLE_CHOICES = (
[
{
title: 'Vanilla',
value: 'vanilla',
description: "provides access to app's native code",
disabled: false,
},
{
title: 'Test app',
value: 'test-app',
description: "app's native code is abstracted away",
// The test app is disabled for now until proper
// Codegen spec shipping is implemented
disabled: !process.env.CRNL_ENABLE_TEST_APP,
},
{
title: 'Expo',
value: 'expo',
description: 'managed expo project with web support',
disabled: false,
},
] as const
).filter((choice) => !choice.disabled);
const TYPE_CHOICES: {
title: string;
value: ProjectType;
description: string;
}[] = [
{
title: 'Turbo module',
value: 'turbo-module',
description: 'integration for native APIs to JS',
},
{
title: 'Fabric view',
value: 'fabric-view',
description: 'integration for native views to JS',
},
{
title: 'Legacy Native module',
value: 'legacy-module',
description: 'bridge for native APIs to JS (old architecture)',
},
{
title: 'Legacy Native view',
value: 'legacy-view',
description: 'bridge for native views to JS (old architecture)',
},
{
title: 'JavaScript library',
value: 'library',
description: 'supports Expo Go and Web',
},
];
export const acceptedArgs: Record<ArgName, yargs.Options> = {
slug: {
description: 'Name of the npm package',
type: 'string',
},
description: {
description: 'Description of the npm package',
type: 'string',
},
authorName: {
description: 'Name of the package author',
type: 'string',
},
authorEmail: {
description: 'Email address of the package author',
type: 'string',
},
authorUrl: {
description: 'URL for the package author',
type: 'string',
},
repoUrl: {
description: 'URL for the repository',
type: 'string',
},
languages: {
description: 'Languages you want to use',
choices: LANGUAGE_CHOICES.map(({ value }) => value),
},
type: {
description: 'Type of library you want to develop',
choices: TYPE_CHOICES.map(({ value }) => value),
},
reactNativeVersion: {
description: 'Version of React Native to use, uses latest if not specified',
type: 'string',
},
local: {
description: 'Whether to create a local library',
type: 'boolean',
},
example: {
description: 'Type of the example app to create',
type: 'string',
choices: EXAMPLE_CHOICES.map(({ value }) => value),
},
} as const;
export type Args = Record<ArgName | 'name', string>;
export type SupportedArchitecture = 'new' | 'legacy';
export type ExampleApp = 'none' | 'test-app' | 'expo' | 'vanilla';
export type Answers = {
name: string;
slug: string;
description: string;
authorName: string;
authorEmail: string;
authorUrl: string;
repoUrl: string;
languages: ProjectLanguages;
type: ProjectType;
example: ExampleApp;
reactNativeVersion?: string;
local?: boolean;
};
export async function createQuestions({
basename,
local,
}: {
basename: string;
local: boolean;
}) {
let name, email;
try {
name = await spawn('git', ['config', '--get', 'user.name']);
email = await spawn('git', ['config', '--get', 'user.email']);
} catch (e) {
// Ignore error
}
const questions: Question<keyof Answers>[] = [
{
type: 'text',
name: 'slug',
message: 'What is the name of the npm package?',
initial: validateNpmPackage(basename).validForNewPackages
? /^(@|react-native)/.test(basename)
? basename
: `react-native-${basename}`
: undefined,
validate: (input) =>
validateNpmPackage(input).validForNewPackages ||
'Must be a valid npm package name',
},
{
type: 'text',
name: 'description',
message: 'What is the description for the package?',
validate: (input) => Boolean(input) || 'Cannot be empty',
},
{
type: local ? null : 'text',
name: 'authorName',
message: 'What is the name of package author?',
initial: name,
validate: (input) => Boolean(input) || 'Cannot be empty',
},
{
type: local ? null : 'text',
name: 'authorEmail',
message: 'What is the email address for the package author?',
initial: email,
validate: (input) =>
/^\S+@\S+$/.test(input) || 'Must be a valid email address',
},
{
type: local ? null : 'text',
name: 'authorUrl',
message: 'What is the URL for the package author?',
// @ts-expect-error this is supported, but types are wrong
initial: async (previous: string) => {
try {
const username = await githubUsername(previous);
return `https://github.com/${username}`;
} catch (e) {
// Ignore error
}
return undefined;
},
validate: (input) => /^https?:\/\//.test(input) || 'Must be a valid URL',
},
{
type: local ? null : 'text',
name: 'repoUrl',
message: 'What is the URL for the repository?',
initial: (_: string, answers: Answers) => {
if (/^https?:\/\/github.com\/[^/]+/.test(answers.authorUrl)) {
return `${answers.authorUrl}/${answers.slug
.replace(/^@/, '')
.replace(/\//g, '-')}`;
}
return '';
},
validate: (input) => /^https?:\/\//.test(input) || 'Must be a valid URL',
},
{
type: 'select',
name: 'type',
message: 'What type of library do you want to develop?',
choices: TYPE_CHOICES,
},
{
type: 'select',
name: 'languages',
message: 'Which languages do you want to use?',
choices: (_, values) => {
return LANGUAGE_CHOICES.filter((choice) => {
if (choice.types) {
return choice.types.includes(values.type);
}
return true;
});
},
},
];
if (!local) {
questions.push({
type: 'select',
name: 'example',
message: 'What type of example app do you want to create?',
choices: (_, values) => {
return EXAMPLE_CHOICES.filter((choice) => {
if (values.type) {
return values.type === 'library'
? choice.value === 'expo'
: choice.value !== 'expo';
}
return true;
});
},
});
}
return questions;
}
export function createMetadata(answers: Answers) {
// Some of the passed args can already be derived from the generated package.json file.
const ignoredAnswers: (keyof Answers)[] = [
'name',
'slug',
'description',
'authorName',
'authorEmail',
'authorUrl',
'repoUrl',
'example',
'reactNativeVersion',
'local',
];
type AnswerEntries<T extends keyof Answers = keyof Answers> = [
T,
Answers[T],
][];
const libraryMetadata = Object.fromEntries(
(Object.entries(answers) as AnswerEntries).filter(
([answer]) => !ignoredAnswers.includes(answer)
)
);
libraryMetadata.version = version;
return libraryMetadata;
}