-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
vitestConfig.ts
77 lines (72 loc) · 2.21 KB
/
vitestConfig.ts
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
import { effect, reactive } from '@vue/reactivity'
import type { ResolvedConfig } from 'vitest'
import getPort from 'get-port'
import { log } from '../../log'
import type { VitestWorkspaceConfig } from '../../config'
import { getConfig } from '../../config'
import { execWithLog, sanitizeFilePath } from '../utils'
import { createClient } from './ws-client'
async function connectAndFetchConfig(
{ port, url = `ws://localhost:${port}/__vitest_api__`, reconnectInterval, reconnectTries }: {
url?: string
port: number
reconnectInterval?: number
reconnectTries?: number
},
) {
let onFailedConnection: (() => void) | undefined
const client = createClient(url, {
reactive: reactive as any,
reconnectInterval,
reconnectTries,
onFailedConnection: () => onFailedConnection?.(),
})
return new Promise<ResolvedConfig>((resolve, reject) => {
onFailedConnection = () => reject(new Error ('Unable to connect to Vitest API'))
const handled = new WeakSet()
effect(() => {
const ws = client.ws
if (!handled.has(ws)) {
handled.add(ws)
ws.addEventListener('open', () => {
log.info('WS Opened')
client.rpc.getConfig().then((_config) => {
client.dispose()
resolve(_config)
})
})
ws.addEventListener('error', (e) => {
console.error('WS ERROR', e)
})
ws.addEventListener('close', () => {
log.info('WS Close')
})
}
})
})
}
export async function fetchVitestConfig(
workspaceConfigs: VitestWorkspaceConfig[],
) {
const port = await getPort()
const workspace = workspaceConfigs.find(workspace =>
workspace.isCompatible && !workspace.isDisabled && workspace.isUsingVitestForSure)
if (!workspace)
return
const folder = workspace.workspace.uri.fsPath
const childProcess = execWithLog(
workspace.cmd,
[...workspace.args, '--api.port', port.toString(), '--api.host', '127.0.0.1'],
{
cwd: sanitizeFilePath(folder),
env: { ...process.env, ...getConfig(folder).env },
},
).child
const config = await connectAndFetchConfig({
port,
reconnectInterval: 500,
reconnectTries: 20,
})
childProcess.kill()
return config
}