-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcompute-meta-hash.ts
33 lines (31 loc) · 1.14 KB
/
compute-meta-hash.ts
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
import { Hash, createHash } from "node:crypto";
import { readdirSync, statSync } from "node:fs";
import { join } from "node:path";
/**
* Creates hash of given files/folders. Used to conditionally deploy custom
* resources depending if source files have changed
*/
export function computeMetaHash(paths: string[], inputHash?: Hash) {
const hash = inputHash ? inputHash : createHash("sha1");
for (const path of paths) {
// ignore the .json files
if (path.endsWith(".json")) continue;
const statInfo = statSync(path);
if (statInfo.isDirectory()) {
const directoryEntries = readdirSync(path, { withFileTypes: true });
const fullPaths = directoryEntries.map((e) => join(path, e.name));
// recursively walk sub-folders
computeMetaHash(fullPaths, hash);
} else {
const statInfo = statSync(path);
// compute hash string name:size:mtime
const fileInfo = `${path}:${statInfo.size}:${statInfo.mtimeMs}`;
hash.update(fileInfo);
}
}
// if not being called recursively, get the digest and return it as the hash result
if (!inputHash) {
return hash.digest().toString("hex");
}
return "";
}