Compare commits

..

1 Commits

Author SHA1 Message Date
xxDeveloper
7cc16ff791 chore: Update LICENSE year 2024-08-06 16:07:43 +03:00
4 changed files with 10 additions and 14 deletions

View File

@@ -1,6 +1,7 @@
import path from 'node:path';
import { type Dirent, existsSync } from 'node:fs';
import { existsSync } from 'node:fs';
import { readdir } from 'fs/promises';
import assert from 'node:assert';
import * as Id from './id'
import { Module } from '../types/core-modules';
@@ -37,9 +38,8 @@ export async function importModule<T>(absPath: string) {
let fileModule = await import(absPath);
let commandModule: Module = fileModule.default;
if(!commandModule) {
throw Error(`No default export @ ${absPath}`);
}
assert(commandModule , `No default export @ ${absPath}`);
if ('default' in commandModule) {
commandModule = commandModule.default as Module;
}
@@ -53,18 +53,16 @@ export async function importModule<T>(absPath: string) {
}
export async function* readRecursive(dir: string, directoryPlugins: string[] = []): AsyncGenerator<[string, string[]]> {
export async function* readRecursive(dir: string): AsyncGenerator<string> {
const files = await readdir(dir, { withFileTypes: true });
const pluginFile = files.find(file => file.isFile() && file.name.startsWith('!plugins')) as Dirent;
for (const file of files) {
const fullPath = path.posix.join(dir, file.name);
const plugins = pluginFile ? [...directoryPlugins, path.posix.join(dir, pluginFile.name) ] : directoryPlugins
if (file.isDirectory()) {
if (!file.name.startsWith('!')) {
yield* readRecursive(fullPath, plugins);
yield* readRecursive(fullPath);
}
} else if (!file.name.startsWith('!')) {
yield ["file:///"+path.resolve(fullPath), plugins];
yield "file:///"+path.resolve(fullPath);
}
}
}

View File

@@ -17,8 +17,7 @@ export default async function(dir: string, deps : UnpackedDependencies) {
// https://observablehq.com/@ehouais/multiple-promises-as-an-async-generator
// possibly optimize to concurrently import modules
for await (const [path, directoryPlugins] of Files.readRecursive(dir)) {
console.log(path, directoryPlugins)
for await (const path of Files.readRecursive(dir)) {
let { module } = await Files.importModule<Module>(path);
const validType = module.type >= CommandType.Text && module.type <= CommandType.ChannelSelect;
if(!validType) {

View File

@@ -6,7 +6,7 @@ import { fileURLToPath } from "url";
export const registerTasks = async (tasksPath: string, deps: UnpackedDependencies) => {
const taskManager = deps['@sern/scheduler']
for await (const [f, _] of Files.readRecursive(tasksPath)) {
for await (const f of Files.readRecursive(tasksPath)) {
let { module } = await Files.importModule<ScheduledTask>(f);
//module.name is assigned by Files.importModule<>
// the id created for the task is unique

View File

@@ -20,8 +20,7 @@ const intoDispatcher = (deps: UnpackedDependencies) =>
export default async function(deps: UnpackedDependencies, eventDir: string) {
const eventModules: EventModule[] = [];
for await (const [path, _] of Files.readRecursive(eventDir)) {
console.log(path, _)
for await (const path of Files.readRecursive(eventDir)) {
let { module } = await Files.importModule<Module>(path);
await callInitPlugins(module, deps)
eventModules.push(module as EventModule);