Repository navigation
Expand file tree
/
Copy pathpatch-apphelper.js
More file actions
114 lines (100 loc) · 3.48 KB
/
Copy pathpatch-apphelper.js
File metadata and controls
114 lines (100 loc) · 3.48 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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const fs = require("fs");
const path = require("path");
const apphelperpPath = "./node_modules/@churchapps/apphelper/dist";
// Function to add .js extensions to relative imports
function addJsExtensions(filePath) {
if (!fs.existsSync(filePath)) {
console.log(`File not found: ${filePath}`);
return;
}
let content = fs.readFileSync(filePath, "utf8");
const originalContent = content;
// Add .js extension to relative imports/exports that don't already have an extension
// Handle import statements
content = content.replace(/from "(\.[^"]+?)"/g, (match, importPath) => {
if (!importPath.includes(".js")) {
if (importPath.endsWith("/")) {
return `from "${importPath}index.js"`;
} else if (importPath.endsWith("/helpers") || importPath.endsWith("/components") || importPath.endsWith("/hooks")) {
return `from "${importPath}/index.js"`;
} else {
return `from "${importPath}.js"`;
}
}
return match;
});
content = content.replace(/from '(\.[^']+?)'/g, (match, importPath) => {
if (!importPath.includes(".js")) {
if (importPath.endsWith("/")) {
return `from '${importPath}index.js'`;
} else if (importPath.endsWith("/helpers") || importPath.endsWith("/components") || importPath.endsWith("/hooks")) {
return `from '${importPath}/index.js'`;
} else {
return `from '${importPath}.js'`;
}
}
return match;
});
// Handle export statements
content = content.replace(/export \* from "(\.[^"]+?)"/g, (match, importPath) => {
if (!importPath.includes(".js")) {
if (importPath.endsWith("/")) {
return `export * from "${importPath}index.js"`;
} else {
return `export * from "${importPath}.js"`;
}
}
return match;
});
content = content.replace(/export \* from '(\.[^']+?)'/g, (match, importPath) => {
if (!importPath.includes(".js")) {
if (importPath.endsWith("/")) {
return `export * from '${importPath}index.js'`;
} else {
return `export * from '${importPath}.js'`;
}
}
return match;
});
// Handle named exports
content = content.replace(/export \{ [^}]+ \} from "(\.[^"]+?)"/g, (match, importPath) => {
if (!importPath.includes(".js")) {
if (importPath.endsWith("/")) {
return match.replace(`from "${importPath}"`, `from "${importPath}index.js"`);
} else {
return match.replace(`from "${importPath}"`, `from "${importPath}.js"`);
}
}
return match;
});
content = content.replace(/export \{ [^}]+ \} from '(\.[^']+?)'/g, (match, importPath) => {
if (!importPath.includes(".js")) {
if (importPath.endsWith("/")) {
return match.replace(`from '${importPath}'`, `from '${importPath}index.js'`);
} else {
return match.replace(`from '${importPath}'`, `from '${importPath}.js'`);
}
}
return match;
});
if (content !== originalContent) {
fs.writeFileSync(filePath, content);
console.log(`Patched: ${filePath}`);
}
}
// Find all JS files and patch them
function patchDirectory(dir) {
const files = fs.readdirSync(dir);
files.forEach(file => {
const fullPath = path.join(dir, file);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
patchDirectory(fullPath);
} else if (file.endsWith(".js")) {
addJsExtensions(fullPath);
}
});
}
console.log("Patching @churchapps/apphelper ES module imports...");
patchDirectory(apphelperpPath);
console.log("Patching complete!");