forked from RusFeniks/MinecraftUpdater-ServerSide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateHash.js
57 lines (47 loc) · 1.71 KB
/
updateHash.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
const crypto = require('crypto');
const fs = require('fs');
let settings = JSON.parse(fs.readFileSync("syncOptions.json"));
function makePath(_localPath) {
let slashRegExp = new RegExp("[\\/]");
let dotRegExp = new RegExp("^[.]*$");
let pathChunks = (settings.root + _localPath).split(slashRegExp);
let resultPath = ".";
pathChunks.forEach(chunk => {
if (!dotRegExp.test(chunk)) {
resultPath += `/${chunk}`
}
});
return resultPath;
}
function getFileHash(_path) {
if (fs.statSync(_path).isDirectory()) { return ""; }
let hash = crypto.createHash('sha256');
let file = fs.readFileSync(_path);
return hash.update(file).digest("hex");
}
function getFilesList(_path) {
let filesList = [ _path ];
if (!fs.existsSync(_path)) { return []; }
if (!fs.statSync(_path).isDirectory()) { return filesList; }
fs.readdirSync(_path).forEach(file => {
let fullPath = _path + `/${file}`;
if(fs.statSync(fullPath).isDirectory()) {
filesList = filesList.concat(getFilesList(fullPath));
} else { filesList.push(fullPath); }
});
return filesList;
}
let serverFileHashList = [];
settings.syncElements.forEach(element => {
let path = makePath(element);
getFilesList(path).forEach(_file => {
!serverFileHashList.find(e => { return e.file == _file }) ?
serverFileHashList.push({
file: _file.replace(new RegExp(`^${settings.root}`), ""),
hash: getFileHash(_file)
}) : null;
});
})
console.log(serverFileHashList);
fs.writeFileSync(makePath(".hash"), JSON.stringify(serverFileHashList));
fs.writeFileSync(makePath(".syncOptions"), JSON.stringify(settings));