-
Notifications
You must be signed in to change notification settings - Fork 0
/
replace-paths.cjs
62 lines (51 loc) · 1.49 KB
/
replace-paths.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
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
const fs = require('fs');
const path = require('path');
const distDir = 'dist'; // 빌드된 폴더 경로
const oldPath = '/images/'; // 변경할 기존 경로
const newPath = 'https://tistory4.daumcdn.net/tistory/2876097/skin/images/'; // 새로운 경로
function shouldProcessFile(filename) {
return (
filename === 'index.html' ||
(filename.startsWith('index-') && filename.endsWith('.js'))
);
}
function replaceInFile(filePath) {
// console.log(filePath);
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error(`Error reading file ${filePath}:`, err);
return;
}
const result = data.replace(new RegExp(oldPath, 'g'), newPath);
fs.writeFile(filePath, result, 'utf8', (err) => {
if (err) {
console.error(`Error writing file ${filePath}:`, err);
} else {
console.log(`Updated ${filePath}`);
}
});
});
}
function walkDir(dir) {
fs.readdir(dir, (err, files) => {
if (err) {
console.error(`Error reading directory ${dir}:`, err);
return;
}
files.forEach((file) => {
const filePath = path.join(dir, file);
fs.stat(filePath, (err, stats) => {
if (err) {
console.error(`Error getting file stats for ${filePath}:`, err);
return;
}
if (stats.isDirectory()) {
walkDir(filePath);
} else if (stats.isFile() && shouldProcessFile(file)) {
replaceInFile(filePath);
}
});
});
});
}
walkDir(distDir);