dressed
Version:
A sleek, serverless-ready Discord bot framework.
81 lines • 3.04 kB
JavaScript
import logTree from "../log-tree.js";
import ora from "ora";
import { stdout } from "node:process";
import bundleFile from "../bundle.js";
import { createHash } from "node:crypto";
import { join } from "node:path";
import { pathToFileURL } from "node:url";
export function createHandlerParser(options) {
return async (files) => {
if (files.length === 0)
return [];
const generatingLoader = ora({
stream: stdout,
text: options.messages.pending,
}).start();
const tree = logTree(files.length, options.col1Name, options.col2Name);
try {
let items = [];
for (const file of files) {
let data;
let itemMessages = options.itemMessages;
const uid = createHash("sha1").update(file.path).digest("hex");
try {
if (typeof itemMessages === "function") {
itemMessages = itemMessages(file);
}
const originalPath = file.path;
file.path = await bundleFile({
...file,
outPath: join(".dressed/cache", `${uid}.mjs`),
});
data = await options.createData({
...file,
path: pathToFileURL(file.path).href,
originalPath,
});
const hasConflict = items.some((c) => {
var _a;
if (c.name !== file.name)
return false;
return (_a = options.uniqueKeys) === null || _a === void 0 ? void 0 : _a.every((k) => data[k] === c.data[k]);
});
if (hasConflict) {
ora(itemMessages.confict).warn();
throw null;
}
items.push({
name: file.name,
path: file.path,
data,
uid,
exports: null,
});
tree.push(file.name, itemMessages.col2);
}
catch (e) {
if (e && e instanceof Error) {
ora(`Failed to parse ${file.path}:`).fail();
console.error(e);
}
continue;
}
}
if (options.postMortem) {
items = await options.postMortem(items);
}
generatingLoader.succeed(items.length > 0
? options.messages.generated
: options.messages.noItems);
if (items.length > 0) {
tree.log();
}
return items;
}
catch (e) {
generatingLoader.fail();
throw e;
}
};
}
//# sourceMappingURL=index.js.map