mirror of
https://github.com/Kir-Antipov/mc-publish.git
synced 2024-11-22 08:20:58 -05:00
Made ModuleLoader
interface
And added 2 default implementations: - `NODE_MODULE_LOADER` - uses `import` under the hood - `DYNAMIC_MODULE_LOADER` - will be able to load modules needed in other parts of this project. Currently it's just a stub
This commit is contained in:
parent
fd2975b30b
commit
491aa11ac5
3 changed files with 45 additions and 0 deletions
3
src/utils/reflection/module-loader.g.ts
Normal file
3
src/utils/reflection/module-loader.g.ts
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
export const ACTION_MODULE_LOADER = (path: string): Promise<Record<string, unknown>> => {
|
||||||
|
return Promise.resolve(undefined);
|
||||||
|
};
|
25
src/utils/reflection/module-loader.ts
Normal file
25
src/utils/reflection/module-loader.ts
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
import { ACTION_MODULE_LOADER } from "./module-loader.g";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a function that loads a module by its name.
|
||||||
|
*/
|
||||||
|
export interface ModuleLoader {
|
||||||
|
/**
|
||||||
|
* Loads a module by its name.
|
||||||
|
*
|
||||||
|
* @param name - The name of the module to load.
|
||||||
|
*
|
||||||
|
* @returns A promise that resolves with the loaded module.
|
||||||
|
*/
|
||||||
|
(name: string): Promise<unknown>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A module loader implementation that loads modules using Node.js dynamic `import` syntax.
|
||||||
|
*/
|
||||||
|
export const NODE_MODULE_LOADER: ModuleLoader = new Function("x", "return import(x).catch(() => undefined)") as (name: string) => Promise<unknown>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a dynamic module loader that is capable of loading modules by their source path (e.g., `"utils/string-utils"`).
|
||||||
|
*/
|
||||||
|
export const DYNAMIC_MODULE_LOADER: ModuleLoader = ACTION_MODULE_LOADER;
|
17
tests/unit/utils/reflection/module-loader.spec.ts
Normal file
17
tests/unit/utils/reflection/module-loader.spec.ts
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
import { NODE_MODULE_LOADER, DYNAMIC_MODULE_LOADER } from "@/utils/reflection/module-loader";
|
||||||
|
|
||||||
|
describe("NODE_MODULE_LOADER", () => {
|
||||||
|
test("returns undefined if a module cannot be loaded", async () => {
|
||||||
|
const loadedModule = await NODE_MODULE_LOADER("#");
|
||||||
|
|
||||||
|
expect(loadedModule).toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("DYNAMIC_MODULE_LOADER", () => {
|
||||||
|
test("returns undefined if a module cannot be loaded", async () => {
|
||||||
|
const loadedModule = await DYNAMIC_MODULE_LOADER("#");
|
||||||
|
|
||||||
|
expect(loadedModule).toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in a new issue