dressed
Version:
A sleek, serverless-ready Discord bot framework.
47 lines • 1.91 kB
JavaScript
import { createHash } from "node:crypto";
import { existsSync } from "node:fs";
import { readdir } from "node:fs/promises";
import { basename, extname, join, relative, resolve } from "node:path";
import { cwd } from "node:process";
import { logWarn } from "./log.js";
export function importString(file) {
return `import * as h${file.uid} from "${relative(".dressed/tmp", file.path).replace(/\\/g, "/")}";`;
}
export function categoryExports(categories) {
return categories.map((c, i) => `export const ${["commands", "components", "events"][i]} = [${c.map((f) => JSON.stringify({ ...f, exports: null }).replace('"exports":null', `"exports":h${f.uid}`))}];`);
}
/** Deep merges two objects, producing a new object where values from `b` override those from `a`. */
export function override(a, b) {
const result = { ...a };
for (const key in b) {
const k = key;
const bv = b[k];
const av = a[k];
if (bv !== undefined && typeof bv === "object" && bv !== null && !Array.isArray(bv)) {
result[k] = override(av !== null && av !== void 0 ? av : {}, bv);
}
else if (bv !== undefined) {
result[k] = bv;
}
}
return result;
}
export async function crawlDir(root, dir, extensions = ["js", "ts", "mjs"]) {
const dirPath = resolve(root, dir);
if (!existsSync(dirPath)) {
logWarn(`${dir.slice(0, 1).toUpperCase() + dir.slice(1)} directory not found`);
return [];
}
const entries = await readdir(dirPath, { recursive: true });
return entries
.filter((e) => extensions.includes(extname(e).slice(1)))
.map((e) => {
const path = relative(cwd(), join(dirPath, e));
return {
name: basename(path, extname(path)),
path,
uid: createHash("sha1").update(path).digest("hex"),
};
});
}
//# sourceMappingURL=build.js.map