@hopeio/utils
Version:
javascript utils
48 lines (47 loc) • 1.12 kB
JavaScript
import o from "fs";
import i from "crypto";
function h(e, t) {
o.readdirSync(e, { withFileTypes: !0 }).forEach(
(n) => {
const r = e + "/" + n.name;
n.isDirectory() ? h(r, t) : t(e, n.name, r);
}
);
}
function y(e, t, n, r) {
o.existsSync(t) || o.mkdirSync(t), o.readdirSync(e, { withFileTypes: !0 }).forEach(
(a) => {
if (r && !r(a)) return;
const c = e + "/" + a.name;
if (a.isDirectory()) {
const s = t + "/" + a.name;
y(c, s, n, r);
} else
n(a.name, c, t);
}
);
}
async function S(e) {
try {
const t = await o.promises.readFile(e), n = i.createHash("sha256");
n.update(t);
const r = n.digest("hex");
console.log(`File ${e} hash: ${r}`);
} catch (t) {
console.error("Error calculating file hash:", t);
}
}
function p(e, t) {
o.existsSync(t) || o.mkdirSync(t), o.readdirSync(e).forEach(
(n) => {
let r = e + "/" + n;
o.statSync(r).isDirectory() ? p(r, t + "/" + n) : o.copyFileSync(r, t + "/" + n);
}
);
}
export {
S as calculateFileHash,
p as copyDir,
h as range,
y as sync
};