@@ -48,8 +48,11 @@ function cleanupDeclaration(filePath) {
4848 return
4949 }
5050
51- // Simple cleanup - just ensure proper formatting
52- // Don't wrap in namespace here, we'll create a consolidated types.d.ts instead
51+ // Remove problematic export statements that cause module format errors
52+ content = content . replace ( / ^ e x p o r t \s * = \s * .+ ; ? \s * $ / gm, '' )
53+
54+ // Remove empty lines at the start
55+ content = content . replace ( / ^ \s * \n / gm, '' )
5356
5457 writeFileSync ( filePath , content )
5558}
@@ -188,6 +191,71 @@ function main() {
188191 cleanupDeclaration ( file )
189192 }
190193
194+ // Step 3: Create consolidated types.d.ts file in CodeceptJS namespace
195+ console . log ( '\nCreating consolidated types file...' )
196+ const outputFilename = isPromiseBased ? 'promiseBasedTypes.d.ts' : 'types.d.ts'
197+ const outputPath = join ( typingsDir , outputFilename )
198+
199+ const consolidated = [ ]
200+ consolidated . push ( '// Auto-generated TypeScript definitions' )
201+ consolidated . push ( '// Generated from JSDoc comments using TypeScript compiler' )
202+ consolidated . push ( '' )
203+ consolidated . push ( 'declare namespace CodeceptJS {' )
204+
205+ // Collect all class, interface, and type definitions from helper and lib files
206+ const helperFiles = dtsFiles . filter ( f => f . includes ( '/docs/build/' ) )
207+ const libFiles = dtsFiles . filter (
208+ f =>
209+ f . includes ( '/lib/' ) &&
210+ ! f . includes ( '/lib/command/' ) &&
211+ ! f . includes ( '/lib/listener/' ) &&
212+ ! f . includes ( '/lib/assert/' ) &&
213+ ! f . includes ( '/lib/data/' ) &&
214+ ! f . includes ( '/lib/plugin/' ) &&
215+ ! f . includes ( '/lib/template/' ) &&
216+ ! f . includes ( '/lib/utils/' ) ,
217+ )
218+
219+ // For promise-based types, only include helpers. For regular types, include both.
220+ const allFiles = isPromiseBased ? helperFiles : [ ...libFiles , ...helperFiles ]
221+
222+ for ( const file of allFiles ) {
223+ if ( ! existsSync ( file ) ) continue
224+
225+ let content = readFileSync ( file , 'utf-8' )
226+
227+ // Remove all import statements
228+ content = content . replace ( / ^ i m p o r t .+ $ / gm, '' )
229+
230+ // Remove export default and export = statements
231+ content = content . replace ( / ^ e x p o r t d e f a u l t .+ $ / gm, '' )
232+ content = content . replace ( / ^ e x p o r t = .+ $ / gm, '' )
233+ content = content . replace ( / ^ e x p o r t \{ \} ; ? $ / gm, '' )
234+
235+ // Remove export keywords but keep declarations
236+ content = content . replace ( / ^ e x p o r t ( d e c l a r e ) ? / gm, '' )
237+ content = content . replace ( / ^ d e c l a r e / gm, '' )
238+
239+ // Keep CodeceptJS. prefix - it's needed for cross-references within the namespace
240+
241+ // Split into lines and indent
242+ const lines = content . split ( '\n' )
243+ for ( const line of lines ) {
244+ if ( line . trim ( ) ) {
245+ consolidated . push ( ' ' + line )
246+ } else if ( consolidated [ consolidated . length - 1 ] !== '' ) {
247+ // Only add empty line if previous line wasn't empty
248+ consolidated . push ( '' )
249+ }
250+ }
251+ }
252+
253+ consolidated . push ( '}' )
254+ consolidated . push ( '' )
255+
256+ writeFileSync ( outputPath , consolidated . join ( '\n' ) )
257+ console . log ( `Created ${ outputFilename } with ${ allFiles . length } type definitions (${ helperFiles . length } helpers, ${ libFiles . length } lib)` )
258+
191259 console . log ( '\nType definitions generated successfully!' )
192260 console . log ( 'Generated .d.ts files are in typings/ directory (excluded from git)' )
193261}
0 commit comments