forked from ByMykel/counter-strike-file-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
139 lines (120 loc) · 4.19 KB
/
main.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
128
129
130
131
132
133
134
135
136
137
138
139
import fs from "fs";
import path from "path";
import chardet from "chardet";
import { parse as parse1 } from "@node-steam/vdf";
import { parse as parse2 } from "vdf-parser";
import { parse as parse3 } from "kvparser";
const __dirname = path.resolve()
// const CONFIG = JSON.parse(fs.readFileSync("../../config.json", "utf-8"));
// const SOURCE_PATHS = CONFIG.SOURCE_PATHS;
const SOURCE_PATHS = path.join(__dirname,'original');
// const TARGET_FOLDER = CONFIG.TARGET_FOLDER;
const TARGET_FOLDER = path.join(__dirname,'parsed');
console.log(SOURCE_PATHS,)
console.log(TARGET_FOLDER,)
const PARSERS = [
{
name: "@node-steam/vdf",
parse: parse1,
output: path.join(TARGET_FOLDER, "@node-steam--vdf"),
},
// {
// name: "vdf-parser",
// parse: parse2,
// output: path.join(TARGET_FOLDER, "vdf-parser"),
// },
{
name: "kvparser",
parse: parse3,
output: path.join(TARGET_FOLDER, "kvparser"),
},
];
PARSERS.forEach((e,i)=>{
if (!fs.existsSync(e.output)) {
fs.mkdirSync(e.output, { recursive: true });
}
})
function detectFileEncoding(filePath) {
return chardet.detectFileSync(filePath);
}
function convertEncodingOfFile(file) {
return new Promise((resolve, reject) => {
const filePath = path.join(SOURCE_PATHS, file);
const fileEncoding = detectFileEncoding(filePath);
if (fileEncoding !== "UTF-16LE") {
console.log(
`[Skipping conversion] File ${file} is not in UTF-16LE.`
);
return resolve();
}
try {
// If it's UTF-16LE, then convert to UTF-8
const content = fs.readFileSync(filePath, { encoding: "utf16le" });
fs.writeFileSync(filePath, content, { encoding: "utf8" });
console.log(`Converted ${file} successfully.`);
resolve();
} catch (error) {
console.error(`Error converting ${file}: ${error}`);
reject(error);
}
});
}
function moveFilesToTarget() {
return new Promise((resolve, reject) => {
try {
for (const sourcePath of SOURCE_PATHS) {
const targetPath = path.join(
TARGET_FOLDER,
path.basename(sourcePath)
);
fs.copyFileSync(sourcePath, targetPath);
}
resolve();
} catch (error) {
reject(error);
}
});
}
function convertAndSaveFiles() {
return new Promise((resolve, reject) => {
const files = fs.readdirSync(SOURCE_PATHS);
for (const file of files) {
const filePath = path.join(SOURCE_PATHS, file);
if (fs.statSync(filePath).isFile()) {
for (const parser of PARSERS) {
fs.mkdirSync(parser.output, { recursive: true });
try {
const content = fs
.readFileSync(filePath, "utf8")
.replace(/^\uFEFF/, "");
const parsedData = parser.parse(content);
const newContent = JSON.stringify(parsedData, null, 4);
const outputFilePath = path.join(
parser.output,
file.replace(path.extname(file), ".json")
);
fs.writeFileSync(outputFilePath, newContent);
} catch (error) {
console.error(
`\n[parser:${parser.name}] Error parsing file ${filePath}\n`,
error
);
}
}
}
}
resolve();
});
}
async function processFiles() {
// 1. First move files to the target folder
// await moveFilesToTarget();
// 2. Then, get a list of all files in the TARGET_FOLDER
const files = fs.readdirSync(SOURCE_PATHS);
console.log(files)
// 3. Wait for all encoding conversions to finish
// await Promise.all(files.map((file) => convertEncodingOfFile(file)));
// 4. Finally, convert and save the files
await convertAndSaveFiles();
}
processFiles();