-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
111 lines (101 loc) · 2.64 KB
/
main.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
const path = require("path")
const fs = require("fs")
const cp = require("child_process")
const assert = require("assert").strict
const chokidar = require("chokidar")
const tempy = require("tempy")
const { adminCredentials } = require("./constants.json")
const testsLockPath = path.join(__dirname, ".tests.lock")
const executablePath = path.join(__dirname, "../../run")
const getFreePort = function () {
const port = cp
.execFileSync("flock", [
"-x",
testsLockPath,
"-c",
`"${executablePath}" get_port_sync | tail -n +2`,
])
.toString()
.trim()
assert(!isNaN(parseInt(port)))
return port
}
const installWatcher = function () {
const signalFile = tempy.file()
const whenReady = new Promise(function (resolve) {
const fileName = path.basename(signalFile)
const watcher = chokidar.watch(signalFile, { persistent: true })
watcher.on("raw", function (event, someFileName) {
if (someFileName === fileName) {
watcher.close()
setTimeout(resolve, 1000)
}
})
})
return { signalFile, whenReady }
}
const serverPort = getFreePort()
const uiPort = getFreePort()
const {
signalFile: serverSignalFile,
whenReady: whenServerReady,
} = installWatcher()
const serverAddr = `http://127.0.0.1:${serverPort}`
const server = cp.spawn(
executablePath,
[
"--listen",
serverAddr,
"--port",
serverPort,
"--instance",
`${serverPort}__${Date.now()}`,
"--admin-credentials-for-test",
adminCredentials,
"--signal-file",
serverSignalFile,
"--port",
serverPort,
"test_server",
],
{ stdio: ["ignore", "inherit", "inherit"] },
)
const uiDir = path.join(__dirname, "..")
const uiAddr = `http://127.0.0.1:${uiPort}`
const { signalFile: uiSignalFile, whenReady: whenUiReady } = installWatcher()
const ui = cp.spawn(
"bash",
[
"-c",
`PORT=${uiPort} SIGNAL_FILE="${uiSignalFile}" API_URL="${serverAddr}" npm run --prefix "${uiDir}" start`,
],
{
stdio: ["ignore", "inherit", "inherit"],
},
)
const cleanOnExit = function () {
server.kill()
ui.kill()
cp.execFileSync(executablePath, [
"free_port",
uiPort,
{
stdio: ["ignore", "inherit", "inherit"],
},
])
process.exit()
}
process.on("SIGINT", cleanOnExit)
process.on("SIGTERM", cleanOnExit)
const operation = process.argv[2] || "run"
const cypressPath = path.join(__dirname, "./node_modules/.bin/cypress")
whenServerReady.then(function () {
whenUiReady.then(function () {
cp.spawnSync(
cypressPath,
[operation, "--env", `UIAddr=${uiAddr},ID=${uiPort}`],
{ cwd: __dirname, stdio: ["ignore", "inherit", "inherit"] },
)
cleanOnExit()
})
})