dressed
Version:
A sleek, serverless-ready Discord bot framework.
128 lines • 4.87 kB
JavaScript
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 { walkFiles } from "walk-it";
import ora from "ora";
import { appendFileSync, existsSync, writeFileSync, readdirSync, mkdirSync, } from "node:fs";
import { cwd, stdout } from "node:process";
import { basename, extname, relative, resolve } from "node:path";
import { parseCommands } from "./parsers/commands.js";
import { parseComponents } from "./parsers/components.js";
import { parseEvents } from "./parsers/events.js";
import { botEnv } from "../../utils/env.js";
import { getApp } from "../../resources/application.js";
import bundleFile from "./bundle.js";
import { pathToFileURL } from "node:url";
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;
}
/**
* Builds the bot imports and other variables.
*/
export default async function build(config = {}) {
mkdirSync(".dressed/cache", { recursive: true });
await fetchMissingVars();
const configPath = readdirSync(".").find((f) => basename(f, extname(f)) === "dressed.config");
if (configPath) {
await bundleFile({
path: configPath,
outPath: ".dressed/cache/config.mjs",
});
const { default: importedConfig } = await import(__rewriteRelativeImportExtension(pathToFileURL(".dressed/cache/config.mjs").href));
config = override(importedConfig, config);
}
else {
writeFileSync(".dressed/cache/config.mjs", `export default ${JSON.stringify(config)}`);
}
const [commandFiles, componentFiles, eventFiles] = await Promise.all([
fetchFiles(config.build, "commands"),
fetchFiles(config.build, "components"),
fetchFiles(config.build, "events"),
]);
const commands = await parseCommands(commandFiles);
const components = await parseComponents(componentFiles);
const events = await parseEvents(eventFiles);
return { commands, components, events, config };
}
async function fetchFiles(config = {}, dirName) {
var _a;
const dirPath = resolve((_a = config.root) !== null && _a !== void 0 ? _a : "src", dirName);
if (!existsSync(dirPath)) {
ora(`${dirName.slice(0, 1).toUpperCase() + dirName.slice(1)} directory not found`).warn();
return [];
}
const filesArray = [];
for await (const file of walkFiles(dirPath, {
filterFile: (f) => {
var _a;
return ((_a = config.extensions) !== null && _a !== void 0 ? _a : ["js", "ts", "mjs"]).includes(extname(f.name).slice(1));
},
})) {
const relativePath = relative(cwd(), file.path);
filesArray.push({
name: basename(file.file.name, extname(file.file.name)),
path: relativePath,
});
}
return filesArray;
}
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) {
const varLoader = ora({
stream: stdout,
text: `Fetching missing variables (${missingVars.join(", ")})`,
}).start();
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")}`);
varLoader.succeed(`Fetched missing variables (${missingVars.join(", ")})`);
}
}
catch (_c) {
//
}
}
//# sourceMappingURL=build.js.map