|
1 | | -#!/usr/bin/env node |
2 | | - |
3 | | -const fs = require('fs'); |
4 | | -const path = require('path'); |
5 | | - |
6 | | -const typesFile = path.join(__dirname, '../dist/index.d.ts'); |
7 | | -let content = fs.readFileSync(typesFile, 'utf8'); |
8 | | - |
9 | | -// eslint-disable-next-line no-console |
10 | | -console.log('Starting comprehensive cleanup of bundled types...'); |
11 | | - |
12 | | -// Count initial problematic imports |
13 | | -const initialWhatwgMatches = (content.match(/whatwg-url/g) || []).length; |
14 | | -const initialEventEmitterMatches = (content.match(/eventemitter3/g) || []) |
15 | | - .length; |
16 | | -const initialRelativeMatches = (content.match(/^import.*from.*['"]\./gm) || []) |
17 | | - .length; |
18 | | -const initialMongoJsMatches = (content.match(/from\s+['"]@mongodb-js\//g) || []) |
19 | | - .length; |
20 | | - |
21 | | -// eslint-disable-next-line no-console |
22 | | -console.log( |
23 | | - `Initial counts - whatwg-url: ${initialWhatwgMatches}, eventemitter3: ${initialEventEmitterMatches}, relative imports: ${initialRelativeMatches}, @mongodb-js imports: ${initialMongoJsMatches}` |
24 | | -); |
25 | | - |
26 | | -// 1. Remove whatwg-url imports and replace references |
27 | | -content = content.replace( |
28 | | - /import\s*\{\s*URL\s*as\s*URL_2\s*\}\s*from\s*['"]whatwg-url['"];\s*\n?/g, |
29 | | - '' |
30 | | -); |
31 | | -content = content.replace( |
32 | | - /import\s*\{\s*URL\s*\}\s*from\s*['"]whatwg-url['"];\s*\n?/g, |
33 | | - '' |
34 | | -); |
35 | | -content = content.replace(/\bURL_2\b/g, 'URL'); |
36 | | - |
37 | | -// 2. Remove eventemitter3 imports and replace with Node.js EventEmitter |
38 | | -content = content.replace( |
39 | | - /import\s*\{\s*default\s+as\s+EventEmitter_2\s*\}\s*from\s*['"]eventemitter3['"];\s*\n?/g, |
40 | | - '' |
41 | | -); |
42 | | -content = content.replace( |
43 | | - /import\s*\{\s*EventEmitter\s*\}\s*from\s*['"]eventemitter3['"];\s*\n?/g, |
44 | | - '' |
45 | | -); |
46 | | -content = content.replace( |
47 | | - /import\s*EventEmitter\s*from\s*['"]eventemitter3['"];\s*\n?/g, |
48 | | - '' |
49 | | -); |
50 | | -content = content.replace(/\bEventEmitter_2\b/g, 'NodeJS.EventEmitter'); |
51 | | - |
52 | | -// 3. Fix duplicate type references (remove _2, _3, etc. suffixes) |
53 | | -content = content.replace(/\b([A-Za-z_][A-Za-z0-9_]*?)_2\b/g, '$1'); |
54 | | -content = content.replace(/\b([A-Za-z_][A-Za-z0-9_]*?)_3\b/g, '$1'); |
55 | | -content = content.replace(/\b([A-Za-z_][A-Za-z0-9_]*?)_4\b/g, '$1'); |
56 | | - |
57 | | -// 4. Remove relative imports that API Extractor couldn't resolve |
58 | | -content = content.replace(/^import.*from\s*['"][./].*['"];\s*\n?/gm, ''); |
59 | | - |
60 | | -// 5. Remove remaining @mongodb-js imports that weren't bundled (deep path imports) |
61 | | -content = content.replace( |
62 | | - /^import\s+\{[^}]+\}\s+from\s+['"]@mongodb-js\/[^'"]+['"];\s*\n?/gm, |
63 | | - '' |
64 | | -); |
65 | | -content = content.replace( |
66 | | - /^import\s+[^{][^;]+from\s+['"]@mongodb-js\/[^'"]+['"];\s*\n?/gm, |
67 | | - '' |
68 | | -); |
69 | | - |
70 | | -// 6. Fix ambient context issues by removing statements in declare blocks |
71 | | -content = content.replace( |
72 | | - /declare\s+namespace\s+[^{]+\s*\{[^}]*\bexport\s*\{[^}]*\}\s*;?\s*([^}]*)\}/g, |
73 | | - (match) => { |
74 | | - // Remove export statements from within declare namespace blocks |
75 | | - return match.replace(/export\s*\{[^}]*\}\s*;?\s*/g, ''); |
76 | | - } |
77 | | -); |
78 | | - |
79 | | -// 7. Fix event emitter method return types |
80 | | -content = content.replace( |
81 | | - /(\s+)(on|off|removeListener)\([^)]*\):\s*any;/g, |
82 | | - '$1$2(...args: any[]): this;' |
83 | | -); |
84 | | - |
85 | | -// 8. Remove problematic type assertions and fix generic types |
86 | | -content = content.replace(/: AppRegistry<[^>]*>/g, ': AppRegistry'); |
87 | | - |
88 | | -// 9. Fix module resolution issues for mongodb-log-writer |
89 | | -content = content.replace( |
90 | | - /from\s*['"]mongodb-log-writer\/mongo-log-writer['"]/g, |
91 | | - "from 'mongodb-log-writer'" |
92 | | -); |
93 | | - |
94 | | -// 10. Remove or fix value/type confusion |
95 | | -content = content.replace( |
96 | | - /(\s+)([a-zA-Z_][a-zA-Z0-9_]*)\s+refers\s+to\s+a\s+value.*?Did you mean typeof.*?\n/g, |
97 | | - '' |
98 | | -); |
99 | | - |
100 | | -// 11. Add necessary imports at the top if we reference Node.js types |
101 | | -if ( |
102 | | - content.includes('NodeJS.EventEmitter') && |
103 | | - !content.includes('/// <reference types="node" />') |
104 | | -) { |
105 | | - content = '/// <reference types="node" />\n' + content; |
106 | | -} |
107 | | - |
108 | | -// Count final problematic imports |
109 | | -const finalWhatwgMatches = (content.match(/whatwg-url/g) || []).length; |
110 | | -const finalEventEmitterMatches = (content.match(/eventemitter3/g) || []).length; |
111 | | -const finalRelativeMatches = (content.match(/^import.*from.*['"]\./gm) || []) |
112 | | - .length; |
113 | | -const finalMongoJsMatches = (content.match(/from\s+['"]@mongodb-js\//g) || []) |
114 | | - .length; |
115 | | - |
116 | | -// eslint-disable-next-line no-console |
117 | | -console.log( |
118 | | - `Final counts - whatwg-url: ${finalWhatwgMatches}, eventemitter3: ${finalEventEmitterMatches}, relative imports: ${finalRelativeMatches}, @mongodb-js imports: ${finalMongoJsMatches}` |
119 | | -); |
120 | | - |
121 | | -fs.writeFileSync(typesFile, content); |
122 | | -// eslint-disable-next-line no-console |
123 | | -console.log('Successfully cleaned up bundled types file'); |
| 1 | +// #!/usr/bin/env node |
| 2 | + |
| 3 | +// const fs = require('fs'); |
| 4 | +// const path = require('path'); |
| 5 | + |
| 6 | +// const typesFile = path.join(__dirname, '../dist/index.d.ts'); |
| 7 | +// let content = fs.readFileSync(typesFile, 'utf8'); |
| 8 | + |
| 9 | +// // eslint-disable-next-line no-console |
| 10 | +// console.log('Starting comprehensive cleanup of bundled types...'); |
| 11 | + |
| 12 | +// // Count initial problematic imports |
| 13 | +// const initialWhatwgMatches = (content.match(/whatwg-url/g) || []).length; |
| 14 | +// const initialEventEmitterMatches = (content.match(/eventemitter3/g) || []) |
| 15 | +// .length; |
| 16 | +// const initialRelativeMatches = (content.match(/^import.*from.*['"]\./gm) || []) |
| 17 | +// .length; |
| 18 | +// const initialMongoJsMatches = (content.match(/from\s+['"]@mongodb-js\//g) || []) |
| 19 | +// .length; |
| 20 | + |
| 21 | +// // eslint-disable-next-line no-console |
| 22 | +// console.log( |
| 23 | +// `Initial counts - whatwg-url: ${initialWhatwgMatches}, eventemitter3: ${initialEventEmitterMatches}, relative imports: ${initialRelativeMatches}, @mongodb-js imports: ${initialMongoJsMatches}` |
| 24 | +// ); |
| 25 | + |
| 26 | +// // 1. Remove whatwg-url imports and replace references |
| 27 | +// content = content.replace( |
| 28 | +// /import\s*\{\s*URL\s*as\s*URL_2\s*\}\s*from\s*['"]whatwg-url['"];\s*\n?/g, |
| 29 | +// '' |
| 30 | +// ); |
| 31 | +// content = content.replace( |
| 32 | +// /import\s*\{\s*URL\s*\}\s*from\s*['"]whatwg-url['"];\s*\n?/g, |
| 33 | +// '' |
| 34 | +// ); |
| 35 | +// content = content.replace(/\bURL_2\b/g, 'URL'); |
| 36 | + |
| 37 | +// // 2. Remove eventemitter3 imports and replace with Node.js EventEmitter |
| 38 | +// content = content.replace( |
| 39 | +// /import\s*\{\s*default\s+as\s+EventEmitter_2\s*\}\s*from\s*['"]eventemitter3['"];\s*\n?/g, |
| 40 | +// '' |
| 41 | +// ); |
| 42 | +// content = content.replace( |
| 43 | +// /import\s*\{\s*EventEmitter\s*\}\s*from\s*['"]eventemitter3['"];\s*\n?/g, |
| 44 | +// '' |
| 45 | +// ); |
| 46 | +// content = content.replace( |
| 47 | +// /import\s*EventEmitter\s*from\s*['"]eventemitter3['"];\s*\n?/g, |
| 48 | +// '' |
| 49 | +// ); |
| 50 | +// content = content.replace(/\bEventEmitter_2\b/g, 'NodeJS.EventEmitter'); |
| 51 | + |
| 52 | +// // 3. Fix duplicate type references (remove _2, _3, etc. suffixes) |
| 53 | +// content = content.replace(/\b([A-Za-z_][A-Za-z0-9_]*?)_2\b/g, '$1'); |
| 54 | +// content = content.replace(/\b([A-Za-z_][A-Za-z0-9_]*?)_3\b/g, '$1'); |
| 55 | +// content = content.replace(/\b([A-Za-z_][A-Za-z0-9_]*?)_4\b/g, '$1'); |
| 56 | + |
| 57 | +// // 4. Remove relative imports that API Extractor couldn't resolve |
| 58 | +// content = content.replace(/^import.*from\s*['"][./].*['"];\s*\n?/gm, ''); |
| 59 | + |
| 60 | +// // 5. Remove remaining @mongodb-js imports that weren't bundled (deep path imports) |
| 61 | +// content = content.replace( |
| 62 | +// /^import\s+\{[^}]+\}\s+from\s+['"]@mongodb-js\/[^'"]+['"];\s*\n?/gm, |
| 63 | +// '' |
| 64 | +// ); |
| 65 | +// content = content.replace( |
| 66 | +// /^import\s+[^{][^;]+from\s+['"]@mongodb-js\/[^'"]+['"];\s*\n?/gm, |
| 67 | +// '' |
| 68 | +// ); |
| 69 | + |
| 70 | +// // 6. Fix ambient context issues by removing statements in declare blocks |
| 71 | +// content = content.replace( |
| 72 | +// /declare\s+namespace\s+[^{]+\s*\{[^}]*\bexport\s*\{[^}]*\}\s*;?\s*([^}]*)\}/g, |
| 73 | +// (match) => { |
| 74 | +// // Remove export statements from within declare namespace blocks |
| 75 | +// return match.replace(/export\s*\{[^}]*\}\s*;?\s*/g, ''); |
| 76 | +// } |
| 77 | +// ); |
| 78 | + |
| 79 | +// // 7. Fix event emitter method return types |
| 80 | +// content = content.replace( |
| 81 | +// /(\s+)(on|off|removeListener)\([^)]*\):\s*any;/g, |
| 82 | +// '$1$2(...args: any[]): this;' |
| 83 | +// ); |
| 84 | + |
| 85 | +// // 8. Remove problematic type assertions and fix generic types |
| 86 | +// content = content.replace(/: AppRegistry<[^>]*>/g, ': AppRegistry'); |
| 87 | + |
| 88 | +// // 9. Fix module resolution issues for mongodb-log-writer |
| 89 | +// content = content.replace( |
| 90 | +// /from\s*['"]mongodb-log-writer\/mongo-log-writer['"]/g, |
| 91 | +// "from 'mongodb-log-writer'" |
| 92 | +// ); |
| 93 | + |
| 94 | +// // 10. Remove or fix value/type confusion |
| 95 | +// content = content.replace( |
| 96 | +// /(\s+)([a-zA-Z_][a-zA-Z0-9_]*)\s+refers\s+to\s+a\s+value.*?Did you mean typeof.*?\n/g, |
| 97 | +// '' |
| 98 | +// ); |
| 99 | + |
| 100 | +// // 11. Add necessary imports at the top if we reference Node.js types |
| 101 | +// if ( |
| 102 | +// content.includes('NodeJS.EventEmitter') && |
| 103 | +// !content.includes('/// <reference types="node" />') |
| 104 | +// ) { |
| 105 | +// content = '/// <reference types="node" />\n' + content; |
| 106 | +// } |
| 107 | + |
| 108 | +// // Count final problematic imports |
| 109 | +// const finalWhatwgMatches = (content.match(/whatwg-url/g) || []).length; |
| 110 | +// const finalEventEmitterMatches = (content.match(/eventemitter3/g) || []).length; |
| 111 | +// const finalRelativeMatches = (content.match(/^import.*from.*['"]\./gm) || []) |
| 112 | +// .length; |
| 113 | +// const finalMongoJsMatches = (content.match(/from\s+['"]@mongodb-js\//g) || []) |
| 114 | +// .length; |
| 115 | + |
| 116 | +// // eslint-disable-next-line no-console |
| 117 | +// console.log( |
| 118 | +// `Final counts - whatwg-url: ${finalWhatwgMatches}, eventemitter3: ${finalEventEmitterMatches}, relative imports: ${finalRelativeMatches}, @mongodb-js imports: ${finalMongoJsMatches}` |
| 119 | +// ); |
| 120 | + |
| 121 | +// fs.writeFileSync(typesFile, content); |
| 122 | +// // eslint-disable-next-line no-console |
| 123 | +// console.log('Successfully cleaned up bundled types file'); |
0 commit comments