-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathcreate-expo-stack.ts
495 lines (427 loc) · 17.4 KB
/
create-expo-stack.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
import { GluegunCommand } from 'gluegun';
const util = require('util');
import {
configureProjectFiles,
generateProjectFiles,
getPackageManager,
printOutput,
renderTitle,
runCLI,
runIgnite,
showHelp
} from '../utilities';
import {
bunInstallationError,
DEFAULT_APP_NAME,
defaultOptions,
nativeWindUIOptions,
navigationValidationError,
projectNameValidationError
} from '../constants';
import { CliResults, availablePackages } from '../types';
import clearStylingPackages from '../utilities/clearStylingPackages';
import { validateProjectName } from '../utilities/validateProjectName';
import { cancel, intro, isCancel, text } from '@clack/prompts';
import clearNavigationPackages from '../utilities/clearNavigationPackages';
const command: GluegunCommand = {
name: 'create-expo-stack',
description: 'Create a new Expo project',
run: async (toolbox) => {
const {
filesystem: { exists, removeAsync },
parameters: { first, options },
print: { error, info, highlight, success, warning },
prompt
} = toolbox;
const printSomethingWentWrong = () => {
info(`\nOops, something went wrong while creating your project 😢`);
info(
`\nIf this was unexpected, please open an issue: https://github.com/roninoss/create-expo-stack#reporting-bugs--feedback`
);
info('');
};
if (options.help || options.h) {
showHelp(info, highlight, warning);
return;
}
// Conditionally skip running the CLI
const useDefault = (options.default !== undefined && options.default) || (options.d !== undefined && options.d);
const skipCLI = options.nonInteractive;
const useBlankTypescript =
options.nativewindui == undefined ? options.blank || false : (options.blank && options.nativewindui) || false;
// Check if any of the options were passed in via the command
const optionsPassedIn = availablePackages.some((condition) => options[condition] !== undefined);
// Set the default options
let cliResults: CliResults = defaultOptions;
cliResults.flags.packageManager = getPackageManager(toolbox, cliResults);
// START INPUT VALIDATION
try {
// Validation: check if the user passed in the tabs/drawer option without passing in either expo router or react navigation. If so, throw an error
if (
(options.tabs || options['drawer+tabs']) &&
!options.reactNavigation &&
!options['react-navigation'] &&
!options.reactnavigation &&
!options.expoRouter &&
!options['expo-router'] &&
!options.exporouter &&
// nativewindui applies the expo router option by default
!options.nativewindui
) {
// throw an error without a stack trace
throw navigationValidationError;
}
await renderTitle(toolbox);
intro(`Let's get started!`);
// Prompt the user for the project name if it is not passed in via the command
// - TODO: simplify this if statement to clarify what is being checked
if (!first && (options.ignite || !(useDefault || optionsPassedIn || skipCLI || useBlankTypescript))) {
const name = await text({
message: 'What do you want to name your project?',
placeholder: DEFAULT_APP_NAME
});
if (isCancel(name)) {
cancel('Cancelled... 👋');
return process.exit(0);
}
// if name is undefined or empty string, use default name
cliResults.projectName = (name && name.toString()) || DEFAULT_APP_NAME;
} else {
// Destructure the results but set the projectName if the first param is passed in
cliResults.projectName = first || DEFAULT_APP_NAME;
const pathSegments = cliResults.projectName.split('/');
cliResults.projectName = pathSegments.pop(); // get last segment as the project name
}
// Validate the provided project name; check if the directory already exists
// - We may or may not be interactive, so conditionally pass in prompt.
// - Ignore validation if the overwrite option is passed in.
if (options.overwrite) {
cliResults.flags.overwrite = true;
}
if (options.eas) {
cliResults.flags.eas = true;
}
await validateProjectName(
exists,
removeAsync,
!(useDefault || optionsPassedIn || skipCLI || useBlankTypescript) ? prompt : null,
cliResults.projectName,
cliResults.flags.overwrite
);
} catch (err: string | any) {
if (process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'development') {
error(`error: ${err}`);
}
if (err === '') {
// user cancelled/exited the interactive CLI
return void success(`\nCancelled... 👋 \n`);
}
if (err === navigationValidationError) {
// user tried passing in tabs/drawer option without passing in either expo router or react navigation
return void error(`\n${navigationValidationError}\n`);
}
if (err.message.includes(projectNameValidationError)) {
return void success(`\nCancelled... 👋 \n`);
}
// we keep this around so we can check what went wrong
if (process.env.NODE_ENV !== 'test') {
// Delete all files with projectName
await removeAsync(cliResults.projectName);
}
printSomethingWentWrong();
throw err;
}
// END INPUT VALIDATION
// Determine remaining options, run interactive CLI if necessary, and generate project
try {
// Check if user wants to create an opinionated stack prior to running the configurable CLI
if (options.ignite) {
await runIgnite(toolbox, cliResults.projectName, cliResults);
} else {
// Check if the user wants to not install dependencies and/or not initialize git, update cliResults accordingly
cliResults.flags.noInstall =
options.noInstall || (typeof options.install === 'boolean' && !options.install) || false;
cliResults.flags.noGit = options.noGit || (typeof options.git === 'boolean' && !options.git) || false;
// Validate import alias string forward slash and asterisk
if (typeof options.importAlias === 'string') {
if (!options.importAlias.endsWith('/*')) {
throw new Error('Import alias must end in `/*`, for example: `@/*` or `~/`');
}
}
cliResults.flags.importAlias = options.importAlias !== false && options['import-alias'] !== false;
if (!(useDefault || optionsPassedIn || skipCLI || useBlankTypescript)) {
// Run the CLI to prompt the user for input
cliResults = await runCLI(toolbox, cliResults.projectName);
}
// Update the cliResults with the options passed in via the command
// Navigation packages
if (options.reactNavigation || options['react-navigation'] || options.reactnavigation) {
// Add react-navigation package
cliResults.packages.push({
name: 'react-navigation',
type: 'navigation',
options: {
type: options.tabs ? 'tabs' : options['drawer+tabs'] ? 'drawer + tabs' : 'stack'
}
});
}
if (options.expoRouter || options['expo-router'] || options.exporouter) {
// Add expo-router package
cliResults.packages.push({
name: 'expo-router',
type: 'navigation',
options: {
type: options.tabs ? 'tabs' : options['drawer+tabs'] ? 'drawer + tabs' : 'stack'
}
});
}
// Styling packages
if (options.nativewind) {
// Check if there is already a styling library added and remove it if so
cliResults = clearStylingPackages(cliResults);
// Add nativewind package
cliResults.packages.push({
name: 'nativewind',
type: 'styling'
});
} else if (options.nativewindui) {
cliResults = clearStylingPackages(cliResults);
cliResults = clearNavigationPackages(cliResults);
const parsedComponents =
options?.selectedComponents
?.split(',')
?.map((c: string) => c.trim())
?.filter((item) => nativeWindUIOptions.includes(item)) ?? [];
const selectedComponents = parsedComponents.length
? Array.from(new Set([...parsedComponents, 'text']))
: nativeWindUIOptions;
cliResults.packages.push({
name: 'nativewindui',
type: 'styling',
options: {
selectedComponents: options.blank ? ['text'] : selectedComponents
}
});
cliResults.packages.push({
name: 'expo-router',
type: 'navigation',
options: {
type: options.tabs ? 'tabs' : options['drawer+tabs'] ? 'drawer + tabs' : 'stack'
}
});
} else if (options.tamagui) {
cliResults = clearStylingPackages(cliResults);
// Add tamagui package
cliResults.packages.push({
name: 'tamagui',
type: 'styling'
});
} else if (options.unistyles) {
cliResults = clearStylingPackages(cliResults);
// Add unistyles package
cliResults.packages.push({
name: 'unistyles',
type: 'styling'
});
} else if (options.stylesheet) {
cliResults = clearStylingPackages(cliResults);
// Add stylesheet package
cliResults.packages.push({
name: 'stylesheet',
type: 'styling'
});
} else if (options.restyle) {
try {
cliResults = clearStylingPackages(cliResults);
// Add stylesheet package
cliResults.packages.push({
name: 'restyle',
type: 'styling'
});
} catch (error) {
console.log({ error });
}
}
// if there is no style package, add stylesheet
else if (cliResults.packages.find((p) => p.type === 'styling') === undefined) {
cliResults.packages.push({
name: 'stylesheet',
type: 'styling'
});
}
// Authentication packages
if (options.supabase) {
// Add supabase package
cliResults.packages.push({
name: 'supabase',
type: 'authentication'
});
}
if (options.firebase) {
// Add firebase package
cliResults.packages.push({
name: 'firebase',
type: 'authentication'
});
}
if (options.clerk) {
// Add clerk package
cliResults.packages.push({
name: 'clerk',
type: 'authentication'
});
}
// State Management packages
if (options.zustand) {
// Add zustand package
cliResults.packages.push({
name: 'zustand',
type: 'state-management'
});
}
// Internalization packages
if (options.i18next) {
cliResults.packages.push({
name: 'i18next',
type: 'internationalization'
});
}
// Analytics packages
if (options.vexoAnalytics || options['vexo-analytics'] || options.vexoanalytics) {
cliResults.packages.push({ name: 'vexo-analytics', type: 'analytics' });
}
// By this point, all cliResults should be set
info('');
highlight('Your project configuration:');
info(`${util.inspect(cliResults, false, null, true /* enable colors */)}`);
info('');
highlight('To recreate this project, run:');
// Function that outputs a string given the CLI results and the packageManager. The outputted string should be a command that can be run to recreate the project
const generateRerunScript = (cliResults: CliResults) => {
let script = `npx create-expo-stack@latest ${cliResults.projectName} `;
const isNativeWindUISelected = cliResults.packages.some((p) => p.name === 'nativewindui');
if (isNativeWindUISelected) {
script += '--nativewindui ';
const nativeWindUIComponents =
cliResults.packages.find((p) => p.name === 'nativewindui')?.options.selectedComponents ?? [];
// we do this to account for older stored config e.g that has selectable text in it
const onlyValidComponents = nativeWindUIComponents.filter((c) => nativeWindUIOptions.includes(c));
if (onlyValidComponents.length === 0) {
script += '--blank ';
} else if (onlyValidComponents.length !== nativeWindUIOptions.length) {
script += `--selected-components=${onlyValidComponents.join(',')} `;
}
// this should always be expo router for nwui
const chosenNavigationOption = cliResults.packages.find((p) => p.type === 'navigation');
const hasNavigationPackage = chosenNavigationOption !== undefined;
const navigationType = chosenNavigationOption.options.type;
if (hasNavigationPackage) {
// NOTE we don't need to add expo-router since its currently getting automatically added with nativewindui
// NOTE stack is default so doesn't need to applied.
if (navigationType === 'tabs') {
script += '--tabs ';
} else if (navigationType === 'drawer + tabs') {
script += '--drawer+tabs ';
}
}
const stateManagementPackage = cliResults.packages.find((p) => p.type === 'state-management');
if (stateManagementPackage) {
script += `--${stateManagementPackage.name} `;
}
} else {
// Add the packages
cliResults.packages.forEach((p) => {
script += `--${p.name} `;
// If the package is a navigation package, add the type if it is tabs
if (p.type === 'navigation') {
if (p.options?.type === 'tabs') {
script += '--tabs ';
} else if (p.options?.type === 'drawer + tabs') {
script += '--drawer+tabs ';
}
}
});
}
// Check if the user wants to skip installing packages
if (cliResults.flags.noInstall) {
script += '--no-install ';
}
// Check if the user wants to skip initializing git
if (cliResults.flags.noGit) {
script += '--no-git ';
}
// Check if the user wants to overwrite the project directory
if (!cliResults.flags.importAlias) {
script += '--no-import-alias ';
}
// Add the package manager
if (cliResults.flags.packageManager !== 'npm') {
script += `--${cliResults.flags.packageManager}`;
}
if (cliResults.flags.eas) {
script += ` --eas`;
}
return script;
};
const packageManager = getPackageManager(toolbox, cliResults);
warning(` ${generateRerunScript(cliResults)}`);
const { packages } = cliResults;
// Define props to be passed into the templates
const authenticationPackage = packages.find((p) => p.type === 'authentication') || undefined;
const navigationPackage = packages.find((p) => p.type === 'navigation') || undefined;
// if there is no styling package, add the stylesheet package
const stylingPackage = packages.find((p) => p.type === 'styling');
const internalizationPackage = packages.find((p) => p.type === 'internationalization');
const analyticsPackage = packages.find((p) => p.type === 'analytics');
//add the state management package if it is selected
const stateManagementPackage = packages.find((p) => p.type === 'state-management') || undefined;
let files: string[] = [];
files = configureProjectFiles(
authenticationPackage,
files,
navigationPackage,
stylingPackage,
analyticsPackage,
toolbox,
cliResults,
internalizationPackage,
stateManagementPackage
);
// Once all the files are defined, format and generate them
let formattedFiles: any[] = [];
formattedFiles = generateProjectFiles(
authenticationPackage,
analyticsPackage,
cliResults,
files,
formattedFiles,
navigationPackage,
packageManager,
stylingPackage,
toolbox,
internalizationPackage,
stateManagementPackage
);
await printOutput(cliResults, formattedFiles, toolbox, stylingPackage);
}
} catch (err) {
if (process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'development') {
error(`error: ${err}`);
}
if (err === '') {
// user cancelled/exited the interactive CLI
return void success(`\nCancelled... 👋 \n`);
}
if (err.message.includes(bunInstallationError)) {
return void success(`\nCancelled to install recommended version of Bun.... 👋 \n`);
}
if (process.env.NODE_ENV !== 'test') {
// Delete all files with projectName
await removeAsync(cliResults.projectName);
}
printSomethingWentWrong();
throw err;
}
}
};
export default command;