forked from SaltyMonkey/fps-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.manifest.js
52 lines (46 loc) · 1.79 KB
/
.manifest.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
"use strict";
const crypto = require("crypto");
const fs = require("fs");
const path = require("path");
const IGNORED_FILES = ["manifest.json", "manifest-generator.js", "manifest-generator.bat", "manifest-generator.exe", "node.exe"];
const IGNORED_START_SYMBOL = [".", "_"];
//load predefined manifest
let manifest = undefined;
try {
manifest = require("./manifest.json");
if (manifest && typeof manifest === "object") {
if (!manifest.files) manifest.files = {};
}
else manifest = { "files": {} };
}
catch (err) { manifest = { "files": {} };}
//cleanup manifest
Object.keys(manifest.files).forEach(entry => {
try { fs.accessSync(path.join(__dirname, entry), fs.constants.F_OK) }
catch (err) { delete manifest.files[entry]; }
});
//recursive file gather
function findInDirRelative(dir, fileList = []) {
const files = fs.readdirSync(dir);
files.forEach((file) => {
const filePath = path.join(dir, file);
const fileStat = fs.lstatSync(filePath);
if (!IGNORED_FILES.includes(file) && !IGNORED_START_SYMBOL.includes(file[0])) {
if (fileStat.isDirectory()) findInDirRelative(filePath, fileList);
else fileList.push(path.relative(__dirname, filePath));
}
});
return fileList;
}
//generate hash
const files = findInDirRelative(__dirname);
files.forEach(filePath => {
let file = filePath.replace(/\\/g, "/");
if (manifest.files[file] && typeof manifest.files[file] === "object") {
manifest.files[file].hash = crypto.createHash("sha256").update(fs.readFileSync(path.join(__dirname, file))).digest("hex");
}
else
manifest.files[file] = crypto.createHash("sha256").update(fs.readFileSync(path.join(__dirname, file))).digest("hex");
})
//save file
fs.writeFileSync(path.join(__dirname, "manifest.json"), JSON.stringify(manifest, null, "\t"), "utf8");