Repository navigation
Expand file tree
/
Copy pathrun-e2e
More file actions
executable file
·320 lines (296 loc) · 8.14 KB
/
Copy pathrun-e2e
File metadata and controls
executable file
·320 lines (296 loc) · 8.14 KB
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
#!/usr/bin/env node
/* eslint-env node, es6*/
const os = require('os')
const path = require('path')
const execa = require('execa')
const fg = require('fast-glob')
const fs = require('fs-extra')
const { hideBin } = require('yargs/helpers')
const yargs = require('yargs/yargs')
// This script sets up a blank RedwoodJS app into a directory.
// It uses the packages from the RedwoodJS framework (../packages).
// So, if you're making changes to the framework,
// you can use the e2e tests to verify your changes.
//
// The steps are composable, so that it can work for the GitHub Workflow, and
// us contributors.
//
const makeDirectory = () => {
console.log('mkdir', REDWOOD_PROJECT_DIRECTORY)
fs.mkdirSync(REDWOOD_PROJECT_DIRECTORY, { recursive: true })
return REDWOOD_PROJECT_DIRECTORY
}
const buildRedwoodFramework = () => {
try {
const files = fg.sync('packages/**/dist', {
onlyDirectories: true,
})
execa.sync(
[files.length && 'yarn build:clean', 'yarn build']
.filter(Boolean)
.join('&&'),
{
cwd: REDWOODJS_FRAMEWORK_PATH,
shell: true,
stdio: 'inherit',
}
)
} catch (e) {
if (e.signal !== 'SIGINT') {
console.error('Error: Could not build Redwood Framework')
console.error(e)
}
process.exit(1)
}
}
const createRedwoodJSApp = ({ typescript, bundler }) => {
try {
execa.sync(
'yarn node dist/create-redwood-app.js',
[
REDWOOD_PROJECT_DIRECTORY,
'--no-yarn-install',
`--typescript ${typescript}`,
'--no-telemetry',
'--no-git',
].filter(Boolean),
{
cwd: path.join(REDWOODJS_FRAMEWORK_PATH, 'packages/create-redwood-app'),
env: { REDWOOD_CI: '1' },
shell: true,
stdio: 'inherit',
}
)
// Add prisma resolutions
const packageJSONPath = path.join(REDWOOD_PROJECT_DIRECTORY, 'package.json')
const packageJSON = fs.readJSONSync(packageJSONPath)
const getVersionFrmRwPkg = (dep, pkg) => {
return fs.readJSONSync(
path.join(REDWOODJS_FRAMEWORK_PATH, 'packages', pkg, 'package.json')
).dependencies[dep]
}
packageJSON.resolutions = {
prisma: getVersionFrmRwPkg('prisma', 'cli'),
'@prisma/client': getVersionFrmRwPkg('@prisma/client', 'api'),
'@prisma/internals': getVersionFrmRwPkg('@prisma/internals', 'cli'),
'graphql-yoga': getVersionFrmRwPkg('graphql-yoga', 'graphql-server'),
graphql: getVersionFrmRwPkg('graphql', 'graphql-server'),
}
fs.writeFileSync(packageJSONPath, JSON.stringify(packageJSON, null, 2))
if (bundler === 'webpack') {
const redwoodTOMLPath = path.join(
REDWOOD_PROJECT_DIRECTORY,
'redwood.toml'
)
const redwoodTOML = fs.readFileSync(redwoodTOMLPath, 'utf-8')
fs.writeFileSync(
redwoodTOMLPath,
redwoodTOML.replace('[web]\n', '[web]\n bundler = "webpack"\n')
)
}
} catch (e) {
if (e.signal !== 'SIGINT') {
console.error('Error: Could not create Redwood Project')
console.error(e)
}
process.exit(1)
}
}
const addFrameworkDepsToProject = () => {
try {
execa.sync('yarn project:deps', {
cwd: REDWOODJS_FRAMEWORK_PATH,
shell: true,
stdio: 'inherit',
env: {
RWFW_PATH: REDWOODJS_FRAMEWORK_PATH,
RWJS_CWD: REDWOOD_PROJECT_DIRECTORY,
},
})
} catch (e) {
if (e.signal !== 'SIGINT') {
console.error(
'Error: Could not add Redwood Framework dependencies to project'
)
console.error(e)
}
process.exit(1)
}
}
const copyFrameworkPackages = () => {
try {
execa.sync('yarn project:copy', {
cwd: REDWOODJS_FRAMEWORK_PATH,
shell: true,
stdio: 'inherit',
env: {
RWFW_PATH: REDWOODJS_FRAMEWORK_PATH,
RWJS_CWD: REDWOOD_PROJECT_DIRECTORY,
},
})
} catch (e) {
if (e.signal !== 'SIGINT') {
console.error('Error: We could not copy Redwood Framework packages')
console.error(e)
}
process.exit(1)
}
}
const runYarnInstall = () => {
try {
execa.sync('yarn install', {
cwd: REDWOOD_PROJECT_DIRECTORY,
shell: true,
stdio: 'inherit',
})
} catch (e) {
if (e.signal !== 'SIGINT') {
console.error('Error: Could not run `yarn install`')
console.error(e)
}
process.exit(1)
}
}
const runDevServerInBackground = () => {
try {
console.log('Starting RedwoodJS dev server...')
execa('yarn rw dev --no-generate --fwd="--no-open"', {
cwd: REDWOOD_PROJECT_DIRECTORY,
shell: true,
stdio: 'inherit',
env: {
RWJS_DELAY_RESTART: '500',
REDWOOD_CI: '1',
},
})
} catch (e) {
if (e.signal !== 'SIGINT') {
console.error('There was an error with the RedwoodJS dev server:')
console.error(e)
}
process.exit(1)
}
}
const runCypress = () => {
try {
console.log('Starting Cypress...')
execa.sync(
// do not use Yarn to run Cypress; this avoids missing binary errors
'../../node_modules/.bin/cypress',
[
'open',
`--env RW_PATH=${REDWOOD_PROJECT_DIRECTORY}`,
'--e2e',
'--browser chrome',
],
{
cwd: path.join(REDWOODJS_FRAMEWORK_PATH, 'tasks/e2e'),
shell: true,
stdio: 'inherit',
}
)
} catch (e) {
if (e.signal !== 'SIGINT') {
console.error('There was an error with Cypress:')
console.error(e)
}
process.exit(1)
}
}
const initGit = () => {
try {
console.log('Initializing Git')
execa.sync('git init --initial-branch main && git add .', {
cwd: REDWOOD_PROJECT_DIRECTORY,
shell: true,
stdio: 'inherit',
})
execa.sync('git commit -a --message=init', {
cwd: REDWOOD_PROJECT_DIRECTORY,
shell: true,
stdio: 'inherit',
})
} catch (e) {
if (e.signal !== 'SIGINT') {
console.error(
'There was an error with the `git init` or `git commit` step:'
)
console.error(e)
}
process.exit(1)
}
}
const args = yargs(hideBin(process.argv))
.option('create-project', { default: true, type: 'boolean', alias: 'create' })
.option('build-framework', { default: true, type: 'boolean', alias: 'build' })
.option('copy-framework', { default: true, type: 'boolean', alias: 'copy' })
.option('typescript', { default: false, type: 'boolean', alias: 'ts' })
.option('auto-start', { default: true, type: 'boolean', alias: 'start' })
.option('clean-files', { default: true, type: 'boolean' })
.option('bundler', {
default: 'vite',
type: 'string',
choices: ['vite', 'webpack'],
})
.scriptName('run-e2e')
.example('run-e2e')
.example('run-e2e /tmp/redwood-app --ts')
.help()
.parse()
const REDWOODJS_FRAMEWORK_PATH = path.resolve(__dirname, '..')
let REDWOOD_PROJECT_DIRECTORY =
args._?.[0] ||
path.join(
os.tmpdir(),
'redwood-e2e',
// ":" is problematic with paths
new Date().toISOString().split(':').join('-')
)
console.log()
console.log('-'.repeat(80))
console.log()
makeDirectory(REDWOOD_PROJECT_DIRECTORY)
console.log()
console.log('-'.repeat(80))
let {
buildFramework,
copyFramework,
createProject,
typescript,
bundler,
autoStart,
cleanFiles,
} = args
const tasks = [
buildFramework && buildRedwoodFramework,
createProject && (() => createRedwoodJSApp({ typescript, bundler })),
copyFramework && addFrameworkDepsToProject,
copyFramework && runYarnInstall,
copyFramework && copyFrameworkPackages,
autoStart && runDevServerInBackground,
autoStart && initGit,
autoStart && runCypress,
].filter(Boolean)
process.on('SIGINT', () => {
process.exit(0)
})
process.on('exit', () => {
console.log('')
console.log('-'.repeat(80))
if (cleanFiles && autoStart) {
console.log('Cleaning up e2e resources...')
console.log(' - Cleaning up files (may take a few seconds)...')
fs.rmSync(REDWOOD_PROJECT_DIRECTORY, { recursive: true, force: true })
console.log('Clean up complete')
} else {
console.log(
`E2E files within "${REDWOOD_PROJECT_DIRECTORY}" have not been automatically cleaned up.`
)
}
})
for (const task of tasks) {
console.log()
task()
console.log()
console.log('-'.repeat(80))
}