This repository has been archived by the owner on Jan 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
99 lines (85 loc) · 3.3 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { Bot } from './deps.ts';
// iMpOrTaNt to make sure files can be reloaded properly!
export let uniqueFilePathCounter = 0;
export let paths: string[] = [];
/** Recursively generates an array of unique paths to import using `fileLoader()`
* (**Is** windows compatible)
*/
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;
}
// Recursive function!
await importDirectory(currentPath);
}
uniqueFilePathCounter++;
}
/** Writes, then imports all everything in fileloader.ts */
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 = [];
}
/** This function will import the specified directories */
export async function fastFileLoader(
/** An array of directories to import recursively. */
paths: string[],
/** A function that will run before recursively setting a part of `paths`.
* `path` contains the path that will be imported, useful for logging
*/
between?: (path: string, uniqueFilePathCounter: number, paths: string[]) => void,
/** A function that runs before **actually** importing all the files. */
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();
}
/** Extend the Bot with the Plugin's added functions */
export interface BotWithFileLoader extends Bot {
/** Recursively generates an array of unique paths to import using `fileLoader()`
* (**Is** windows compatible)
*/
importDirectory: (path: string) => void,
/** Writes, then imports all everything in fileloader.ts */
fileLoader: () => void,
/** This function will import the specified directories */
fastFileLoader: (
/** An array of directories to import recursively. */
paths: string[],
/** A function that will run before recursively setting a part of `paths`.
* `path` contains the path that will be imported, useful for logging
*/
between?: (path: string, uniqueFilePathCounter: number, paths: string[]) => void,
/** A function that runs before **actually** importing all the files. */
before?: (uniqueFilePathCounter: number, paths: string[]) => void
) => void,
}
/** Pass in a (compatible) bot instance, and get sweet file loader goodness.
* Remember to capture the output of this function!
*/
export function enableFileLoaderPlugin(rawBot: Bot): BotWithFileLoader {
const bot = rawBot as BotWithFileLoader;
bot.importDirectory = importDirectory;
bot.fileLoader = fileLoader;
bot.fastFileLoader = fastFileLoader;
return bot;
}