forked from SkyCryptWebsite/SkyCrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashes.js
More file actions
58 lines (42 loc) · 1.49 KB
/
Copy pathhashes.js
File metadata and controls
58 lines (42 loc) · 1.49 KB
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
import { createHash } from "crypto";
import * as fs from "fs";
import * as path from "path";
import { fileURLToPath } from "url";
import { promisify } from "util";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
export const HASHED_DIRECTORIES = ["css"];
export function getFileHash(filename) {
return new Promise((resolve, reject) => {
const md5sum = createHash("md5");
const s = fs.createReadStream(filename);
s.on("data", function (data) {
md5sum.update(data);
});
s.on("end", function () {
const hash = md5sum.digest("hex");
resolve(hash);
});
});
}
export function getFileHashes() {
const directoryPromises = HASHED_DIRECTORIES.map(async (directory) => {
const readdirPromise = promisify(fs.readdir);
const fileNames = await readdirPromise(path.join(__dirname, "../public/resources", directory));
const filePromises = fileNames.map((filename) =>
getFileHash(path.join(__dirname, "../public/resources", directory, filename)),
);
const fileHashes = await Promise.all(filePromises);
const hashesObject = {};
for (let i = 0; i < fileNames.length; i++) {
hashesObject[fileNames[i]] = fileHashes[i];
}
return hashesObject;
});
return Promise.all(directoryPromises).then((directories) => {
const directoriesObject = {};
for (let i = 0; i < HASHED_DIRECTORIES.length; i++) {
directoriesObject[HASHED_DIRECTORIES[i]] = directories[i];
}
return directoriesObject;
});
}