UNPKG

dressed

Version:

A sleek, serverless-ready Discord bot framework.

87 lines 4.04 kB
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) { if (typeof path === "string" && /^\.\.?\//.test(path)) { return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) { return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js"); }); } return path; }; import { appendFileSync, mkdirSync, readdirSync, writeFileSync } from "node:fs"; import { basename, extname, resolve } from "node:path"; import { getApp } from "../../resources/generated.resources.js"; import { categoryExports, crawlDir, importString, override } from "../../utils/build.js"; import { botEnv } from "../../utils/env.js"; import { logDefer, logError } from "../../utils/log.js"; import bundleFiles from "./bundle.js"; import { parseCommands } from "./parsers/commands.js"; import { parseComponents } from "./parsers/components.js"; import { parseEvents } from "./parsers/events.js"; /** * Builds the bot imports and other variables. */ export default async function build(config = {}, { bundle = bundleFiles } = {}) { var _a, _b; mkdirSync(".dressed/tmp", { recursive: true }); await fetchMissingVars(); const configPath = readdirSync(".").find((f) => basename(f, extname(f)) === "dressed.config"); const configOutPath = ".dressed/tmp/dressed.config.mjs"; if (configPath) { await bundle(configPath, ".dressed/tmp"); const { default: importedConfig } = await import(__rewriteRelativeImportExtension(resolve(configOutPath))); config = override(importedConfig, config); } else { writeFileSync(configOutPath, `export default ${JSON.stringify(config)}`); } const root = (_b = (_a = config.build) === null || _a === void 0 ? void 0 : _a.root) !== null && _b !== void 0 ? _b : "src"; const categories = ["commands", "components", "events"]; const files = await Promise.all(categories.map((d) => { var _a; return crawlDir(root, d, (_a = config.build) === null || _a === void 0 ? void 0 : _a.extensions); })); const entriesPath = ".dressed/tmp/entries.ts"; writeFileSync(entriesPath, [files.map((c) => c.map(importString)), categoryExports(files)].flat(2).join("")); logDefer("Bundling handlers"); await bundle(entriesPath, ".dressed/tmp"); const { commands, components, events } = await import(__rewriteRelativeImportExtension(resolve(entriesPath.replace(".ts", ".mjs")))); console.log(); // This just adds a newline before the logged trees for consistency return { commands: parseCommands(commands, `${root}/commands`), components: parseComponents(components, `${root}/components`), events: parseEvents(events, `${root}/events`), config, }; } async function fetchMissingVars() { try { void botEnv.DISCORD_TOKEN; const missingVars = []; try { void botEnv.DISCORD_APP_ID; } catch (_a) { missingVars.push("DISCORD_APP_ID"); } try { void botEnv.DISCORD_PUBLIC_KEY; } catch (_b) { missingVars.push("DISCORD_PUBLIC_KEY"); } if (missingVars.length) { logDefer(`Fetching missing variables (${missingVars.join(", ")})`); const app = await getApp(); const envLines = [ "# Some required bot variables were missing, so they've been filled in automatically", ]; if (missingVars.includes("DISCORD_APP_ID")) { envLines.push(`DISCORD_APP_ID="${app.id}"`); } if (missingVars.includes("DISCORD_PUBLIC_KEY")) { envLines.push(`DISCORD_PUBLIC_KEY="${app.verify_key}"`); } appendFileSync(".env", `\n${envLines.join("\n")}`); } } catch (_c) { logError("Failed to fetch missing variables"); } } //# sourceMappingURL=build.js.map