-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.ts
More file actions
90 lines (76 loc) · 3.05 KB
/
utils.ts
File metadata and controls
90 lines (76 loc) · 3.05 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
import fs from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url'
import { resolve } from 'import-meta-resolve'
import type { WorkerMetaContext } from '@vscode-wdio/types'
import type { createTempConfigFile } from './config.js'
export type TempConfigFileCreator = typeof createTempConfigFile
const VSCODE_WINDOWS_CONFIG_CREATOR_PATH = path.resolve(__dirname, 'parser/ast.cjs')
let tempConfigCreator: TempConfigFileCreator | undefined
export async function getTempConfigCreator(context: WorkerMetaContext): Promise<TempConfigFileCreator> {
return (await dynamicLoader(
context,
tempConfigCreator,
VSCODE_WINDOWS_CONFIG_CREATOR_PATH,
'createTempConfigFile'
)) as TempConfigFileCreator
}
export function isWindows() {
return process.platform === 'win32'
}
type PackageJson = { name: string; version: string }
export async function isFixedWdio(this: WorkerMetaContext, configPath: string) {
try {
const pkgName = '@wdio/utils'
this.log.debug(`Try to detect the version of ${pkgName}`)
const utilEntryPoint = resolve(`${pkgName}`, resolve('@wdio/cli', pathToFileURL(configPath).href))
const utilPkg = await findPackageJson(fileURLToPath(utilEntryPoint))
if (!utilPkg) {
throw new Error(`Could not detect the entry point of ${pkgName}`)
}
const pkg = JSON.parse(await fs.readFile(utilPkg, { encoding: 'utf-8' })) as PackageJson
if (pkg.name !== pkgName) {
throw new Error(`Could not detect the version of ${pkgName}`)
}
this.log.debug(`Detected version of ${pkgName}@${pkg.version}`)
const versions = pkg.version.split('.')
if (Number(versions[0]) >= 10 || (Number(versions[0]) >= 9 && Number(versions[1]) >= 16)) {
this.log.debug(`Use cli argument to run WDIO: ${pkgName}@${pkg.version} >= 9.16.0`)
return true
}
this.log.debug(`Use temporary configuration files to run WDIO: ${pkgName}@${pkg.version} < 9.16.0`)
return false
} catch (error) {
const msg = error instanceof Error ? error.message : String(error)
this.log.debug(`Use temporary configuration files because ${msg}`)
return false
}
}
async function findPackageJson(startPath: string) {
let dir = path.dirname(startPath)
const root = path.parse(dir).root
while (dir !== root) {
const pkgPath = path.join(dir, 'package.json')
try {
await fs.access(pkgPath)
return pkgPath
} catch {
dir = path.dirname(dir)
}
}
return undefined
}
export async function dynamicLoader(
context: WorkerMetaContext,
libModule: unknown,
modulePath: string,
fnName: string
): Promise<unknown> {
if (libModule) {
context.log.debug(`Use cached ${path.dirname(modulePath)}`)
return libModule
}
context.log.debug(`Import ${path.basename(modulePath)}`)
libModule = (await import(pathToFileURL(modulePath).href))[fnName]
return libModule
}