-
Notifications
You must be signed in to change notification settings - Fork 0
/
skeleton.js
127 lines (117 loc) · 4.82 KB
/
skeleton.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const fs = require('fs');
const path = require('path');
function replaceStringsInFolder(folderPath, mapping) {
fs.readdir(folderPath, (err, files) => {
if (err) {
console.error("Error reading directory:", err);
return;
}
files.forEach(file => {
const oldFilePath = path.join(folderPath, file);
fs.stat(oldFilePath, (err, stats) => {
if (err) {
console.error("Error getting file stats:", err);
return;
}
if (stats.isDirectory()) {
if (!mapping.ignoredFolders.includes(file)) {
const newFolderName = replaceStrings(file, mapping.mappings);
if (newFolderName !== file) {
const newFolderPath = path.join(folderPath, newFolderName);
fs.rename(oldFilePath, newFolderPath, (err) => {
if (err) {
console.error(`Error renaming folder ${oldFilePath} to ${newFolderPath}:`, err);
return;
}
console.log(`✅ Renamed folder: ${oldFilePath} => ${newFolderPath}`);
replaceStringsInFolder(newFolderPath, mapping);
});
} else {
replaceStringsInFolder(oldFilePath, mapping);
}
}
} else if (stats.isFile()) {
if (!mapping.ignoredFiles.includes(file)) {
replaceStringsInFile(oldFilePath, mapping.mappings, () => {
const newFileName = replaceStrings(file, mapping.mappings);
if (newFileName !== file) {
const newFilePath = path.join(folderPath, newFileName);
fs.rename(oldFilePath, newFilePath, (err) => {
if (err) {
console.error(`Error renaming file ${oldFilePath} to ${newFilePath}:`, err);
return;
}
console.log(`✅ Renamed file: ${oldFilePath} => ${newFilePath}`);
});
}
});
}
}
});
});
});
}
function replaceStringsInFile(filePath, mapping, callback) {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error("Error reading file:", err);
return;
}
let replacedData = data;
let replacementsCount = 0;
for (const search in mapping) {
if (mapping.hasOwnProperty(search)) {
const replacement = mapping[search];
if (!replacement) {
console.warn(`⚠️ Warning: Replacement value for "${search}" is empty.`);
} else {
const regex = new RegExp(search, 'g');
const occurrences = (replacedData.match(regex) || []).length;
if (occurrences > 0) {
replacedData = replacedData.replace(regex, replacement);
replacementsCount += occurrences;
}
}
}
}
fs.writeFile(filePath, replacedData, 'utf8', (err) => {
if (err) {
console.error("❌ Error writing to file:", err);
return;
}
if (replacementsCount > 0) {
console.log(`✅ Replaced ${replacementsCount} values in ${filePath}`);
}
callback();
});
});
}
function replaceStrings(str, mapping) {
let replacedStr = str;
for (const search in mapping) {
if (mapping.hasOwnProperty(search)) {
const replacement = mapping[search];
if (!replacement) {
console.warn(`⚠️ Warning: Replacement value for "${search}" is empty.`);
} else {
replacedStr = replacedStr.replace(new RegExp(search, 'g'), replacement);
}
}
}
return replacedStr;
}
const folderPath = '.';
const mappingFilePath = 'skeletonrc.json';
fs.readFile(mappingFilePath, 'utf8', (err, data) => {
if (err) {
console.error("❌ Error reading mapping file:", err);
return;
}
try {
const mapping = JSON.parse(data);
replaceStringsInFolder(folderPath, mapping);
} catch (err) {
console.error("❌ Error parsing mapping file:", err);
}
console.log("\x1b[33m%s\x1b[0m", `\n💛 Stay Frank!`);
});