import { Bot } from "./deps.ts";
export let uniqueFilePathCounter = 0;export let paths: string[] = [];
export async function importDirectory(path: string) { path = path.replaceAll("\\", "/"); const files = Deno.readDirSync(Deno.realPathSync(path));
for (const file of files) { if (!file.name) continue;
const currentPath = `${path}/${file.name}`; if (file.isFile) { if (!currentPath.endsWith(".ts")) continue; paths.push( `import "${Deno.mainModule.substring(0, Deno.mainModule.lastIndexOf("/"))}/${ currentPath.substring( currentPath.indexOf("src/"), ) }#${uniqueFilePathCounter}";`, ); continue; }
await importDirectory(currentPath); }
uniqueFilePathCounter++;}
export async function fileLoader() { await Deno.writeTextFile("fileloader.ts", paths.join("\n").replaceAll("\\", "/")); await import( `${Deno.mainModule.substring(0, Deno.mainModule.lastIndexOf("/"))}/fileloader.ts#${uniqueFilePathCounter}` ); paths = [];}
export async function fastFileLoader( paths: string[], between?: (path: string, uniqueFilePathCounter: number, paths: string[]) => void, before?: (uniqueFilePathCounter: number, paths: string[]) => void,) { await Promise.all( [...paths].map((path) => { if (between) between(path, uniqueFilePathCounter, paths); importDirectory(path); }), );
if (before) before(uniqueFilePathCounter, paths);
await fileLoader();}
export interface BotWithFileLoader extends Bot { importDirectory: (path: string) => void; fileLoader: () => void; fastFileLoader: ( paths: string[], between?: (path: string, uniqueFilePathCounter: number, paths: string[]) => void, before?: (uniqueFilePathCounter: number, paths: string[]) => void, ) => void;}
export function enableFileLoaderPlugin(rawBot: Bot): BotWithFileLoader { const bot = rawBot as BotWithFileLoader;
bot.importDirectory = importDirectory; bot.fileLoader = fileLoader; bot.fastFileLoader = fastFileLoader;
return bot;}