Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: feat: impl RunnerOptions.ModuleConfigUtil #137

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 29 additions & 15 deletions standalone/standalone/src/Runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface RunnerOptions {
innerObjects?: Record<string, object>;

innerObjectHandlers?: Record<string, InnerObject[]>;
ModuleConfigUtil?: typeof ModuleConfigUtil;
}

export class Runner {
Expand All @@ -46,27 +47,42 @@ export class Runner {

constructor(cwd: string, options?: RunnerOptions) {
this.cwd = cwd;
this.moduleReferences = ModuleConfigUtil.readModuleReference(this.cwd);
this.moduleConfigs = {};
this.innerObjects = {
moduleConfigs: [{
obj: new ModuleConfigs(this.moduleConfigs),
}],
moduleConfig: [],
};
this.moduleReferences = this.#loadModuleReferences(options?.ModuleConfigUtil || ModuleConfigUtil);
this.moduleConfigs = this.#loadConfig(options?.ModuleConfigUtil || ModuleConfigUtil);
this.#prepareInnerObjects(options);
this.loadUnitLoader = new EggModuleLoader(this.moduleReferences);
const configSourceEggPrototypeHook = new ConfigSourceLoadUnitHook();
LoadUnitLifecycleUtil.registerLifecycle(configSourceEggPrototypeHook);
}

#loadModuleReferences(moduleConfigUtil: typeof ModuleConfigUtil) {
return moduleConfigUtil.readModuleReference(this.cwd);
}

#loadConfig(moduleConfigUtil: typeof ModuleConfigUtil) {
const moduleConfigs = {};
for (const reference of this.moduleReferences) {
const absoluteRef = {
path: ModuleConfigUtil.resolveModuleDir(reference.path, this.cwd),
path: moduleConfigUtil.resolveModuleDir(reference.path, this.cwd),
};

const moduleName = ModuleConfigUtil.readModuleNameSync(absoluteRef.path);
this.moduleConfigs[moduleName] = {
const moduleName = moduleConfigUtil.readModuleNameSync(absoluteRef.path);
moduleConfigs[moduleName] = {
name: moduleName,
reference: absoluteRef,
config: ModuleConfigUtil.loadModuleConfigSync(absoluteRef.path) || {},
config: moduleConfigUtil.loadModuleConfigSync(absoluteRef.path) || {},
};
}
return moduleConfigs;
}

#prepareInnerObjects(options?: RunnerOptions) {
this.innerObjects = {
moduleConfigs: [{
obj: new ModuleConfigs(this.moduleConfigs),
}],
moduleConfig: [],
};
for (const moduleConfig of Object.values(this.moduleConfigs)) {
this.innerObjects.moduleConfig.push({
obj: moduleConfig.config,
Expand All @@ -76,6 +92,7 @@ export class Runner {
}],
});
}

if (options?.innerObjects) {
for (const [ name, obj ] of Object.entries(options.innerObjects)) {
this.innerObjects[name] = [{
Expand All @@ -85,9 +102,6 @@ export class Runner {
} else if (options?.innerObjectHandlers) {
Object.assign(this.innerObjects, options.innerObjectHandlers);
}
this.loadUnitLoader = new EggModuleLoader(this.moduleReferences);
const configSourceEggPrototypeHook = new ConfigSourceLoadUnitHook();
LoadUnitLifecycleUtil.registerLifecycle(configSourceEggPrototypeHook);
}

async init() {
Expand Down