| import * as fs from 'fs'; |
| import * as path from 'path'; |
|
|
| function processDirectory(directory: string) { |
| fs.readdirSync(directory).forEach(file => { |
| const fullPath = path.join(directory, file); |
| if (fs.statSync(fullPath).isDirectory()) { |
| processDirectory(fullPath); |
| } else if (fullPath.endsWith('.ts') && !fullPath.includes('node_modules') && !fullPath.includes('dist')) { |
| processFile(fullPath); |
| } |
| }); |
| } |
|
|
| function processFile(filePath: string) { |
| let content = fs.readFileSync(filePath, 'utf-8'); |
| let original = content; |
|
|
| |
| content = content.replace(/catch\s*\(\s*([a-zA-Z0-9_]+)\s*:\s*any\s*\)/g, 'catch ($1: unknown)'); |
|
|
| |
| |
| |
| |
| const errorVars = ['e', 'err', 'error']; |
| for (const v of errorVars) { |
| |
| const regex = new RegExp(`\\b${v}\\.message\\b`, 'g'); |
| content = content.replace(regex, `(${v} instanceof Error ? ${v}.message : String(${v}))`); |
| } |
|
|
| |
| |
| |
|
|
| if (content !== original) { |
| fs.writeFileSync(filePath, content, 'utf-8'); |
| console.log(`Updated: ${filePath}`); |
| } |
| } |
|
|
| const targetDirs = [ |
| path.resolve(__dirname, '..'), |
| path.resolve(__dirname, '../../../whatsapp-worker/src') |
| ]; |
|
|
| targetDirs.forEach(dir => processDirectory(dir)); |
| console.log('Purge any in catch blocks completed!'); |
|
|