-
Notifications
You must be signed in to change notification settings - Fork 964
/
create-redwood-app.js
731 lines (660 loc) · 19.7 KB
/
create-redwood-app.js
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
#!/usr/bin/env node
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { trace, SpanStatusCode } from '@opentelemetry/api'
import chalk from 'chalk'
import checkNodeVersionCb from 'check-node-version'
import execa from 'execa'
import fs from 'fs-extra'
import semver from 'semver'
import terminalLink from 'terminal-link'
import { hideBin, Parser } from 'yargs/helpers'
import yargs from 'yargs/yargs'
import { RedwoodTUI, ReactiveTUIContent, RedwoodStyling } from '@redwoodjs/tui'
// In ESM, for relative paths, the extension is important.
// See https://nodejs.org/dist/latest-v18.x/docs/api/esm.html#mandatory-file-extensions.
import {
startTelemetry,
shutdownTelemetry,
recordErrorViaTelemetry,
} from './telemetry.js'
// JSON modules aren't stable yet, but if they were we could use them instead.
// See https://nodejs.org/dist/latest-v18.x/docs/api/esm.html#json-modules.
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const { name, version } = fs.readJSONSync(
path.resolve(__dirname, '../package.json')
)
// Telemetry
const { telemetry } = Parser(hideBin(process.argv))
const tui = new RedwoodTUI()
async function executeCompatibilityCheck(templateDir, yarnInstall) {
const tuiContent = new ReactiveTUIContent({
mode: 'text',
content: 'Checking node and yarn compatibility',
spinner: {
enabled: true,
},
})
tui.startReactive(tuiContent)
if (!yarnInstall) {
tuiContent.update({
spinner: {
enabled: false,
},
content: `${RedwoodStyling.warning(
'⚠'
)} Skipped compatibility check because yarn install was skipped via command line flag`,
})
tui.stopReactive()
return
}
const [checksPassed, checksData] = await checkNodeAndYarnVersion(templateDir)
if (checksPassed) {
tuiContent.update({
spinner: {
enabled: false,
},
content: `${RedwoodStyling.green('✔')} Compatibility checks passed`,
})
tui.stopReactive()
return
}
if (!checksPassed) {
const foundNodeVersionIsLessThanRequired = semver.lt(
checksData.node.version.version,
semver.minVersion(checksData.node.wanted.raw)
)
const foundYarnVersionIsLessThanRequired = semver.lt(
checksData.yarn.version.version,
semver.minVersion(checksData.yarn.wanted.raw)
)
if (
foundNodeVersionIsLessThanRequired ||
foundNodeVersionIsLessThanRequired
) {
const errorMessages = [
{ type: 'node', ok: foundNodeVersionIsLessThanRequired },
{ type: 'yarn', ok: foundYarnVersionIsLessThanRequired },
]
.filter(({ ok }) => !ok)
.map(
({ type }) =>
` ${type} ${checksData[type].wanted.range} required; found ${checksData[type].version.version}`
)
tui.stopReactive(true)
tui.displayError(
'Compatibility checks failed',
[
` ${errorMessages.join('\n')}`,
'',
` Please use tools like nvm or corepack to change to a compatible version.`,
` See: ${terminalLink(
'Tutorial - Prerequisites',
'https://redwoodjs.com/docs/tutorial/chapter1/prerequisites'
)}`,
].join('\n')
)
recordErrorViaTelemetry('Compatibility checks failed')
await shutdownTelemetry()
process.exit(1)
}
tui.stopReactive(true)
tui.displayWarning(
'Compatibility checks failed',
[
` node ${checksData.node.wanted.range} supported; found ${checksData.node.version.version}`,
'',
` This may make your project incompatible with some deploy targets, especially those using AWS Lambdas.`,
` See: ${terminalLink(
'Tutorial - Prerequisites',
'https://redwoodjs.com/docs/tutorial/chapter1/prerequisites'
)}`,
].join('\n')
)
// Try catch for handling if the user cancels the prompt.
try {
const response = await tui.prompt({
type: 'select',
name: 'override-engine-error',
message: 'How would you like to proceed?',
choices: ['Override error and continue install', 'Quit install'],
initial: 0,
})
if (response['override-engine-error'] === 'Quit install') {
recordErrorViaTelemetry('User quit after engine check error')
await shutdownTelemetry()
process.exit(1)
}
} catch (error) {
recordErrorViaTelemetry('User cancelled install at engine check error')
await shutdownTelemetry()
process.exit(1)
}
}
}
/**
*
* This type has to be updated if the engines field in the create redwood app template package.json is updated.
* @returns [boolean, Record<'node' | 'yarn', any>]
*/
function checkNodeAndYarnVersion(templateDir) {
return new Promise((resolve) => {
const { engines } = fs.readJSONSync(path.join(templateDir, 'package.json'))
checkNodeVersionCb(engines, (_error, result) => {
return resolve([result.isSatisfied, result.versions])
})
})
}
async function createProjectFiles(newAppDir, overwrite, yarn1) {
const tuiContent = new ReactiveTUIContent({
mode: 'text',
content: 'Creating project files',
spinner: {
enabled: true,
},
})
tui.startReactive(tuiContent)
// Check if the new app directory already exists
if (fs.existsSync(newAppDir) && !overwrite) {
// Check if the directory contains files and show an error if it does
if (fs.readdirSync(newAppDir).length > 0) {
tui.stopReactive(true)
tui.displayError(
'Project directory already contains files',
[
`'${RedwoodStyling.info(newAppDir)}' already exists and is not empty`,
``,
`You can use the '${RedwoodStyling.info(
'overwrite'
)}' flag to create the project even if target directory isn't empty`,
].join('\n')
)
recordErrorViaTelemetry(`Project directory already contains files`)
await shutdownTelemetry()
process.exit(1)
}
}
// Ensure the new app directory exists
fs.ensureDirSync(path.dirname(newAppDir))
// Copy the template files to the new app directory
const templateDirectory = path.resolve(__dirname, '../template')
fs.copySync(templateDirectory, newAppDir, { overwrite })
// .gitignore is renamed here to force file inclusion during publishing
fs.rename(
path.join(newAppDir, 'gitignore.template'),
path.join(newAppDir, '.gitignore')
)
// We need to update some files when the user selects to use yarn v1
if (yarn1) {
// rm files:
// - .yarnrc.yml
// - .yarn
fs.rmSync(path.join(newAppDir, '.yarnrc.yml'))
fs.rmSync(path.join(newAppDir, '.yarn'), {
recursive: true,
force: true,
})
// rm after `.pnp.*`
const gitignore = fs.readFileSync(path.join(newAppDir, '.gitignore'), {
encoding: 'utf-8',
})
const [yarn1Gitignore, _yarn3Gitignore] = gitignore.split('.pnp.*')
fs.writeFileSync(path.join(newAppDir, '.gitignore'), yarn1Gitignore)
// rm `packageManager` from package.json
const packageJSON = fs.readJSONSync(path.join(newAppDir, 'package.json'))
delete packageJSON.packageManager
fs.writeJSONSync(path.join(newAppDir, 'package.json'), packageJSON, {
spaces: 2,
})
}
tuiContent.update({
spinner: {
enabled: false,
},
content: `${RedwoodStyling.green('✔')} Project files created`,
})
tui.stopReactive()
}
async function installNodeModules(newAppDir) {
const tuiContent = new ReactiveTUIContent({
mode: 'text',
header: 'Installing node modules',
content: ' ⏱ This could take a while...',
spinner: {
enabled: true,
},
})
tui.startReactive(tuiContent)
const yarnInstallSubprocess = execa('yarn install', {
shell: true,
cwd: newAppDir,
})
try {
await yarnInstallSubprocess
} catch (error) {
tui.stopReactive(true)
tui.displayError(
"Couldn't install node modules",
[
`We could not install node modules via ${RedwoodStyling.info(
"'yarn install'"
)}. Please see below for the full error message.`,
'',
error,
].join('\n')
)
recordErrorViaTelemetry(error)
await shutdownTelemetry()
process.exit(1)
}
tuiContent.update({
header: '',
content: `${RedwoodStyling.green('✔')} Node modules successfully installed`,
spinner: {
enabled: false,
},
})
tui.stopReactive()
}
async function convertToJavascript(newAppDir) {
const tuiContent = new ReactiveTUIContent({
mode: 'text',
content: 'Converting TypeScript files to JavaScript',
spinner: {
enabled: true,
},
})
tui.startReactive(tuiContent)
const conversionSubprocess = execa('yarn rw ts-to-js', {
shell: true,
cwd: newAppDir,
})
try {
await conversionSubprocess
} catch (error) {
tui.stopReactive(true)
tui.displayError(
"Couldn't convert TypeScript files to JavaScript",
[
`We could not convert the Typescript files to Javascript using ${RedwoodStyling.info(
"'yarn rw ts-to-js'"
)}. Please see below for the full error message.`,
'',
error,
].join('\n')
)
recordErrorViaTelemetry(error)
await shutdownTelemetry()
process.exit(1)
}
tuiContent.update({
content: `${RedwoodStyling.green(
'✔'
)} Converted TypeScript files to JavaScript`,
spinner: {
enabled: false,
},
})
tui.stopReactive()
}
async function generateTypes(newAppDir) {
const tuiContent = new ReactiveTUIContent({
mode: 'text',
content: 'Generating types',
spinner: {
enabled: true,
},
})
tui.startReactive(tuiContent)
const generateSubprocess = execa('yarn rw-gen', {
shell: true,
cwd: newAppDir,
})
try {
await generateSubprocess
} catch (error) {
tui.stopReactive(true)
tui.displayError(
"Couldn't generate types",
[
`We could not generate types using ${RedwoodStyling.info(
"'yarn rw-gen'"
)}. Please see below for the full error message.`,
'',
error,
].join('\n')
)
recordErrorViaTelemetry(error)
await shutdownTelemetry()
process.exit(1)
}
tuiContent.update({
content: `${RedwoodStyling.green('✔')} Generated types`,
spinner: {
enabled: false,
},
})
tui.stopReactive()
}
async function initialiseGit(newAppDir) {
const tuiContent = new ReactiveTUIContent({
mode: 'text',
content: 'Initialising a git repo',
spinner: {
enabled: true,
},
})
tui.startReactive(tuiContent)
const gitSubprocess = execa(
'git init && git add . && git commit -m "Initial commit"',
{
shell: true,
cwd: newAppDir,
}
)
try {
await gitSubprocess
} catch (error) {
tui.stopReactive(true)
tui.displayError(
"Couldn't initialise a git repo",
[
`We could not initialise a git repo using ${RedwoodStyling.info(
'git init && git add . && git commit -m "Initial commit"'
)}. Please see below for the full error message.`,
'',
error,
].join('\n')
)
recordErrorViaTelemetry(error)
await shutdownTelemetry()
process.exit(1)
}
tuiContent.update({
content: `${RedwoodStyling.green('✔')} Initialised a git repo`,
spinner: {
enabled: false,
},
})
tui.stopReactive()
}
async function handleTypescriptPreference(typescriptFlag) {
// Handle case where flag is set
if (typescriptFlag !== null) {
tui.drawText(
`${RedwoodStyling.green('✔')} Using ${
typescriptFlag ? 'TypeScript' : 'JavaScript'
} based on command line flag`
)
return typescriptFlag
}
// Prompt user for preference
try {
const response = await tui.prompt({
type: 'Select',
name: 'language',
choices: ['TypeScript', 'JavaScript'],
message: 'Select your preferred coding language',
initial: 'TypeScript',
})
return response.language === 'TypeScript'
} catch (_error) {
recordErrorViaTelemetry('User cancelled install at language prompt')
await shutdownTelemetry()
process.exit(1)
}
}
async function handleGitPreference(gitInitFlag) {
// Handle case where flag is set
if (gitInitFlag !== null) {
tui.drawText(
`${RedwoodStyling.green('✔')} ${
gitInitFlag ? 'Will' : 'Will not'
} initialise a git repo based on command line flag`
)
return gitInitFlag
}
// Prompt user for preference
try {
const response = await tui.prompt({
type: 'Toggle',
name: 'git',
message: 'Do you want to initialize a git repo?',
enabled: 'Yes',
disabled: 'no',
initial: 'Yes',
})
return response.git
} catch (_error) {
recordErrorViaTelemetry('User cancelled install at git prompt')
await shutdownTelemetry()
process.exit(1)
}
}
/**
* This function creates a new RedwoodJS app.
*
* It performs the following actions:
* - TODO - Add a list of what this function does
*/
async function createRedwoodApp() {
// Introductory message
tui.drawText(
[
`${RedwoodStyling.redwood('-'.repeat(66))}`,
`${' '.repeat(16)}🌲⚡️ ${RedwoodStyling.header(
'Welcome to RedwoodJS!'
)} ⚡️🌲`,
`${RedwoodStyling.redwood('-'.repeat(66))}`,
].join('\n')
)
// Extract the args as provided by the user in the command line
// TODO: Make all flags have the 'flag' suffix
const {
_: args,
'yarn-install': yarnInstall,
typescript: typescriptFlag,
overwrite,
// telemetry, // Extracted above to check if telemetry is disabled before we even reach this point
yarn1,
'git-init': gitInitFlag,
} = yargs(hideBin(process.argv))
.scriptName(name)
.usage('Usage: $0 <project directory> [option]')
.example('$0 newapp')
.option('yarn-install', {
default: true,
type: 'boolean',
describe:
'Skip yarn install with --no-yarn-install. Also skips version requirements check.',
})
.option('typescript', {
alias: 'ts',
default: null,
type: 'boolean',
describe: 'Generate a TypeScript project.',
})
.option('overwrite', {
default: false,
type: 'boolean',
describe: "Create even if target directory isn't empty",
})
.option('telemetry', {
default: true,
type: 'boolean',
describe:
'Enables sending telemetry events for this create command and all Redwood CLI commands https://telemetry.redwoodjs.com',
})
.option('yarn1', {
default: false,
type: 'boolean',
describe: 'Use yarn 1. yarn 3 by default',
})
.option('git-init', {
alias: 'git',
default: null,
type: 'boolean',
describe: 'Initialize a git repository.',
})
.version(version)
.parse()
// Record some of the arguments for telemetry
trace.getActiveSpan()?.setAttribute('yarn-install', yarnInstall)
trace.getActiveSpan()?.setAttribute('overwrite', overwrite)
trace.getActiveSpan()?.setAttribute('yarn1', yarn1)
// Get the directory for installation from the args
const targetDir = String(args).replace(/,/g, '-')
// Throw an error if there is no target directory specified
if (!targetDir) {
tui.displayError(
'No target directory specified',
[
'Please specify the project directory',
` ${chalk.cyan('yarn create redwood-app')} ${chalk.green(
'<project-directory>'
)}`,
'',
'For example:',
` ${chalk.cyan('yarn create redwood-app')} ${chalk.green(
'my-redwood-app'
)}`,
].join('\n')
)
recordErrorViaTelemetry('No target directory specified')
await shutdownTelemetry()
process.exit(1)
}
const newAppDir = path.resolve(process.cwd(), targetDir)
const templateDir = path.resolve(__dirname, '../template')
// Engine check
await executeCompatibilityCheck(templateDir, yarnInstall)
// Determine ts/js preference
const useTypescript = await handleTypescriptPreference(typescriptFlag)
trace.getActiveSpan()?.setAttribute('typescript', useTypescript)
// Determine git preference
const useGit = await handleGitPreference(gitInitFlag)
trace.getActiveSpan()?.setAttribute('git', useGit)
// Create project files
await createProjectFiles(newAppDir, overwrite, yarn1)
// Install the node packages
if (yarnInstall) {
const yarnInstallStart = Date.now()
await installNodeModules(newAppDir)
trace
.getActiveSpan()
?.setAttribute('yarn-install-time', Date.now() - yarnInstallStart)
} else {
tui.drawText(
`${RedwoodStyling.warning(
'⚠'
)} Skipped yarn install step based on command line flag`
)
}
// Conditionally convert to javascript
if (!useTypescript) {
if (yarnInstall) {
await convertToJavascript(newAppDir)
} else {
tui.drawText(
[
`${RedwoodStyling.warning(
'⚠'
)} Unable to convert to javascript without yarn install step`,
' Please run the following command inside your project once yarn install has been executed:',
` ${RedwoodStyling.info("'yarn rw ts-to-js'")}`,
].join('\n')
)
}
}
// Generate types
if (yarnInstall) {
await generateTypes(newAppDir)
}
// Initialise git repo
if (useGit) {
await initialiseGit(newAppDir)
}
// Post install message
tui.drawText(
[
'',
RedwoodStyling.success('Thanks for trying out Redwood!'),
'',
` ⚡️ ${RedwoodStyling.redwood(
'Get up and running fast with this Quick Start guide'
)}: https://redwoodjs.com/docs/quick-start`,
'',
RedwoodStyling.header('Join the Community'),
'',
`${RedwoodStyling.redwood(
' ❖ Join our Forums'
)}: https://community.redwoodjs.com`,
`${RedwoodStyling.redwood(
' ❖ Join our Chat'
)}: https://discord.gg/redwoodjs`,
'',
RedwoodStyling.header('Get some help'),
'',
`${RedwoodStyling.redwood(
' ❖ Get started with the Tutorial'
)}: https://redwoodjs.com/docs/tutorial`,
`${RedwoodStyling.redwood(
' ❖ Read the Documentation'
)}: https://redwoodjs.com/docs`,
'',
RedwoodStyling.header('Stay updated'),
'',
`${RedwoodStyling.redwood(
' ❖ Sign up for our Newsletter'
)}: https://www.redwoodjs.com/newsletter`,
`${RedwoodStyling.redwood(
' ❖ Follow us on Twitter'
)}: https://twitter.com/redwoodjs`,
'',
`${RedwoodStyling.header(`Become a Contributor`)} ${RedwoodStyling.love(
'❤'
)}`,
'',
`${RedwoodStyling.redwood(
' ❖ Learn how to get started'
)}: https://redwoodjs.com/docs/contributing`,
`${RedwoodStyling.redwood(
' ❖ Find a Good First Issue'
)}: https://redwoodjs.com/good-first-issue`,
'',
`${RedwoodStyling.header(`Fire it up!`)} 🚀`,
'',
`${RedwoodStyling.redwood(
` > ${RedwoodStyling.green(`cd ${targetDir}`)}`
)}`,
`${RedwoodStyling.redwood(` > ${RedwoodStyling.green(`yarn rw dev`)}`)}`,
'',
].join('\n')
)
}
;(async () => {
// Conditionally start telemetry
if (telemetry !== 'false' && !process.env.REDWOOD_DISABLE_TELEMETRY) {
try {
await startTelemetry()
} catch (error) {
console.error('Telemetry startup error')
console.error(error)
}
}
// Execute create redwood app within a span
const tracer = trace.getTracer('redwoodjs')
await tracer.startActiveSpan('create-redwood-app', async (span) => {
await createRedwoodApp()
// Span housekeeping
span?.setStatus({ code: SpanStatusCode.OK })
span?.end()
})
// Shutdown telemetry, ensures data is sent before the process exits
try {
await shutdownTelemetry()
} catch (error) {
console.error('Telemetry shutdown error')
console.error(error)
}
})()