Repository navigation
Expand file tree
/
Copy pathgate.mjs
More file actions
229 lines (202 loc) · 7.17 KB
/
Copy pathgate.mjs
File metadata and controls
229 lines (202 loc) · 7.17 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
// Runs the steps of README.md with no person at the device, and checks the
// output of Expo CLI and the store after each run. The cache provider gate
// workflow calls it. It needs a running emulator, or a Mac with a simulator.
//
// Usage: node gate.mjs <android|ios> [more arguments for expo run]
//
// Each run uses --no-bundler. Expo CLI then stops after the upload, and
// does not wait for Metro.
import { execFileSync, spawn } from 'node:child_process'
import { readFileSync, rmSync, writeFileSync } from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
const root = path.dirname(fileURLToPath(import.meta.url))
const store = path.join(root, '.devhub')
const appJson = path.join(root, 'app.json')
// Expo SDK 58 has Swift packages with Swift tools 6.2 (Xcode 26). Expo CLI
// does not check it, and xcodebuild fails late with code 65.
const MIN_SWIFT = [6, 2]
class GateError extends Error {}
const [platform, ...runArgs] = process.argv.slice(2)
if (platform !== 'android' && platform !== 'ios') {
console.error('Usage: node gate.mjs <android|ios> [more arguments]')
process.exit(2)
}
function run(command, args) {
return new Promise((resolve, reject) => {
const child = spawn(
process.execPath,
[path.join(root, 'with-store.mjs'), command, ...args],
{ cwd: root, stdio: ['ignore', 'pipe', 'pipe'] },
)
let stdout = ''
let output = ''
child.stdout.on('data', (chunk) => {
process.stdout.write(chunk)
stdout += chunk
output += chunk
})
child.stderr.on('data', (chunk) => {
process.stderr.write(chunk)
output += chunk
})
child.on('error', reject)
child.on('close', (code) => resolve({ code, stdout, output }))
})
}
function check(condition, message) {
if (!condition) {
throw new GateError(message)
}
}
function expectLine(result, text) {
check(result.output.includes(text), `The output has no line with: ${text}`)
}
// On a busy CI runner, `simctl openurl` can time out after a good build and
// install. Expo CLI then stops before the upload, outside DevHub, so the gate
// tries the run one more time. A build that cannot start fails both times.
const OPEN_TIMEOUT = /simctl openurl .* exited with non-zero code: 60/
async function expoRun() {
const args = [`run:${platform}`, '--no-bundler', ...deviceArgs(), ...runArgs]
let result = await run('expo', args)
if (result.code !== 0 && OPEN_TIMEOUT.test(result.output)) {
console.log(
'\nThe simulator did not open the app in time. The gate does the run one more time.\n',
)
result = await run('expo', args)
}
check(result.code === 0, `expo run:${platform} exited with ${result.code}.`)
return result
}
let iosDevice
function deviceArgs() {
return iosDevice === undefined ? [] : ['--device', iosDevice]
}
// `bootstatus -b` boots the device, and waits until the boot is complete.
// Then the first run does not open the app while the boot still runs.
function bootSimulator(udid) {
execFileSync('xcrun', ['simctl', 'bootstatus', udid, '-b'], {
stdio: 'inherit',
})
}
function newestIphone() {
const { devices } = JSON.parse(
execFileSync('xcrun', ['simctl', 'list', 'devices', 'available', '-j'], {
encoding: 'utf8',
}),
)
const runtimes = Object.keys(devices)
.filter((runtime) => /\.iOS-\d/.test(runtime))
.sort((a, b) => compareVersions(version(b), version(a)))
for (const runtime of runtimes) {
const phone = devices[runtime].find((device) =>
device.name.startsWith('iPhone'),
)
if (phone !== undefined) {
console.log(`Simulator: ${phone.name} (${runtime})`)
return phone.udid
}
}
throw new GateError('No iPhone simulator is available.')
}
function version(text) {
return (text.match(/(\d+)[-.](\d+)/)?.slice(1) ?? []).map(Number)
}
function compareVersions(a, b) {
return (a[0] ?? 0) - (b[0] ?? 0) || (a[1] ?? 0) - (b[1] ?? 0)
}
function checkSwift() {
const text = execFileSync('xcrun', ['swift', '--version'], {
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'ignore'],
})
const found = version(text.match(/Swift version [\d.]+/)?.[0] ?? '')
check(
compareVersions(found, MIN_SWIFT) >= 0,
`Expo SDK 58 needs Swift ${MIN_SWIFT.join('.')} or later (Xcode 26), and this Mac has Swift ${found.join('.') || 'unknown'}. Select Xcode 26 with xcode-select.`,
)
}
async function storedBuilds(count) {
const result = await run('devhub', ['list', '--json'])
check(result.code === 0, `devhub list exited with ${result.code}.`)
const builds = JSON.parse(result.stdout).builds.filter(
(build) =>
build.source === 'local' &&
build.local.platform === platform.toUpperCase(),
)
check(
builds.length === count,
`The store has ${builds.length} ${platform} builds, not ${count}.`,
)
return builds
}
function removeNativeFolder() {
rmSync(path.join(root, platform), { recursive: true, force: true })
}
function step(text) {
console.log(`\n=== ${text}\n`)
}
const original = readFileSync(appJson, 'utf8')
const changed = original.replace(
'"orientation": "portrait"',
'"orientation": "default"',
)
try {
check(changed !== original, 'app.json has no "orientation": "portrait".')
if (platform === 'ios') {
checkSwift()
if (!runArgs.includes('--device')) {
iosDevice = newestIphone()
bootSimulator(iosDevice)
}
}
rmSync(store, { recursive: true, force: true })
removeNativeFolder()
step('1. The first run builds the app, and DevHub stores the build.')
let result = await expoRun()
expectLine(result, `No stored build has the ${platform} fingerprint`)
expectLine(result, 'Stored the build')
const [first] = await storedBuilds(1)
const firstId = first.id.slice(0, 8)
step('2. The second run installs the stored build.')
result = await expoRun()
expectLine(result, `Expo CLI installs the stored build ${firstId}`)
expectLine(result, store)
await storedBuilds(1)
step('3. A changed app config and the old native folder: no new build.')
writeFileSync(appJson, changed)
result = await expoRun()
expectLine(
result,
'DevHub does not store this build. app.json changed after the last prebuild',
)
await storedBuilds(1)
step('4. After a new prebuild, DevHub stores the build.')
removeNativeFolder()
result = await expoRun()
expectLine(result, 'Stored the build')
await storedBuilds(2)
step('5. The first app config again: the run installs the first build.')
writeFileSync(appJson, original)
removeNativeFolder()
result = await expoRun()
expectLine(result, `Expo CLI installs the stored build ${firstId}`)
await storedBuilds(2)
step('6. devhub match shows the first build.')
result = await run('devhub', ['match', '--json', '--platform', platform])
check(result.code === 0, `devhub match exited with ${result.code}.`)
const [match] = JSON.parse(result.stdout).platforms
check(
match?.matches.some((build) => build.id === first.id),
`devhub match does not show the build ${firstId}.`,
)
console.log(`\nThe ${platform} gate passed.`)
} catch (error) {
if (!(error instanceof GateError)) {
throw error
}
console.error(`\nThe ${platform} gate failed: ${error.message}`)
process.exitCode = 1
} finally {
writeFileSync(appJson, original)
}