/
index.ts
152 lines (130 loc) · 4.19 KB
/
index.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
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
// eslint-disable-next-line no-restricted-imports
import { relative as relativeBrowser } from 'path'
import c from 'picocolors'
import { isPackageExists } from 'local-pkg'
import { relative as relativeNode } from 'pathe'
import type { ModuleCacheMap } from 'vite-node'
import type { Suite, Task } from '../types'
import { EXIT_CODE_RESTART } from '../constants'
import { getNames } from './tasks'
export * from './tasks'
export * from './base'
export * from './global'
export * from './timers'
export const isNode = typeof process < 'u' && typeof process.stdout < 'u' && !process.versions?.deno && !globalThis.window
// export const isNode = typeof process !== 'undefined' && typeof process.platform !== 'undefined'
export const isBrowser = typeof window !== 'undefined'
export const isWindows = isNode && process.platform === 'win32'
export const relativePath = isBrowser ? relativeBrowser : relativeNode
/**
* Partition in tasks groups by consecutive concurrent
*/
export function partitionSuiteChildren(suite: Suite) {
let tasksGroup: Task[] = []
const tasksGroups: Task[][] = []
for (const c of suite.tasks) {
if (tasksGroup.length === 0 || c.concurrent === tasksGroup[0].concurrent) {
tasksGroup.push(c)
}
else {
tasksGroups.push(tasksGroup)
tasksGroup = [c]
}
}
if (tasksGroup.length > 0)
tasksGroups.push(tasksGroup)
return tasksGroups
}
export function resetModules(modules: ModuleCacheMap, resetMocks = false) {
const skipPaths = [
// Vitest
/\/vitest\/dist\//,
// yarn's .store folder
/vitest-virtual-\w+\/dist/,
// cnpm
/@vitest\/dist/,
// don't clear mocks
...(!resetMocks ? [/^mock:/] : []),
]
modules.forEach((_, path) => {
if (skipPaths.some(re => re.test(path)))
return
modules.delete(path)
})
}
export function getFullName(task: Task) {
return getNames(task).join(c.dim(' > '))
}
export async function ensurePackageInstalled(
dependency: string,
root: string,
) {
if (isPackageExists(dependency, { paths: [root] }))
return true
const promptInstall = !process.env.CI && process.stdout.isTTY
process.stderr.write(c.red(`${c.inverse(c.red(' MISSING DEP '))} Can not find dependency '${dependency}'\n\n`))
if (!promptInstall)
return false
const prompts = await import('prompts')
const { install } = await prompts.prompt({
type: 'confirm',
name: 'install',
message: c.reset(`Do you want to install ${c.green(dependency)}?`),
})
if (install) {
await (await import('@antfu/install-pkg')).installPackage(dependency, { dev: true })
// TODO: somehow it fails to load the package after installation, remove this when it's fixed
process.stderr.write(c.yellow(`\nPackage ${dependency} installed, re-run the command to start.\n`))
process.exit(EXIT_CODE_RESTART)
return true
}
return false
}
/**
* If code starts with a function call, will return its last index, respecting arguments.
* This will return 25 - last ending character of toMatch ")"
* Also works with callbacks
* ```
* toMatch({ test: '123' });
* toBeAliased('123')
* ```
*/
export function getCallLastIndex(code: string) {
let charIndex = -1
let inString: string | null = null
let startedBracers = 0
let endedBracers = 0
let beforeChar: string | null = null
while (charIndex <= code.length) {
beforeChar = code[charIndex]
charIndex++
const char = code[charIndex]
const isCharString = char === '"' || char === '\'' || char === '`'
if (isCharString && beforeChar !== '\\') {
if (inString === char)
inString = null
else if (!inString)
inString = char
}
if (!inString) {
if (char === '(')
startedBracers++
if (char === ')')
endedBracers++
}
if (startedBracers && endedBracers && startedBracers === endedBracers)
return charIndex
}
return null
}
const resolve = isNode ? relativeNode : relativeBrowser
export { resolve as resolvePath }
// AggregateError is supported in Node.js 15.0.0+
class AggregateErrorPonyfill extends Error {
errors: unknown[]
constructor(errors: Iterable<unknown>, message = '') {
super(message)
this.errors = [...errors]
}
}
export { AggregateErrorPonyfill as AggregateError }