-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix-server-exports.js
69 lines (60 loc) · 2.11 KB
/
fix-server-exports.js
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Script to fix constant/variable exports in files with "use server" directive
const fs = require("fs");
const { execSync } = require("child_process");
// Get list of files with "use server" directive
const output = execSync(
'grep -l "use server" $(find src -name "*.ts" -o -name "*.tsx")'
).toString();
const files = output.split("\n").filter(Boolean);
console.log(`Found ${files.length} files with "use server" directive.`);
// Functions to fix different export patterns
function fixFileExports(filePath) {
let content = fs.readFileSync(filePath, "utf8");
let fixed = false;
// Pattern 1: export const foo = async () => { ... }
// Replace with: export async function foo() { ... }
const arrowFunctionPattern =
/export\s+const\s+([a-zA-Z0-9_]+)\s*=\s*async\s*\(([^)]*)\)\s*=>\s*(\{[\s\S]*?\n\})/g;
content = content.replace(
arrowFunctionPattern,
(match, name, params, body) => {
fixed = true;
return `export async function ${name}(${params})${body}`;
}
);
// Pattern 2: export const foo = () => { ... }
// Replace with: export async function foo() { ... }
const nonAsyncArrowPattern =
/export\s+const\s+([a-zA-Z0-9_]+)\s*=\s*\(([^)]*)\)\s*=>\s*(\{[\s\S]*?\n\})/g;
content = content.replace(
nonAsyncArrowPattern,
(match, name, params, body) => {
fixed = true;
return `export async function ${name}(${params})${body}`;
}
);
// If we fixed anything, write the file back
if (fixed) {
fs.writeFileSync(filePath, content);
console.log(`Fixed exports in ${filePath}`);
return true;
}
return false;
}
// Fix all files
let fixedCount = 0;
for (const file of files) {
if (fixFileExports(file)) {
fixedCount++;
}
}
if (fixedCount > 0) {
console.log(`\nFixed ${fixedCount} files.`);
console.log(
"You may need to manually review some files as the automatic fixes might not cover all cases."
);
console.log("Run check-server-exports.js again to verify remaining issues.");
} else {
console.log("No files needed fixing or automatic fixes were not sufficient.");
console.log("You may need to manually fix any remaining issues.");
}