Плагин автозагрузки
Плагин автозагрузки команд для GramIO с поддержкой Bun.build
.
Установка
bash
npm install @gramio/autoload
bash
yarn add @gramio/autoload
bash
pnpm add @gramio/autoload
bash
bun install @gramio/autoload
Использование
IMPORTANT
Пожалуйста, прочитайте о Ленивой загрузке плагинов
Регистрация плагина
ts
// index.ts
import { Bot } from "gramio";
import { autoload } from "@gramio/autoload";
const bot = new Bot(process.env.BOT_TOKEN as string)
.extend(await autoload())
.onStart(console.log);
bot.start();
export type BotType = typeof bot;
Создание команды
ts
// commands/command.ts
import type { BotType } from "..";
export default (bot: BotType) =>
bot.command("start", (context) => context.send("привет!"));
Опции
Ключ | Тип | По умолчанию | Описание |
---|---|---|---|
pattern? | string | string[] | "**/*.{ts,js,cjs,mjs}" | Шаблоны Glob |
path? | string | "./commands" | Путь к папке |
import? | string | (file: any) => string | "default" | Импорт конкретного export из файла |
failGlob? | boolean | true | Бросать ошибку, если не найдены совпадения |
skipImportErrors? | boolean | false | Пропускать импорты, где нужный export не определён |
onLoad? | (params: { absolute: string; relative: string }) => unknown | Хук, вызываемый при загрузке файла | |
onFinish? | (paths: { absolute: string; relative: string }[]) => unknown; | Хук, вызываемый после загрузки всех файлов | |
fdir? | Options | Настройки для fdir | |
picomatch? | PicomatchOptions | Настройки для picomatch |
Использование Bun build
Вы можете использовать этот плагин с Bun.build
благодаря esbuild-plugin-autoload!
ts
// @filename: build.ts
import { autoload } from "esbuild-plugin-autoload"; // также поддерживается импорт по умолчанию
await Bun.build({
entrypoints: ["src/index.ts"],
target: "bun",
outdir: "out",
plugins: [autoload("./src/commands")],
}).then(console.log);
Затем соберите с помощью bun build.ts
и запустите с помощью bun out/index.ts
.
Использование Bun compile
Вы можете собрать, а затем скомпилировать в единый исполняемый бинарный файл
ts
import { autoload } from "esbuild-plugin-autoload"; // также поддерживается импорт по умолчанию
await Bun.build({
entrypoints: ["src/index.ts"],
target: "bun",
outdir: "out",
plugins: [autoload("./src/commands")],
}).then(console.log);
await Bun.$`bun build --compile out/index.js`;
WARNING
Вы не можете использовать это в режиме bun build --compile
без дополнительного шага (Issue с функционалом)