Iosevka/utility/create-sha-file.mjs
Au f6e57fbf0b
fix(utility): correct the format of generated hash values and file names (#2012)
Fix a bug in the utility/create-sha-file.mjs module that caused the generated hash values to have an incorrect format and the file names to be undefined. Use the file.name property instead of the file.base property to get the correct file name. Append the hash value before the file name, separated by a tab character, to match the expected format.
2023-09-28 13:49:13 -07:00

30 lines
753 B
JavaScript

import crypto from "crypto";
import fs from "fs";
function hashFile(path) {
return new Promise((resolve, reject) => {
let sum = crypto.createHash("sha256");
let fileStream = fs.createReadStream(path);
fileStream.on("error", err => {
return reject(err);
});
fileStream.on("data", chunk => {
try {
sum.update(chunk);
} catch (ex) {
return reject(ex);
}
});
fileStream.on("end", () => {
return resolve(sum.digest("hex"));
});
});
}
export default (async function (out, archiveFiles) {
const filesToAnalyze = Array.from(new Set(archiveFiles.map(f => f.full))).sort();
let s = "";
for (const file of filesToAnalyze) {
s += `${await hashFile(file)}\t${file.name}\n`;
}
await fs.promises.writeFile(out, s);
});