Skip to content

Commit

Permalink
feat(core): introduce process specific application configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
faustbrian authored Nov 14, 2019
1 parent acc5e16 commit 1a4e132
Show file tree
Hide file tree
Showing 17 changed files with 135 additions and 285 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const setUp = async (): Promise<Contracts.Kernel.Application> => {
token: "ark",
network: "unitnet",
env: "test",
processType: "core",
},
plugins: {
include: [
Expand Down
1 change: 1 addition & 0 deletions __tests__/integration/core-api/__support__/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const setUp = async () => {
token: "ark",
network: "unitnet",
env: "test",
processType: "core",
},
plugins: {
exclude: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import "jest-extended";

import { Application } from "@packages/core-kernel/src/application";
import { Container, interfaces, Identifiers } from "@packages/core-kernel/src/ioc";
import { ConfigRepository } from "@packages/core-kernel/src/services/config";
import {
ServiceProvider,
ServiceProviderRepository,
Expand Down Expand Up @@ -64,30 +63,6 @@ describe("RegisterServiceProviders", () => {
expect(spyRegister).toHaveBeenCalled();
});

it("should respect the include configuration", async () => {
app.get<ConfigRepository>(Identifiers.ConfigRepository).set("app.pluginOptions.include", ["stub-other"]);

const serviceProvider: ServiceProvider = new StubServiceProvider();
const spyRegister = jest.spyOn(serviceProvider, "register");
serviceProviderRepository.set("stub", serviceProvider);

await app.resolve<RegisterServiceProviders>(RegisterServiceProviders).bootstrap();

expect(spyRegister).not.toHaveBeenCalled();
});

it("should respect the exclude configuration", async () => {
app.get<ConfigRepository>(Identifiers.ConfigRepository).set("app.pluginOptions.exclude", ["stub"]);

const serviceProvider: ServiceProvider = new StubServiceProvider();
const spyRegister = jest.spyOn(serviceProvider, "register");
serviceProviderRepository.set("stub", serviceProvider);

await app.resolve<RegisterServiceProviders>(RegisterServiceProviders).bootstrap();

expect(spyRegister).not.toHaveBeenCalled();
});

it("should bootstrap if the configuration validation passes", async () => {
const serviceProvider: ServiceProvider = new ValidConfigurationServiceProvider();
serviceProvider.setManifest(app.resolve(PluginManifest));
Expand Down
20 changes: 5 additions & 15 deletions __tests__/unit/core/common/__fixtures__/app.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
module.exports = {
cli: {
forger: {
run: {
plugins: {
include: ["@arkecosystem/core-forger"],
},
},
},
relay: {
run: {
plugins: {
exclude: ["@arkecosystem/core-forger"],
},
},
},
forger: {
plugins: [{ package: "@arkecosystem/core-forger" }],
},
relay: {
plugins: [{ package: "@arkecosystem/core-forger" }],
},
};
71 changes: 0 additions & 71 deletions __tests__/unit/core/common/config.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion __tests__/unit/core/common/process.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe("restartRunningProcessWithPrompt", () => {
spyRestart.mockClear();
});

it.only("should restart the process", async () => {
it("should restart the process", async () => {
const spyOnline = jest.spyOn(processManager, "isOnline").mockReturnValue(true);
const spyRestart = jest.spyOn(processManager, "restart").mockImplementation(undefined);

Expand Down
8 changes: 4 additions & 4 deletions packages/core-kernel/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ export class Application implements Contracts.Kernel.Application {
}

/**
* @param {{ flags: JsonObject; plugins?: JsonObject }} { flags, plugins }
* @param {{ flags: JsonObject; plugins: JsonObject; }} options
* @returns {Promise<void>}
* @memberof Application
*/
public async bootstrap({ flags, plugins }: { flags: JsonObject; plugins?: JsonObject }): Promise<void> {
this.bind<KeyValuePair>(Identifiers.ConfigFlags).toConstantValue(flags);
this.bind<KeyValuePair>(Identifiers.ConfigPlugins).toConstantValue(plugins || {});
public async bootstrap(options: { flags: JsonObject; plugins?: JsonObject }): Promise<void> {
this.bind<KeyValuePair>(Identifiers.ConfigFlags).toConstantValue(options.flags);
this.bind<KeyValuePair>(Identifiers.ConfigPlugins).toConstantValue(options.plugins || {});

await this.registerEventDispatcher();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
} from "../../exceptions/plugins";
import { Identifiers, inject, injectable } from "../../ioc";
import { PluginConfiguration, ServiceProvider, ServiceProviderRepository } from "../../providers";
import { ConfigRepository } from "../../services/config";
import { ValidationManager } from "../../services/validation";
import { assert } from "../../utils";
import { Bootstrapper } from "../interfaces";
Expand Down Expand Up @@ -49,11 +48,6 @@ export class RegisterServiceProviders implements Bootstrapper {
assert.defined<string>(serviceProviderName);

try {
// Shall we include the plugin?
if (!this.shouldBeIncluded(serviceProviderName) || this.shouldBeExcluded(serviceProviderName)) {
continue;
}

// Does the configuration conform to the given rules?
await this.validateConfiguration(serviceProvider);

Expand Down Expand Up @@ -178,32 +172,4 @@ export class RegisterServiceProviders implements Bootstrapper {

return true;
}

/**
* @private
* @param {string} name
* @returns {boolean}
* @memberof RegisterServiceProviders
*/
private shouldBeIncluded(name: string): boolean {
const includes: string[] | undefined = this.app
.get<ConfigRepository>(Identifiers.ConfigRepository)
.get<string[]>("app.pluginOptions.include", []);

return includes && includes.length > 0 ? includes.includes(name) : true;
}

/**
* @private
* @param {string} name
* @returns {boolean}
* @memberof RegisterServiceProviders
*/
private shouldBeExcluded(name: string): boolean {
const excludes: string[] | undefined = this.app
.get<ConfigRepository>(Identifiers.ConfigRepository)
.get<string[]>("app.pluginOptions.exclude", []);

return excludes && excludes.length > 0 ? excludes.includes(name) : false;
}
}
2 changes: 0 additions & 2 deletions packages/core-kernel/src/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export const defaults = {
flags: {},
services: {
log: {
levels: {
Expand All @@ -11,5 +10,4 @@ export const defaults = {
},
},
},
plugins: [],
};
4 changes: 3 additions & 1 deletion packages/core-kernel/src/providers/plugin-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ export class PluginConfiguration {
public discover(name: string): this {
try {
this.items = require(`${name}/dist/defaults.js`).defaults;
} catch {}
} catch {
// Failed to discover the defaults configuration file. This can be intentional.
}

this.mergeWithGlobal(name);

Expand Down
12 changes: 8 additions & 4 deletions packages/core-kernel/src/services/config/drivers/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,21 @@ export class LocalConfigLoader implements ConfigLoader {
* @memberof LocalConfigLoader
*/
private loadApplication(): void {
const processType: string = this.app.get<KeyValuePair>(Identifiers.ConfigFlags).processType;

const config = this.loadFromLocation(["app.json", "app.js"]);

this.configRepository.set("app.flags", {
...this.app.get<JsonObject>(Identifiers.ConfigFlags),
...defaults.flags,
...get(config, "flags", {}),
...get(config, `${processType}.flags`, {}),
});

this.configRepository.set("app.services", { ...defaults.services, ...get(config, "services", {}) });
this.configRepository.set("app.services", {
...defaults.services,
...get(config, `${processType}.services`, {}),
});

this.configRepository.set("app.plugins", [...defaults.plugins, ...get(config, "plugins", [] as any)]);
this.configRepository.set("app.plugins", get(config, `${processType}.plugins`, []));
}

/**
Expand Down
Loading

0 comments on commit 1a4e132

Please sign in to comment.