codeceptjs 4.0.0-beta.13 → 4.0.0-beta.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/config.js +7 -128
- package/lib/container.js +8 -133
- package/lib/utils/typescript.js +186 -0
- package/package.json +1 -1
- package/typings/promiseBasedTypes.d.ts +4 -0
- package/typings/types.d.ts +4 -0
package/lib/config.js
CHANGED
|
@@ -2,6 +2,7 @@ import fs from 'fs'
|
|
|
2
2
|
import path from 'path'
|
|
3
3
|
import { createRequire } from 'module'
|
|
4
4
|
import { fileExists, isFile, deepMerge, deepClone } from './utils.js'
|
|
5
|
+
import { transpileTypeScript, cleanupTempFiles } from './utils/typescript.js'
|
|
5
6
|
|
|
6
7
|
const defaultConfig = {
|
|
7
8
|
output: './_output',
|
|
@@ -156,137 +157,15 @@ async function loadConfigFile(configFile) {
|
|
|
156
157
|
// For .ts files, try to compile and load as JavaScript
|
|
157
158
|
if (extensionName === '.ts') {
|
|
158
159
|
try {
|
|
159
|
-
//
|
|
160
|
-
const
|
|
161
|
-
|
|
162
|
-
// Recursively transpile the file and its dependencies
|
|
163
|
-
const transpileTS = (filePath) => {
|
|
164
|
-
const tsContent = fs.readFileSync(filePath, 'utf8')
|
|
165
|
-
|
|
166
|
-
// Transpile TypeScript to JavaScript with ES module output
|
|
167
|
-
let jsContent = transpile(tsContent, {
|
|
168
|
-
module: 99, // ModuleKind.ESNext
|
|
169
|
-
target: 99, // ScriptTarget.ESNext
|
|
170
|
-
esModuleInterop: true,
|
|
171
|
-
allowSyntheticDefaultImports: true,
|
|
172
|
-
})
|
|
173
|
-
|
|
174
|
-
// Check if the code uses __dirname or __filename (CommonJS globals)
|
|
175
|
-
const usesCommonJSGlobals = /__dirname|__filename/.test(jsContent)
|
|
176
|
-
|
|
177
|
-
if (usesCommonJSGlobals) {
|
|
178
|
-
// Inject ESM equivalents at the top of the file
|
|
179
|
-
const esmGlobals = `import { fileURLToPath as __fileURLToPath } from 'url';
|
|
180
|
-
import { dirname as __dirname_fn } from 'path';
|
|
181
|
-
const __filename = __fileURLToPath(import.meta.url);
|
|
182
|
-
const __dirname = __dirname_fn(__filename);
|
|
183
|
-
|
|
184
|
-
`
|
|
185
|
-
jsContent = esmGlobals + jsContent
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
return jsContent
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
// Create a map to track transpiled files
|
|
192
|
-
const transpiledFiles = new Map()
|
|
193
|
-
const baseDir = path.dirname(configFile)
|
|
194
|
-
|
|
195
|
-
// Transpile main file
|
|
196
|
-
let jsContent = transpileTS(configFile)
|
|
197
|
-
|
|
198
|
-
// Find and transpile all relative TypeScript imports
|
|
199
|
-
// Match: import ... from './file' or '../file' or './file.ts'
|
|
200
|
-
const importRegex = /from\s+['"](\..+?)(?:\.ts)?['"]/g
|
|
201
|
-
let match
|
|
202
|
-
const imports = []
|
|
203
|
-
|
|
204
|
-
while ((match = importRegex.exec(jsContent)) !== null) {
|
|
205
|
-
imports.push(match[1])
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
// Transpile each imported TypeScript file
|
|
209
|
-
for (const relativeImport of imports) {
|
|
210
|
-
let importedPath = path.resolve(baseDir, relativeImport)
|
|
211
|
-
|
|
212
|
-
// Handle .js extensions that might actually be .ts files
|
|
213
|
-
if (importedPath.endsWith('.js')) {
|
|
214
|
-
const tsVersion = importedPath.replace(/\.js$/, '.ts')
|
|
215
|
-
if (fs.existsSync(tsVersion)) {
|
|
216
|
-
importedPath = tsVersion
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
// Try adding .ts extension if file doesn't exist and no extension provided
|
|
221
|
-
if (!path.extname(importedPath)) {
|
|
222
|
-
if (fs.existsSync(importedPath + '.ts')) {
|
|
223
|
-
importedPath = importedPath + '.ts'
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
// If it's a TypeScript file, transpile it
|
|
228
|
-
if (importedPath.endsWith('.ts') && fs.existsSync(importedPath)) {
|
|
229
|
-
const transpiledImportContent = transpileTS(importedPath)
|
|
230
|
-
const tempImportFile = importedPath.replace(/\.ts$/, '.temp.mjs')
|
|
231
|
-
fs.writeFileSync(tempImportFile, transpiledImportContent)
|
|
232
|
-
transpiledFiles.set(importedPath, tempImportFile)
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
// Replace imports in the main file to point to temp .mjs files
|
|
237
|
-
jsContent = jsContent.replace(
|
|
238
|
-
/from\s+['"](\..+?)(?:\.ts)?['"]/g,
|
|
239
|
-
(match, importPath) => {
|
|
240
|
-
let resolvedPath = path.resolve(baseDir, importPath)
|
|
241
|
-
|
|
242
|
-
// Handle .js extension that might be .ts
|
|
243
|
-
if (resolvedPath.endsWith('.js')) {
|
|
244
|
-
const tsVersion = resolvedPath.replace(/\.js$/, '.ts')
|
|
245
|
-
if (transpiledFiles.has(tsVersion)) {
|
|
246
|
-
const tempFile = transpiledFiles.get(tsVersion)
|
|
247
|
-
const relPath = path.relative(baseDir, tempFile).replace(/\\/g, '/')
|
|
248
|
-
return `from './${relPath}'`
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
// Try with .ts extension
|
|
253
|
-
const tsPath = resolvedPath.endsWith('.ts') ? resolvedPath : resolvedPath + '.ts'
|
|
254
|
-
|
|
255
|
-
// If we transpiled this file, use the temp file
|
|
256
|
-
if (transpiledFiles.has(tsPath)) {
|
|
257
|
-
const tempFile = transpiledFiles.get(tsPath)
|
|
258
|
-
// Get relative path from main temp file to this temp file
|
|
259
|
-
const relPath = path.relative(baseDir, tempFile).replace(/\\/g, '/')
|
|
260
|
-
return `from './${relPath}'`
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
// Otherwise, keep the import as-is
|
|
264
|
-
return match
|
|
265
|
-
}
|
|
266
|
-
)
|
|
267
|
-
|
|
268
|
-
// Create a temporary JS file with .mjs extension to force ES module treatment
|
|
269
|
-
const tempJsFile = configFile.replace('.ts', '.temp.mjs')
|
|
270
|
-
fs.writeFileSync(tempJsFile, jsContent)
|
|
271
|
-
|
|
272
|
-
// Store all temp files for cleanup
|
|
273
|
-
const allTempFiles = [tempJsFile, ...Array.from(transpiledFiles.values())]
|
|
160
|
+
// Use the TypeScript transpilation utility
|
|
161
|
+
const typescript = require('typescript')
|
|
162
|
+
const { tempFile, allTempFiles } = await transpileTypeScript(configFile, typescript)
|
|
274
163
|
|
|
275
164
|
try {
|
|
276
|
-
configModule = await import(
|
|
277
|
-
|
|
278
|
-
for (const file of allTempFiles) {
|
|
279
|
-
if (fs.existsSync(file)) {
|
|
280
|
-
fs.unlinkSync(file)
|
|
281
|
-
}
|
|
282
|
-
}
|
|
165
|
+
configModule = await import(tempFile)
|
|
166
|
+
cleanupTempFiles(allTempFiles)
|
|
283
167
|
} catch (err) {
|
|
284
|
-
|
|
285
|
-
for (const file of allTempFiles) {
|
|
286
|
-
if (fs.existsSync(file)) {
|
|
287
|
-
fs.unlinkSync(file)
|
|
288
|
-
}
|
|
289
|
-
}
|
|
168
|
+
cleanupTempFiles(allTempFiles)
|
|
290
169
|
throw err
|
|
291
170
|
}
|
|
292
171
|
} catch (tsError) {
|
package/lib/container.js
CHANGED
|
@@ -5,6 +5,7 @@ import debugModule from 'debug'
|
|
|
5
5
|
const debug = debugModule('codeceptjs:container')
|
|
6
6
|
import { MetaStep } from './step.js'
|
|
7
7
|
import { methodsOfObject, fileExists, isFunction, isAsyncFunction, installedLocally, deepMerge } from './utils.js'
|
|
8
|
+
import { transpileTypeScript, cleanupTempFiles } from './utils/typescript.js'
|
|
8
9
|
import Translation from './translation.js'
|
|
9
10
|
import MochaFactory from './mocha/factory.js'
|
|
10
11
|
import recorder from './recorder.js'
|
|
@@ -683,124 +684,14 @@ async function loadSupportObject(modulePath, supportObjectName) {
|
|
|
683
684
|
// Handle TypeScript files
|
|
684
685
|
if (ext === '.ts') {
|
|
685
686
|
try {
|
|
686
|
-
|
|
687
|
+
// Use the TypeScript transpilation utility
|
|
688
|
+
const typescript = await import('typescript')
|
|
689
|
+
const { tempFile, allTempFiles } = await transpileTypeScript(importPath, typescript)
|
|
687
690
|
|
|
688
|
-
|
|
689
|
-
const transpileTS = (filePath) => {
|
|
690
|
-
const tsContent = fs.readFileSync(filePath, 'utf8')
|
|
691
|
-
|
|
692
|
-
// Transpile TypeScript to JavaScript with ES module output
|
|
693
|
-
let jsContent = transpile(tsContent, {
|
|
694
|
-
module: 99, // ModuleKind.ESNext
|
|
695
|
-
target: 99, // ScriptTarget.ESNext
|
|
696
|
-
esModuleInterop: true,
|
|
697
|
-
allowSyntheticDefaultImports: true,
|
|
698
|
-
})
|
|
699
|
-
|
|
700
|
-
// Check if the code uses __dirname or __filename (CommonJS globals)
|
|
701
|
-
const usesCommonJSGlobals = /__dirname|__filename/.test(jsContent)
|
|
702
|
-
|
|
703
|
-
if (usesCommonJSGlobals) {
|
|
704
|
-
// Inject ESM equivalents at the top of the file
|
|
705
|
-
const esmGlobals = `import { fileURLToPath as __fileURLToPath } from 'url';
|
|
706
|
-
import { dirname as __dirname_fn } from 'path';
|
|
707
|
-
const __filename = __fileURLToPath(import.meta.url);
|
|
708
|
-
const __dirname = __dirname_fn(__filename);
|
|
709
|
-
|
|
710
|
-
`
|
|
711
|
-
jsContent = esmGlobals + jsContent
|
|
712
|
-
}
|
|
713
|
-
|
|
714
|
-
return jsContent
|
|
715
|
-
}
|
|
716
|
-
|
|
717
|
-
// Create a map to track transpiled files
|
|
718
|
-
const transpiledFiles = new Map()
|
|
719
|
-
const baseDir = path.dirname(importPath)
|
|
720
|
-
|
|
721
|
-
// Transpile main file
|
|
722
|
-
let jsContent = transpileTS(importPath)
|
|
723
|
-
|
|
724
|
-
// Find and transpile all relative TypeScript imports
|
|
725
|
-
// Match: import ... from './file' or '../file' or './file.ts'
|
|
726
|
-
const importRegex = /from\s+['"](\..+?)(?:\.ts)?['"]/g
|
|
727
|
-
let match
|
|
728
|
-
const imports = []
|
|
729
|
-
|
|
730
|
-
while ((match = importRegex.exec(jsContent)) !== null) {
|
|
731
|
-
imports.push(match[1])
|
|
732
|
-
}
|
|
733
|
-
|
|
734
|
-
// Transpile each imported TypeScript file
|
|
735
|
-
for (const relativeImport of imports) {
|
|
736
|
-
let importedPath = path.resolve(baseDir, relativeImport)
|
|
737
|
-
|
|
738
|
-
// Handle .js extensions that might actually be .ts files
|
|
739
|
-
if (importedPath.endsWith('.js')) {
|
|
740
|
-
const tsVersion = importedPath.replace(/\.js$/, '.ts')
|
|
741
|
-
if (fs.existsSync(tsVersion)) {
|
|
742
|
-
importedPath = tsVersion
|
|
743
|
-
}
|
|
744
|
-
}
|
|
745
|
-
|
|
746
|
-
// Try adding .ts extension if file doesn't exist and no extension provided
|
|
747
|
-
if (!path.extname(importedPath)) {
|
|
748
|
-
if (fs.existsSync(importedPath + '.ts')) {
|
|
749
|
-
importedPath = importedPath + '.ts'
|
|
750
|
-
}
|
|
751
|
-
}
|
|
752
|
-
|
|
753
|
-
// If it's a TypeScript file, transpile it
|
|
754
|
-
if (importedPath.endsWith('.ts') && fs.existsSync(importedPath)) {
|
|
755
|
-
const transpiledImportContent = transpileTS(importedPath)
|
|
756
|
-
const tempImportFile = importedPath.replace(/\.ts$/, '.temp.mjs')
|
|
757
|
-
fs.writeFileSync(tempImportFile, transpiledImportContent)
|
|
758
|
-
transpiledFiles.set(importedPath, tempImportFile)
|
|
759
|
-
debug(`Transpiled dependency: ${importedPath} -> ${tempImportFile}`)
|
|
760
|
-
}
|
|
761
|
-
}
|
|
762
|
-
|
|
763
|
-
// Replace imports in the main file to point to temp .mjs files
|
|
764
|
-
jsContent = jsContent.replace(
|
|
765
|
-
/from\s+['"](\..+?)(?:\.ts)?['"]/g,
|
|
766
|
-
(match, importPath) => {
|
|
767
|
-
let resolvedPath = path.resolve(baseDir, importPath)
|
|
768
|
-
|
|
769
|
-
// Handle .js extension that might be .ts
|
|
770
|
-
if (resolvedPath.endsWith('.js')) {
|
|
771
|
-
const tsVersion = resolvedPath.replace(/\.js$/, '.ts')
|
|
772
|
-
if (transpiledFiles.has(tsVersion)) {
|
|
773
|
-
const tempFile = transpiledFiles.get(tsVersion)
|
|
774
|
-
const relPath = path.relative(baseDir, tempFile).replace(/\\/g, '/')
|
|
775
|
-
return `from './${relPath}'`
|
|
776
|
-
}
|
|
777
|
-
}
|
|
778
|
-
|
|
779
|
-
// Try with .ts extension
|
|
780
|
-
const tsPath = resolvedPath.endsWith('.ts') ? resolvedPath : resolvedPath + '.ts'
|
|
781
|
-
|
|
782
|
-
// If we transpiled this file, use the temp file
|
|
783
|
-
if (transpiledFiles.has(tsPath)) {
|
|
784
|
-
const tempFile = transpiledFiles.get(tsPath)
|
|
785
|
-
// Get relative path from main temp file to this temp file
|
|
786
|
-
const relPath = path.relative(baseDir, tempFile).replace(/\\/g, '/')
|
|
787
|
-
return `from './${relPath}'`
|
|
788
|
-
}
|
|
789
|
-
|
|
790
|
-
// Otherwise, keep the import as-is
|
|
791
|
-
return match
|
|
792
|
-
}
|
|
793
|
-
)
|
|
794
|
-
|
|
795
|
-
// Create a temporary JS file with .mjs extension for the main file
|
|
796
|
-
tempJsFile = importPath.replace(/\.ts$/, '.temp.mjs')
|
|
797
|
-
fs.writeFileSync(tempJsFile, jsContent)
|
|
798
|
-
|
|
799
|
-
// Store all temp files for cleanup
|
|
800
|
-
const allTempFiles = [tempJsFile, ...Array.from(transpiledFiles.values())]
|
|
691
|
+
debug(`Transpiled TypeScript file: ${importPath} -> ${tempFile}`)
|
|
801
692
|
|
|
802
693
|
// Attach cleanup handler
|
|
803
|
-
importPath =
|
|
694
|
+
importPath = tempFile
|
|
804
695
|
// Store temp files list in a way that cleanup can access them
|
|
805
696
|
tempJsFile = allTempFiles
|
|
806
697
|
|
|
@@ -820,30 +711,14 @@ const __dirname = __dirname_fn(__filename);
|
|
|
820
711
|
// Clean up temp files if created before rethrowing
|
|
821
712
|
if (tempJsFile) {
|
|
822
713
|
const filesToClean = Array.isArray(tempJsFile) ? tempJsFile : [tempJsFile]
|
|
823
|
-
|
|
824
|
-
try {
|
|
825
|
-
if (fs.existsSync(file)) {
|
|
826
|
-
fs.unlinkSync(file)
|
|
827
|
-
}
|
|
828
|
-
} catch (cleanupError) {
|
|
829
|
-
// Ignore cleanup errors
|
|
830
|
-
}
|
|
831
|
-
}
|
|
714
|
+
cleanupTempFiles(filesToClean)
|
|
832
715
|
}
|
|
833
716
|
throw importError
|
|
834
717
|
} finally {
|
|
835
718
|
// Clean up temp files if created
|
|
836
719
|
if (tempJsFile) {
|
|
837
720
|
const filesToClean = Array.isArray(tempJsFile) ? tempJsFile : [tempJsFile]
|
|
838
|
-
|
|
839
|
-
try {
|
|
840
|
-
if (fs.existsSync(file)) {
|
|
841
|
-
fs.unlinkSync(file)
|
|
842
|
-
}
|
|
843
|
-
} catch (cleanupError) {
|
|
844
|
-
// Ignore cleanup errors
|
|
845
|
-
}
|
|
846
|
-
}
|
|
721
|
+
cleanupTempFiles(filesToClean)
|
|
847
722
|
}
|
|
848
723
|
}
|
|
849
724
|
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import fs from 'fs'
|
|
2
|
+
import path from 'path'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Transpile TypeScript files to ES modules with CommonJS shim support
|
|
6
|
+
* Handles recursive transpilation of imported TypeScript files
|
|
7
|
+
*
|
|
8
|
+
* @param {string} mainFilePath - Path to the main TypeScript file to transpile
|
|
9
|
+
* @param {object} typescript - TypeScript compiler instance
|
|
10
|
+
* @returns {Promise<{tempFile: string, allTempFiles: string[]}>} - Main temp file and all temp files created
|
|
11
|
+
*/
|
|
12
|
+
export async function transpileTypeScript(mainFilePath, typescript) {
|
|
13
|
+
const { transpile } = typescript
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Transpile a single TypeScript file to JavaScript
|
|
17
|
+
* Injects CommonJS shims (require, module, exports, __dirname, __filename) as needed
|
|
18
|
+
*/
|
|
19
|
+
const transpileTS = (filePath) => {
|
|
20
|
+
const tsContent = fs.readFileSync(filePath, 'utf8')
|
|
21
|
+
|
|
22
|
+
// Transpile TypeScript to JavaScript with ES module output
|
|
23
|
+
let jsContent = transpile(tsContent, {
|
|
24
|
+
module: 99, // ModuleKind.ESNext
|
|
25
|
+
target: 99, // ScriptTarget.ESNext
|
|
26
|
+
esModuleInterop: true,
|
|
27
|
+
allowSyntheticDefaultImports: true,
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
// Check if the code uses CommonJS globals
|
|
31
|
+
const usesCommonJSGlobals = /__dirname|__filename/.test(jsContent)
|
|
32
|
+
const usesRequire = /\brequire\s*\(/.test(jsContent)
|
|
33
|
+
const usesModuleExports = /\b(module\.exports|exports\.)/.test(jsContent)
|
|
34
|
+
|
|
35
|
+
if (usesCommonJSGlobals || usesRequire || usesModuleExports) {
|
|
36
|
+
// Inject ESM equivalents at the top of the file
|
|
37
|
+
let esmGlobals = ''
|
|
38
|
+
|
|
39
|
+
if (usesRequire || usesModuleExports) {
|
|
40
|
+
// IMPORTANT: Use the original .ts file path as the base for require()
|
|
41
|
+
// This ensures dynamic require() calls work with relative paths from the original file location
|
|
42
|
+
const originalFileUrl = `file://${filePath.replace(/\\/g, '/')}`
|
|
43
|
+
esmGlobals += `import { createRequire } from 'module';
|
|
44
|
+
const require = createRequire('${originalFileUrl}');
|
|
45
|
+
const module = { exports: {} };
|
|
46
|
+
const exports = module.exports;
|
|
47
|
+
|
|
48
|
+
`
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (usesCommonJSGlobals) {
|
|
52
|
+
// For __dirname and __filename, also use the original file path
|
|
53
|
+
const originalFileUrl = `file://${filePath.replace(/\\/g, '/')}`
|
|
54
|
+
esmGlobals += `import { fileURLToPath as __fileURLToPath } from 'url';
|
|
55
|
+
import { dirname as __dirname_fn } from 'path';
|
|
56
|
+
const __filename = '${filePath.replace(/\\/g, '/')}';
|
|
57
|
+
const __dirname = __dirname_fn(__filename);
|
|
58
|
+
|
|
59
|
+
`
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
jsContent = esmGlobals + jsContent
|
|
63
|
+
|
|
64
|
+
// If module.exports is used, we need to export it as default
|
|
65
|
+
if (usesModuleExports) {
|
|
66
|
+
jsContent += `\nexport default module.exports;\n`
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return jsContent
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Create a map to track transpiled files
|
|
74
|
+
const transpiledFiles = new Map()
|
|
75
|
+
const baseDir = path.dirname(mainFilePath)
|
|
76
|
+
|
|
77
|
+
// Transpile main file
|
|
78
|
+
let jsContent = transpileTS(mainFilePath)
|
|
79
|
+
|
|
80
|
+
// Find and transpile all relative TypeScript imports
|
|
81
|
+
// Match: import ... from './file' or '../file' or './file.ts'
|
|
82
|
+
const importRegex = /from\s+['"](\..+?)(?:\.ts)?['"]/g
|
|
83
|
+
let match
|
|
84
|
+
const imports = []
|
|
85
|
+
|
|
86
|
+
while ((match = importRegex.exec(jsContent)) !== null) {
|
|
87
|
+
imports.push(match[1])
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Transpile each imported TypeScript file
|
|
91
|
+
for (const relativeImport of imports) {
|
|
92
|
+
let importedPath = path.resolve(baseDir, relativeImport)
|
|
93
|
+
|
|
94
|
+
// Handle .js extensions that might actually be .ts files
|
|
95
|
+
if (importedPath.endsWith('.js')) {
|
|
96
|
+
const tsVersion = importedPath.replace(/\.js$/, '.ts')
|
|
97
|
+
if (fs.existsSync(tsVersion)) {
|
|
98
|
+
importedPath = tsVersion
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Try adding .ts extension if file doesn't exist and no extension provided
|
|
103
|
+
if (!path.extname(importedPath)) {
|
|
104
|
+
const tsPath = importedPath + '.ts'
|
|
105
|
+
if (fs.existsSync(tsPath)) {
|
|
106
|
+
importedPath = tsPath
|
|
107
|
+
} else {
|
|
108
|
+
// Try .js extension as well
|
|
109
|
+
const jsPath = importedPath + '.js'
|
|
110
|
+
if (fs.existsSync(jsPath)) {
|
|
111
|
+
// Skip .js files, they don't need transpilation
|
|
112
|
+
continue
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// If it's a TypeScript file, transpile it
|
|
118
|
+
if (importedPath.endsWith('.ts') && fs.existsSync(importedPath)) {
|
|
119
|
+
const transpiledImportContent = transpileTS(importedPath)
|
|
120
|
+
const tempImportFile = importedPath.replace(/\.ts$/, '.temp.mjs')
|
|
121
|
+
fs.writeFileSync(tempImportFile, transpiledImportContent)
|
|
122
|
+
transpiledFiles.set(importedPath, tempImportFile)
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Replace imports in the main file to point to temp .mjs files
|
|
127
|
+
jsContent = jsContent.replace(
|
|
128
|
+
/from\s+['"](\..+?)(?:\.ts)?['"]/g,
|
|
129
|
+
(match, importPath) => {
|
|
130
|
+
let resolvedPath = path.resolve(baseDir, importPath)
|
|
131
|
+
|
|
132
|
+
// Handle .js extension that might be .ts
|
|
133
|
+
if (resolvedPath.endsWith('.js')) {
|
|
134
|
+
const tsVersion = resolvedPath.replace(/\.js$/, '.ts')
|
|
135
|
+
if (transpiledFiles.has(tsVersion)) {
|
|
136
|
+
const tempFile = transpiledFiles.get(tsVersion)
|
|
137
|
+
const relPath = path.relative(baseDir, tempFile).replace(/\\/g, '/')
|
|
138
|
+
return `from './${relPath}'`
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Try with .ts extension
|
|
143
|
+
const tsPath = resolvedPath.endsWith('.ts') ? resolvedPath : resolvedPath + '.ts'
|
|
144
|
+
|
|
145
|
+
// If we transpiled this file, use the temp file
|
|
146
|
+
if (transpiledFiles.has(tsPath)) {
|
|
147
|
+
const tempFile = transpiledFiles.get(tsPath)
|
|
148
|
+
// Get relative path from main temp file to this temp file
|
|
149
|
+
const relPath = path.relative(baseDir, tempFile).replace(/\\/g, '/')
|
|
150
|
+
// Ensure the path starts with ./
|
|
151
|
+
if (!relPath.startsWith('.')) {
|
|
152
|
+
return `from './${relPath}'`
|
|
153
|
+
}
|
|
154
|
+
return `from '${relPath}'`
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Otherwise, keep the import as-is
|
|
158
|
+
return match
|
|
159
|
+
}
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
// Create a temporary JS file with .mjs extension for the main file
|
|
163
|
+
const tempJsFile = mainFilePath.replace(/\.ts$/, '.temp.mjs')
|
|
164
|
+
fs.writeFileSync(tempJsFile, jsContent)
|
|
165
|
+
|
|
166
|
+
// Store all temp files for cleanup
|
|
167
|
+
const allTempFiles = [tempJsFile, ...Array.from(transpiledFiles.values())]
|
|
168
|
+
|
|
169
|
+
return { tempFile: tempJsFile, allTempFiles }
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Clean up temporary transpiled files
|
|
174
|
+
* @param {string[]} tempFiles - Array of temp file paths to delete
|
|
175
|
+
*/
|
|
176
|
+
export function cleanupTempFiles(tempFiles) {
|
|
177
|
+
for (const file of tempFiles) {
|
|
178
|
+
if (fs.existsSync(file)) {
|
|
179
|
+
try {
|
|
180
|
+
fs.unlinkSync(file)
|
|
181
|
+
} catch (err) {
|
|
182
|
+
// Ignore cleanup errors
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
package/package.json
CHANGED
|
@@ -2742,6 +2742,7 @@ declare namespace CodeceptJS {
|
|
|
2742
2742
|
* `grabStorageState({ indexedDB: true })`) IndexedDB data; treat as sensitive and do not commit.
|
|
2743
2743
|
*/
|
|
2744
2744
|
// @ts-ignore
|
|
2745
|
+
// @ts-ignore
|
|
2745
2746
|
type PlaywrightConfig = {
|
|
2746
2747
|
url?: string;
|
|
2747
2748
|
browser?: 'chromium' | 'firefox' | 'webkit' | 'electron';
|
|
@@ -6142,6 +6143,7 @@ declare namespace CodeceptJS {
|
|
|
6142
6143
|
* @property [highlightElement] - highlight the interacting elements. Default: false. Note: only activate under verbose mode (--verbose).
|
|
6143
6144
|
*/
|
|
6144
6145
|
// @ts-ignore
|
|
6146
|
+
// @ts-ignore
|
|
6145
6147
|
type PuppeteerConfig = {
|
|
6146
6148
|
url: string;
|
|
6147
6149
|
basicAuth?: any;
|
|
@@ -7987,6 +7989,7 @@ declare namespace CodeceptJS {
|
|
|
7987
7989
|
* @property [maxUploadFileSize] - set the max content file size in MB when performing api calls.
|
|
7988
7990
|
*/
|
|
7989
7991
|
// @ts-ignore
|
|
7992
|
+
// @ts-ignore
|
|
7990
7993
|
type RESTConfig = {
|
|
7991
7994
|
endpoint?: string;
|
|
7992
7995
|
prettyPrintJson?: boolean;
|
|
@@ -9143,6 +9146,7 @@ declare namespace CodeceptJS {
|
|
|
9143
9146
|
* @property [logLevel = silent] - level of logging verbosity. Default: silent. Options: trace | debug | info | warn | error | silent. More info: https://webdriver.io/docs/configuration/#loglevel
|
|
9144
9147
|
*/
|
|
9145
9148
|
// @ts-ignore
|
|
9149
|
+
// @ts-ignore
|
|
9146
9150
|
type WebDriverConfig = {
|
|
9147
9151
|
url: string;
|
|
9148
9152
|
browser: string;
|
package/typings/types.d.ts
CHANGED
|
@@ -2832,6 +2832,7 @@ declare namespace CodeceptJS {
|
|
|
2832
2832
|
* `grabStorageState({ indexedDB: true })`) IndexedDB data; treat as sensitive and do not commit.
|
|
2833
2833
|
*/
|
|
2834
2834
|
// @ts-ignore
|
|
2835
|
+
// @ts-ignore
|
|
2835
2836
|
type PlaywrightConfig = {
|
|
2836
2837
|
url?: string;
|
|
2837
2838
|
browser?: 'chromium' | 'firefox' | 'webkit' | 'electron';
|
|
@@ -6383,6 +6384,7 @@ declare namespace CodeceptJS {
|
|
|
6383
6384
|
* @property [highlightElement] - highlight the interacting elements. Default: false. Note: only activate under verbose mode (--verbose).
|
|
6384
6385
|
*/
|
|
6385
6386
|
// @ts-ignore
|
|
6387
|
+
// @ts-ignore
|
|
6386
6388
|
type PuppeteerConfig = {
|
|
6387
6389
|
url: string;
|
|
6388
6390
|
basicAuth?: any;
|
|
@@ -8364,6 +8366,7 @@ declare namespace CodeceptJS {
|
|
|
8364
8366
|
* @property [maxUploadFileSize] - set the max content file size in MB when performing api calls.
|
|
8365
8367
|
*/
|
|
8366
8368
|
// @ts-ignore
|
|
8369
|
+
// @ts-ignore
|
|
8367
8370
|
type RESTConfig = {
|
|
8368
8371
|
endpoint?: string;
|
|
8369
8372
|
prettyPrintJson?: boolean;
|
|
@@ -9580,6 +9583,7 @@ declare namespace CodeceptJS {
|
|
|
9580
9583
|
* @property [logLevel = silent] - level of logging verbosity. Default: silent. Options: trace | debug | info | warn | error | silent. More info: https://webdriver.io/docs/configuration/#loglevel
|
|
9581
9584
|
*/
|
|
9582
9585
|
// @ts-ignore
|
|
9586
|
+
// @ts-ignore
|
|
9583
9587
|
type WebDriverConfig = {
|
|
9584
9588
|
url: string;
|
|
9585
9589
|
browser: string;
|