1+
2+
3+ import * as fs from 'fs' ;
4+ import * as path from 'path' ;
5+
6+ const SRC_DIR = path . join ( __dirname , 'src' ) ;
7+ const OUTPUT_FILE = path . join ( __dirname , 'test' , 'mongodb.ts' ) ;
8+
9+ /**
10+ * Recursively get all TypeScript files in a directory
11+ */
12+ function getTsFiles ( dir : string , fileList : string [ ] = [ ] ) : string [ ] {
13+ const files = fs . readdirSync ( dir ) ;
14+
15+ for ( const file of files ) {
16+ const filePath = path . join ( dir , file ) ;
17+ const stat = fs . statSync ( filePath ) ;
18+
19+ if ( stat . isDirectory ( ) ) {
20+ getTsFiles ( filePath , fileList ) ;
21+ } else if ( file . endsWith ( '.ts' ) && ! file . endsWith ( '.d.ts' ) ) {
22+ fileList . push ( filePath ) ;
23+ }
24+ }
25+
26+ return fileList ;
27+ }
28+
29+ /**
30+ * Convert absolute path to relative import path
31+ */
32+ function toImportPath ( filePath : string ) : string {
33+ // Get relative path from output file to source file
34+ const relativePath = path . relative ( path . dirname ( OUTPUT_FILE ) , filePath ) ;
35+ // Remove .ts extension and normalize path separators
36+ const importPath = relativePath . replace ( / \. t s $ / , '' ) . replace ( / \\ / g, '/' ) ;
37+ // Ensure it starts with ./
38+ return importPath . startsWith ( '.' ) ? importPath : `../src/${ importPath } ` ;
39+ }
40+
41+ /**
42+ * Generate the mongodb.ts file with all exports
43+ */
44+ function generateExportFile ( ) : void {
45+ const tsFiles = getTsFiles ( SRC_DIR ) ;
46+
47+ // Sort files for consistent output
48+ tsFiles . sort ( ) ;
49+
50+ const exports : string [ ] = [
51+ '/**' ,
52+ ' * Auto-generated file that exports everything from src/' ,
53+ ' * Generated on: ' + new Date ( ) . toISOString ( ) ,
54+ ' */' ,
55+ ''
56+ ] ;
57+
58+ for ( const file of tsFiles ) {
59+ const importPath = toImportPath ( file ) ;
60+ exports . push ( `export * from '${ importPath } ';` ) ;
61+ }
62+
63+ const content = exports . join ( '\n' ) + '\n' ;
64+
65+ fs . writeFileSync ( OUTPUT_FILE , content , 'utf-8' ) ;
66+
67+ console . log ( `✓ Generated ${ OUTPUT_FILE } ` ) ;
68+ console . log ( `✓ Exported ${ tsFiles . length } files` ) ;
69+ }
70+
71+ // Run the generator
72+ try {
73+ generateExportFile ( ) ;
74+ } catch ( error ) {
75+ console . error ( 'Error generating export file:' , error ) ;
76+ process . exit ( 1 ) ;
77+ }
0 commit comments