-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
fixup.cjs
executable file
·25 lines (21 loc) · 804 Bytes
/
fixup.cjs
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
const fs = require('fs');
const path = require('path');
const updateRequires = (filePath) => {
let content = fs.readFileSync(filePath, 'utf8');
//replace local imports eg. require("./ecpair.js") to require("ecpair.cjs")
content = content.replace(/require\('\.\/([^']*)\.js'\)/g, "require('./$1.cjs')");
content = content.replace(/require\('\.\.\/([^']*)\.js'\)/g, "require('../$1.cjs')");
fs.writeFileSync(filePath, content, 'utf8');
};
const processFiles = (dir) => {
fs.readdirSync(dir).forEach((file) => {
const filePath = path.join(dir, file);
if (fs.lstatSync(filePath).isDirectory()) {
processFiles(filePath);
} else if (filePath.endsWith('.cjs')) {
updateRequires(filePath);
}
});
};
const dir = path.join(__dirname, 'src', 'cjs');
processFiles(dir);