-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathfixup.cjs
executable file
·34 lines (28 loc) · 1.03 KB
/
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
26
27
28
29
30
31
32
33
34
const fs = require('fs');
const path = require('path');
const updateRequires = (filePath) => {
let content = fs.readFileSync(filePath, 'utf8');
//replace local imports eg. require('./ecpair') to require('ecpair.cjs')
content = content.replace(/require\('\.\/([^']*)'\)/g, "require('./$1.cjs')");
fs.writeFileSync(filePath, content, 'utf8');
};
const updateImports = (filePath) => {
let content = fs.readFileSync(filePath, 'utf8');
//replace local imports eg. from './types'; to from './types.js';
content = content.replace(/from '\.\/([^']*)'/g, "from './$1.js'");
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);
} else if (filePath.endsWith('.js')) {
updateImports(filePath);
}
});
};
const dir = path.join(__dirname, 'src');
processFiles(dir);