-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostprocess.js
63 lines (55 loc) · 1.92 KB
/
postprocess.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
const fs = require("fs");
const path = require("path");
const directory = "./out/app"; // Directory where JavaScript files are located
// Recursive function to browse all files in a directory and its sub-directories
function processFiles(dirPath) {
fs.readdir(dirPath, (err, files) => {
if (err) {
console.error(`Error reading directory ${dirPath}:`, err);
return;
}
files.forEach((file) => {
const filePath = path.join(dirPath, file);
// Check if it's a file
fs.stat(filePath, (err, stats) => {
if (err) {
console.error(`Error stating file ${filePath}:`, err);
return;
}
if (stats.isFile() && filePath.endsWith(".js")) {
// It's a .js file, read the contents
fs.readFile(filePath, "utf8", (err, data) => {
if (err) {
console.error(`Error reading file ${filePath}:`, err);
return;
}
// Replace imports without extension by those with ".js" extension
const updatedContent = data.replace(
/from ['"](.+)['"]/g,
(match, importPath) => {
if (!importPath.endsWith(".js")) {
return `from '${importPath}.js'`;
} else {
return match;
}
}
);
// Write the updated content to the file
fs.writeFile(filePath, updatedContent, "utf8", (err) => {
if (err) {
console.error(`Error writing to file ${filePath}:`, err);
return;
}
console.log(`File updated: ${filePath}`);
});
});
} else if (stats.isDirectory()) {
// It's a directory, recursively call the function to process its files
processFiles(filePath);
}
});
});
});
}
// Start root directory processing
processFiles(directory);