codeceptjs 4.0.0-beta.15 → 4.0.0-beta.16
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/utils/typescript.js +94 -77
- package/package.json +1 -1
package/lib/utils/typescript.js
CHANGED
|
@@ -74,97 +74,114 @@ const __dirname = __dirname_fn(__filename);
|
|
|
74
74
|
const transpiledFiles = new Map()
|
|
75
75
|
const baseDir = path.dirname(mainFilePath)
|
|
76
76
|
|
|
77
|
-
//
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
-
}
|
|
77
|
+
// Recursive function to transpile a file and all its TypeScript dependencies
|
|
78
|
+
const transpileFileAndDeps = (filePath) => {
|
|
79
|
+
// Already transpiled, skip
|
|
80
|
+
if (transpiledFiles.has(filePath)) {
|
|
81
|
+
return
|
|
100
82
|
}
|
|
101
83
|
|
|
102
|
-
//
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
const jsPath = importedPath + '.js'
|
|
110
|
-
if (fs.existsSync(jsPath)) {
|
|
111
|
-
// Skip .js files, they don't need transpilation
|
|
112
|
-
continue
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
84
|
+
// Transpile this file
|
|
85
|
+
let jsContent = transpileTS(filePath)
|
|
86
|
+
|
|
87
|
+
// Find all relative TypeScript imports in this file
|
|
88
|
+
const importRegex = /from\s+['"](\..+?)(?:\.ts)?['"]/g
|
|
89
|
+
let match
|
|
90
|
+
const imports = []
|
|
116
91
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
const transpiledImportContent = transpileTS(importedPath)
|
|
120
|
-
const tempImportFile = importedPath.replace(/\.ts$/, '.temp.mjs')
|
|
121
|
-
fs.writeFileSync(tempImportFile, transpiledImportContent)
|
|
122
|
-
transpiledFiles.set(importedPath, tempImportFile)
|
|
92
|
+
while ((match = importRegex.exec(jsContent)) !== null) {
|
|
93
|
+
imports.push(match[1])
|
|
123
94
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
(
|
|
130
|
-
let
|
|
95
|
+
|
|
96
|
+
// Get the base directory for this file
|
|
97
|
+
const fileBaseDir = path.dirname(filePath)
|
|
98
|
+
|
|
99
|
+
// Recursively transpile each imported TypeScript file
|
|
100
|
+
for (const relativeImport of imports) {
|
|
101
|
+
let importedPath = path.resolve(fileBaseDir, relativeImport)
|
|
131
102
|
|
|
132
|
-
// Handle .js
|
|
133
|
-
if (
|
|
134
|
-
const tsVersion =
|
|
135
|
-
if (
|
|
136
|
-
|
|
137
|
-
const relPath = path.relative(baseDir, tempFile).replace(/\\/g, '/')
|
|
138
|
-
return `from './${relPath}'`
|
|
103
|
+
// Handle .js extensions that might actually be .ts files
|
|
104
|
+
if (importedPath.endsWith('.js')) {
|
|
105
|
+
const tsVersion = importedPath.replace(/\.js$/, '.ts')
|
|
106
|
+
if (fs.existsSync(tsVersion)) {
|
|
107
|
+
importedPath = tsVersion
|
|
139
108
|
}
|
|
140
109
|
}
|
|
141
110
|
|
|
142
|
-
// Try
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
111
|
+
// Try adding .ts extension if file doesn't exist and no extension provided
|
|
112
|
+
if (!path.extname(importedPath)) {
|
|
113
|
+
const tsPath = importedPath + '.ts'
|
|
114
|
+
if (fs.existsSync(tsPath)) {
|
|
115
|
+
importedPath = tsPath
|
|
116
|
+
} else {
|
|
117
|
+
// Try .js extension as well
|
|
118
|
+
const jsPath = importedPath + '.js'
|
|
119
|
+
if (fs.existsSync(jsPath)) {
|
|
120
|
+
// Skip .js files, they don't need transpilation
|
|
121
|
+
continue
|
|
122
|
+
}
|
|
153
123
|
}
|
|
154
|
-
return `from '${relPath}'`
|
|
155
124
|
}
|
|
156
125
|
|
|
157
|
-
//
|
|
158
|
-
|
|
126
|
+
// If it's a TypeScript file, recursively transpile it and its dependencies
|
|
127
|
+
if (importedPath.endsWith('.ts') && fs.existsSync(importedPath)) {
|
|
128
|
+
transpileFileAndDeps(importedPath)
|
|
129
|
+
}
|
|
159
130
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
131
|
+
|
|
132
|
+
// After all dependencies are transpiled, rewrite imports in this file
|
|
133
|
+
jsContent = jsContent.replace(
|
|
134
|
+
/from\s+['"](\..+?)(?:\.ts)?['"]/g,
|
|
135
|
+
(match, importPath) => {
|
|
136
|
+
let resolvedPath = path.resolve(fileBaseDir, importPath)
|
|
137
|
+
|
|
138
|
+
// Handle .js extension that might be .ts
|
|
139
|
+
if (resolvedPath.endsWith('.js')) {
|
|
140
|
+
const tsVersion = resolvedPath.replace(/\.js$/, '.ts')
|
|
141
|
+
if (transpiledFiles.has(tsVersion)) {
|
|
142
|
+
const tempFile = transpiledFiles.get(tsVersion)
|
|
143
|
+
const relPath = path.relative(fileBaseDir, tempFile).replace(/\\/g, '/')
|
|
144
|
+
// Ensure the path starts with ./
|
|
145
|
+
if (!relPath.startsWith('.')) {
|
|
146
|
+
return `from './${relPath}'`
|
|
147
|
+
}
|
|
148
|
+
return `from '${relPath}'`
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Try with .ts extension
|
|
153
|
+
const tsPath = resolvedPath.endsWith('.ts') ? resolvedPath : resolvedPath + '.ts'
|
|
154
|
+
|
|
155
|
+
// If we transpiled this file, use the temp file
|
|
156
|
+
if (transpiledFiles.has(tsPath)) {
|
|
157
|
+
const tempFile = transpiledFiles.get(tsPath)
|
|
158
|
+
const relPath = path.relative(fileBaseDir, tempFile).replace(/\\/g, '/')
|
|
159
|
+
// Ensure the path starts with ./
|
|
160
|
+
if (!relPath.startsWith('.')) {
|
|
161
|
+
return `from './${relPath}'`
|
|
162
|
+
}
|
|
163
|
+
return `from '${relPath}'`
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Otherwise, keep the import as-is
|
|
167
|
+
return match
|
|
168
|
+
}
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
// Write the transpiled file with updated imports
|
|
172
|
+
const tempFile = filePath.replace(/\.ts$/, '.temp.mjs')
|
|
173
|
+
fs.writeFileSync(tempFile, jsContent)
|
|
174
|
+
transpiledFiles.set(filePath, tempFile)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Start recursive transpilation from the main file
|
|
178
|
+
transpileFileAndDeps(mainFilePath)
|
|
179
|
+
|
|
180
|
+
// Get the main transpiled file
|
|
181
|
+
const tempJsFile = transpiledFiles.get(mainFilePath)
|
|
165
182
|
|
|
166
183
|
// Store all temp files for cleanup
|
|
167
|
-
const allTempFiles =
|
|
184
|
+
const allTempFiles = Array.from(transpiledFiles.values())
|
|
168
185
|
|
|
169
186
|
return { tempFile: tempJsFile, allTempFiles }
|
|
170
187
|
}
|