-
Notifications
You must be signed in to change notification settings - Fork 969
/
run-e2e
executable file
·248 lines (231 loc) · 6.19 KB
/
run-e2e
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
#!/usr/bin/env node
/* eslint-env node, es6*/
const os = require('os')
const path = require('path')
const execa = require('execa')
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 {
execa.sync('yarn build:clean && yarn build:js', {
cwd: REDWOODJS_FRAMEWORK_PATH,
shell: true,
stdio: 'inherit',
})
} catch (e) {
console.error('Error: Could not build Redwood Framework')
console.error(e)
process.exit(1)
}
}
const createRedwoodJSApp = () => {
try {
execa.sync(
'yarn babel-node src/create-redwood-app.js',
[REDWOOD_PROJECT_DIRECTORY, '--no-yarn-install', '--typescript'],
{
cwd: path.join(REDWOODJS_FRAMEWORK_PATH, 'packages/create-redwood-app'),
shell: true,
stdio: 'inherit',
}
)
} catch (e) {
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) {
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) {
console.error('Error: We could not copy Redwood Framework packages')
console.error(e)
process.exit(1)
}
}
const checkYarnVersion = () => {
try {
const { stdout } = execa.sync('yarn --version', {
cwd: REDWOOD_PROJECT_DIRECTORY,
shell: true,
})
console.log('Yarn version is', stdout)
if (!stdout.startsWith('1')) {
setYarnClassic()
}
} catch (e) {
console.error(e)
console.error('Error: We could not find yarn')
process.exit(1)
}
}
const setYarnClassic = () => {
try {
execa.sync('yarn set version classic', {
cwd: REDWOOD_PROJECT_DIRECTORY,
shell: true,
stdio: 'inherit',
})
} catch (e) {
console.error(e)
console.error('Error: We could not set yarn classic')
process.exit(1)
}
}
const runYarnInstall = () => {
try {
execa.sync('yarn install', {
cwd: REDWOOD_PROJECT_DIRECTORY,
shell: true,
stdio: 'inherit',
})
} catch (e) {
console.error('Error: Could not run `yarn install`')
console.error(e)
process.exit(1)
}
}
const convertProjectToJavaScript = () => {
try {
execa.sync('yarn rw ts-to-js', {
cwd: REDWOOD_PROJECT_DIRECTORY,
shell: true,
stdio: 'inherit',
})
} catch (e) {
console.error('Could not convert the TypeScript app to JavaScript`')
console.error(e)
process.exit(1)
}
}
const runDevServerInBackground = () => {
console.log('Starting RedwoodJS dev server...')
execa.sync('yarn rw dev --no-generate --fwd="--no-open" &', {
cwd: REDWOOD_PROJECT_DIRECTORY,
shell: true,
stdio: 'inherit',
env: {
RWJS_DELAY_RESTART: '500',
},
})
}
const runCypress = () => {
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}`],
{
cwd: path.join(REDWOODJS_FRAMEWORK_PATH, 'tasks/e2e'),
shell: true,
stdio: 'inherit',
}
)
}
const initGit = () => {
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',
})
}
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' })
.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, autoStart } =
args
const tasks = [
buildFramework && buildRedwoodFramework,
createProject && createRedwoodJSApp,
copyFramework && addFrameworkDepsToProject,
copyFramework && checkYarnVersion,
copyFramework && runYarnInstall,
copyFramework && copyFrameworkPackages,
!typescript && convertProjectToJavaScript,
autoStart && runDevServerInBackground,
autoStart && initGit,
autoStart && runCypress,
].filter(Boolean)
for (const task of tasks) {
console.log()
task()
console.log()
console.log('-'.repeat(80))
}
process.on('SIGINT', () => {
console.log()
console.log(REDWOOD_PROJECT_DIRECTORY)
console.log('-'.repeat(80))
process.exit(0)
})