-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·106 lines (93 loc) · 2.86 KB
/
index.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
#!/usr/bin/env node
import { createRequire } from 'module'
import path from 'path'
import { execa } from 'execa'
import { program } from 'commander'
import { build, preview, createServer } from 'vite'
program
.name('vite-e2e-cypress')
.description('Simple command line script to run cypress e2e with vite')
.option('-r, --root, <root>', 'root path for vite project')
.option('-p, --port <port>', 'port to serve the vite server')
.option('-c, --config-file <configFile>', 'path to vite config file')
.option('--headless', 'run in headless mode without GUI')
.option('-d, --dev', 'run dev server instead of using build & preview')
.option('-m, --mode <mode>', 'specify the mode the dev server should run in.', 'production')
.option('-s, --spec <spec>', '(headless only) runs a specific spec file.', 'all')
.option('-e, --e2e', 'run end to end tests' )
.option('-b, --browser <browser>', 'specify browser to run', 'chrome')
program.parse()
const options = program.opts()
const resolveModule = function (request, context) {
let resolvedPath
try {
resolvedPath = createRequire(path.resolve(context, 'package.json')).resolve(request)
} catch (e) {
console.warn(e)
}
return resolvedPath
}
;(async () => {
const cwd = process.cwd()
const root = options.root ? path.resolve(cwd, options.root) : cwd
const configFile = path.resolve(cwd, options.configFile || 'vite.config.ts')
const port = options.port || 8080
const mode = options.mode || 'production'
const configs = {
root,
configFile,
mode
}
let server = null
let previewServer = null
if (options.dev) {
server = await createServer({
...configs,
server: {
port
}
})
await server.listen()
server.printUrls()
} else {
await build(configs)
previewServer = await preview({
...configs,
preview: {
port,
open: false
}
})
previewServer.printUrls()
}
const closeServer = () => {
if (server) server.close()
if (previewServer) previewServer.httpServer.close()
}
let cyArgs = [
options.headless ? 'run' : 'open', // open or run
'--config', `baseUrl=http://localhost:${port}`,
options.e2e ? '--e2e' : ''
]
if (options.headless) {
cyArgs = cyArgs.concat(['--spec', options.spec])
}
if (options.browser) {
cyArgs = cyArgs.concat(['--browser', options.browser])
}
// Use loadModule to allow users to customize their Cypress dependency version.
const cypressModulePath = resolveModule('cypress', cwd)
const cypressBinPath = path.resolve(cypressModulePath, '../bin/cypress')
const runner = execa(cypressBinPath, cyArgs, { stdio: 'inherit' })
if (server || previewServer) {
runner.on('error', () => {
closeServer()
process.exit(1)
})
runner.on('exit', code => {
closeServer()
process.exit(code)
})
}
return runner
})()