-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
29 lines (21 loc) · 1.08 KB
/
Copy pathscript.js
File metadata and controls
29 lines (21 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const fs = require('fs');
const path = require('path');
const dir = path.join(process.cwd(), 'src/db/queries');
const files = fs.readdirSync(dir);
files.forEach(file => {
if (!file.endsWith('.ts')) return;
const filePath = path.join(dir, file);
let code = fs.readFileSync(filePath, 'utf8');
code = code.replace(/export async function (\w+)\(([^)]*)\)(?:\s*:\s*[^{]+)?\s*\{([\s\S]*?)\n\}/g, (match, fnName, args, body) => {
if (!body.includes('db.') || body.includes('try {')) return match;
const dbMatch = body.match(/\s*const db = await getDatabase\(\);/);
if (!dbMatch) return match;
const preDb = body.substring(0, dbMatch.index + dbMatch[0].length);
const postDb = body.substring(dbMatch.index + dbMatch[0].length);
const indentedPostDb = postDb.replace(/\n/g, '\n ');
const newBody = `${preDb}\n try {${indentedPostDb}\n } catch (error) {\n console.error('Database Error in ${fnName}:', error);\n throw error;\n }`;
const newMatch = match.replace(body, newBody);
return newMatch;
});
fs.writeFileSync(filePath, code);
});