-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost-build.js
70 lines (61 loc) · 2.13 KB
/
post-build.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
70
import fs from "fs";
import path, { dirname } from "path";
import { fileURLToPath } from "url";
const directoryPath = './dist'; // Replace with your directory path
const searchPattern = /from\s+['"]([^'"]+)['"]/g;
const __dirname = dirname(fileURLToPath(import.meta.url));
const indexPath = path.join(__dirname, 'dist', 'index.js');
function getRelativePath(filePath, importPath) {
const fileDir = path.dirname(filePath);
const importDir = path.dirname(importPath);
let relativePath = path.relative(fileDir, importDir);
if (relativePath.startsWith('../')) {
relativePath = relativePath.slice(3);
}
return relativePath.startsWith('.') ? `${relativePath.replace(importDir, '')}` : `./`;
}
function replaceImportsInFile(filePath) {
const fileContents = fs.readFileSync(filePath, 'utf8');
const updatedContents = fileContents.replace(searchPattern, (match, importPath) => {
if (importPath.startsWith('./') || importPath.startsWith('../')) {
return match; // import path already contains relative prefix
} else if (importPath.includes('/')) {
const relativePath = getRelativePath(filePath, importPath);
return `from "${relativePath}${importPath}.js"`;
}
return match;
});
fs.writeFileSync(filePath, updatedContents);
}
function processDirectory(dirPath) {
const files = fs.readdirSync(dirPath);
for (const file of files) {
const filePath = path.join(dirPath, file);
const stats = fs.statSync(filePath);
if (stats.isDirectory()) {
processDirectory(filePath);
} else if (filePath.endsWith('.js')) {
replaceImportsInFile(filePath);
}
}
}
function addShebang() {
fs.readFile(indexPath, 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
// add the shebang line at the beginning of the file
const updatedContents = `#!/usr/bin/env node\n${data}`;
// write the updated contents back to the file
fs.writeFile(indexPath, updatedContents, (err) => {
if (err) {
console.error(err);
return;
}
console.log('Shebang line added to index.js');
});
});
}
processDirectory(directoryPath);
addShebang();