Skip to content

Commit f77ed90

Browse files
committed
fix: format url when execute dynamic import
1 parent 93e1f2d commit f77ed90

2 files changed

Lines changed: 30 additions & 21 deletions

File tree

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from 'node:path'
22

3+
import { dynamicLoader } from '../utils.js'
34
import type { WorkerMetaContext } from '@vscode-wdio/types/worker'
45
import type { parseCucumberFeature } from './cucumber.js'
56
import type { parseTestCases } from './js.js'
@@ -14,21 +15,14 @@ let cucumberParser: CucumberParser | undefined
1415
let astParser: AstParser | undefined
1516

1617
export async function getCucumberParser(context: WorkerMetaContext): Promise<CucumberParser> {
17-
if (cucumberParser) {
18-
context.log.debug('Use cached Cucumber parser')
19-
return cucumberParser
20-
}
21-
context.log.debug('Import Cucumber parser')
22-
cucumberParser = (await import(CUCUMBER_PARSER_PATH)).parseCucumberFeature as CucumberParser
23-
return cucumberParser
18+
return (await dynamicLoader(
19+
context,
20+
cucumberParser,
21+
CUCUMBER_PARSER_PATH,
22+
'parseCucumberFeature'
23+
)) as CucumberParser
2424
}
2525

2626
export async function getAstParser(context: WorkerMetaContext): Promise<AstParser> {
27-
if (astParser) {
28-
context.log.debug('Use cached Ast parser')
29-
return astParser
30-
}
31-
context.log.debug('Import Ast parser')
32-
astParser = (await import(AST_PARSER_PATH)).parseTestCases as AstParser
33-
return astParser
27+
return (await dynamicLoader(context, astParser, AST_PARSER_PATH, 'parseTestCases')) as AstParser
3428
}
Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from 'node:path'
2+
import { pathToFileURL } from 'node:url'
23

34
import type { WorkerMetaContext } from '@vscode-wdio/types'
45
import type { createTempConfigFile } from './config.js'
@@ -10,15 +11,29 @@ const VSCODE_WINDOWS_CONFIG_CREATOR_PATH = path.resolve(__dirname, 'parser/ast.c
1011
let tempConfigCreator: TempConfigFileCreator | undefined
1112

1213
export async function getTempConfigCreator(context: WorkerMetaContext): Promise<TempConfigFileCreator> {
13-
if (tempConfigCreator) {
14-
context.log.debug('Use cached TempConfigFileCreator')
15-
return tempConfigCreator
16-
}
17-
context.log.debug('Import TempConfigFileCreator')
18-
tempConfigCreator = (await import(VSCODE_WINDOWS_CONFIG_CREATOR_PATH)).createTempConfigFile as TempConfigFileCreator
19-
return tempConfigCreator
14+
return (await dynamicLoader(
15+
context,
16+
tempConfigCreator,
17+
VSCODE_WINDOWS_CONFIG_CREATOR_PATH,
18+
'createTempConfigFile'
19+
)) as TempConfigFileCreator
2020
}
2121

2222
export function isWindows() {
2323
return process.platform === 'win32'
2424
}
25+
26+
export async function dynamicLoader(
27+
context: WorkerMetaContext,
28+
libModule: unknown,
29+
modulePath: string,
30+
fnName: string
31+
): Promise<unknown> {
32+
if (libModule) {
33+
context.log.debug(`Use cached ${path.dirname(modulePath)}`)
34+
return libModule
35+
}
36+
context.log.debug(`Import ${path.basename(modulePath)}`)
37+
libModule = (await import(pathToFileURL(modulePath).href))[fnName]
38+
return libModule
39+
}

0 commit comments

Comments
 (0)