This repository has been archived by the owner on Jun 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkspace-hash.js
65 lines (55 loc) · 3.33 KB
/
workspace-hash.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
const { hashElement } = require("folder-hash");
var crypto = require("crypto");
const fs = require("fs");
const b64 = require("base-64");
/**
* @typedef {Object} hashOptions JSON object with additional options
* @property {Array} excludeFolders an array of folders that should be excluded from the hashing
* @property {Array} excludeFiles an array of files that should be excluded from the hashing
* @property {String} outputFile output file that gets overridden with the hashes, leave null or undefined to disable (note: this file will automatically be excluded from the hashing)
*/
/**
* Hashes your entire workspace (excluding some files and folders)
* @param {String} [algorithm="sha1"] the algorithm of the hash (can be "md5", "sha1" or "sha256", defaults to "sha1")
* @param {String} [digest="hex"] the digest type you want (can be "hex", "base64" or "latin1", defaults to "hex")
* @param {hashOptions} options JSON object with additional options
* @returns {Promise<String>} on resolve: string containing the digested hash, on reject: string containing error message
*/
module.exports = (algorithm, digest, options) => {
var algorithmAllowed = ["md5", "sha1", "sha256"];
var digestAllowed = ["hex", "base64", "latin1"];
return new Promise((resolve, reject) => {
if(!algorithmAllowed.join(",").includes(algorithm) && algorithm != null) reject(`Algorithm is wrong! You entered: "${algorithm}" - Allowed values: "${algorithmAllowed.join(", ")}"`);
if(!digestAllowed.join(",").includes(digest) && digest != null) reject(`Digest is wrong! You entered: "${digest}" - Allowed values: "${digestAllowed.join(", ")}"`);
if(digest == null) digest = "hex";
if(algorithm == null) algorithm = "sha1";
var opts = {
folders: {exclude: [".*", "node_modules", "test_coverage"]},
files: {include: ["*.js", "*.json"], exclude: ["*.ini", ".*", "settings.*", "*.cfg", "*.conf", "*.txt", "*.log", ".gitignore", "*.md"]}
}
if(options.outputFile != null) opts.files.exclude.push(options.outputFile);
if(options.excludeFiles != null && typeof options.excludeFiles == "object") {
options.excludeFiles.forEach(v => {
opts.files.exclude.push(v);
});
}
if(options.excludeFolders != null && typeof options.excludeFolders == "object") {
options.excludeFolders.forEach(v => {
opts.folders.exclude.push(v);
});
}
hashElement(".", opts).then(hash => {
var shasum = crypto.createHash(algorithm);
var encHash = shasum.update(hash.toString());
encHash = shasum.digest(digest);
var fileChecksum;
if(digest == "base64") fileChecksum = `${algorithm};\nbase64: ${encHash}\nBASE64: ${encHash.toUpperCase()}`;
else if(digest == "latin1") fileChecksum = `${algorithm};\nlatin1: ${encHash}\nLATIN1: ${encHash.toUpperCase()}\nbase64: ${b64.encode(encHash)}`;
else fileChecksum = `${algorithm};\nhex: ${encHash}\nHEX: ${encHash.toUpperCase()}\nbase64: ${b64.encode(encHash)}`;
if(options.outputFile != null) fs.writeFileSync(options.outputFile, fileChecksum);
resolve(encHash);
}).catch(err => {
reject(err);
});
});
}