forked from HakuryuuDom/translate-chat
-
Notifications
You must be signed in to change notification settings - Fork 3
/
manifest-generator.js
52 lines (44 loc) · 1.99 KB
/
manifest-generator.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
// copied from https://github.com/SaltyMonkey/fps-manager
'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', 'package-lock.json'];
const IGNORED_START_SYMBOL = ['.', '_'];
const IGNORED_DIRS = [path.join(__dirname, 'node_modules')];
// load predefined manifest
let manifest;
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 = []) {
if (IGNORED_DIRS.includes(dir)) return 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 => {
const 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 and add an empty line at the end
fs.writeFileSync(path.join(__dirname, 'manifest.json'), JSON.stringify(manifest, null, ' ') + '\n', 'utf8');