This repository was archived by the owner on Oct 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
54 lines (45 loc) · 1.64 KB
/
index.js
File metadata and controls
54 lines (45 loc) · 1.64 KB
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
const fs = require("fs");
const config = require("./config.json");
var targetPath = config.targetPath;
var outputPath = config.outputPath;
var StringsToReplace = config.strings;
// check if file exists
if (fs.existsSync(targetPath)) {
console.log("Metadata file exists! writing to file.");
/*const reader = fs.createReadStream(targetPath);
reader.on('data', function(chunk) {
PatchFile(chunk);
}); */
// reads from the file.
fs.readFile(targetPath, function read(error, data) {
if (error) {
throw error;
}
const content = data;
PatchFile(content);
});
}
function PatchFile(fileData) {
var data = fileData; // buffer that we change.
for (let i = 0; i < StringsToReplace.length; i++) {
//data.write(StringsToReplace[i][1], data.indexOf(StringsToReplace[i][0]));
var setString = StringsToReplace[i][1];
var lookStringLength = StringsToReplace[i][0].length;
var replaceStringLength = StringsToReplace[i][1].length;
var diff = Math.abs(lookStringLength - replaceStringLength);
if (setString.length < lookStringLength)
{
// fill in data with an empty char. if the string to replace is shorter than the string to look for
for (let a = 0; a < diff; a++) {
setString += "\x00";
}
}
data.write(setString, data.indexOf(StringsToReplace[i][0]));
}
try {
fs.writeFileSync(outputPath, data);
console.log("Successfully wrote to file!");
} catch (error) {
throw error;
}
}