diff --git a/packages/desktop-core/src/main/commands/init/handler.ts b/packages/desktop-core/src/main/commands/init/handler.ts index 2efc7c7c..9cb4a731 100644 --- a/packages/desktop-core/src/main/commands/init/handler.ts +++ b/packages/desktop-core/src/main/commands/init/handler.ts @@ -22,7 +22,7 @@ export const handler = async (options: IInitOptions) => { logger.verbose(`Creating config at ${configurationPath}`); - const defaultConfig = await configurationGenerator.generate(kind, name, url); + const defaultConfig = await configurationGenerator.generate({ kind, name, url }); await configurationWriter.write(configurationPath, defaultConfig); diff --git a/packages/desktop-core/src/main/commands/start/handler.ts b/packages/desktop-core/src/main/commands/start/handler.ts index cfafecee..f966e577 100644 --- a/packages/desktop-core/src/main/commands/start/handler.ts +++ b/packages/desktop-core/src/main/commands/start/handler.ts @@ -47,7 +47,11 @@ export const handler = async (options: IStartOptions) => { dictionaries: [adjectives, colors, animals], }); - const configuration = await configurationGenerator.generate(ConfigurationKind.Application, name, url); + const configuration = await configurationGenerator.generate({ + kind: ConfigurationKind.Application, + name, + url, + }); await registryService.register(configuration); diff --git a/packages/desktop-core/src/main/configuration/generators/__tests__/applicationConfigurationGenerator.test.ts b/packages/desktop-core/src/main/configuration/generators/__tests__/applicationConfigurationGenerator.test.ts index 3cadfcf6..3a45741f 100644 --- a/packages/desktop-core/src/main/configuration/generators/__tests__/applicationConfigurationGenerator.test.ts +++ b/packages/desktop-core/src/main/configuration/generators/__tests__/applicationConfigurationGenerator.test.ts @@ -29,7 +29,11 @@ describe("generate", () => { test("should generate application config", async () => { const generator = new ApplicationConfigurationGenerator(); - const configuration = await generator.generate(ConfigurationKind.Application, "test", "http://url"); + const configuration = await generator.generate({ + kind: ConfigurationKind.Application, + name: "test", + url: "http://url", + }); expect(configuration.kind).toBe(ConfigurationKind.Application); }); @@ -37,7 +41,11 @@ describe("generate", () => { test("should set namespace to default", async () => { const generator = new ApplicationConfigurationGenerator(); - const configuration = await generator.generate(ConfigurationKind.Application, "test", "http://url"); + const configuration = await generator.generate({ + kind: ConfigurationKind.Application, + name: "test", + url: "http://url", + }); expect(configuration.metadata.namespace).toBe(WellKnownNamespaces.default); }); diff --git a/packages/desktop-core/src/main/configuration/generators/__tests__/externalConfigurationGenerator.test.ts b/packages/desktop-core/src/main/configuration/generators/__tests__/externalConfigurationGenerator.test.ts index 3a9c0bcc..0289a528 100644 --- a/packages/desktop-core/src/main/configuration/generators/__tests__/externalConfigurationGenerator.test.ts +++ b/packages/desktop-core/src/main/configuration/generators/__tests__/externalConfigurationGenerator.test.ts @@ -29,7 +29,7 @@ describe("generate", () => { test("should generate external config", async () => { const generator = new ExternalConfigurationGenerator(); - const configuration = await generator.generate(ConfigurationKind.External, "test"); + const configuration = await generator.generate({ kind: ConfigurationKind.External, name: "test" }); expect(configuration.kind).toBe(ConfigurationKind.External); }); @@ -37,7 +37,7 @@ describe("generate", () => { test("should set namespace to default", async () => { const generator = new ExternalConfigurationGenerator(); - const configuration = await generator.generate(ConfigurationKind.External, "test"); + const configuration = await generator.generate({ kind: ConfigurationKind.External, name: "test" }); expect(configuration.metadata.namespace).toBe(WellKnownNamespaces.default); }); diff --git a/packages/desktop-core/src/main/configuration/generators/__tests__/protocolResolvingConfigurationGenerator.test.ts b/packages/desktop-core/src/main/configuration/generators/__tests__/protocolResolvingConfigurationGenerator.test.ts index 84d97092..e47db58c 100644 --- a/packages/desktop-core/src/main/configuration/generators/__tests__/protocolResolvingConfigurationGenerator.test.ts +++ b/packages/desktop-core/src/main/configuration/generators/__tests__/protocolResolvingConfigurationGenerator.test.ts @@ -28,7 +28,7 @@ describe("generate", () => { const protocol = new ProtocolResolvingConfigurationGenerator(generator); - const configuration = await protocol.generate(ConfigurationKind.Application, "name", url); + const configuration = await protocol.generate({ kind: ConfigurationKind.Application, name: "name", url }); const application = configuration.spec as IApplicationSpecification; diff --git a/packages/desktop-core/src/main/configuration/generators/__tests__/serviceConfigurationGenerator.test.ts b/packages/desktop-core/src/main/configuration/generators/__tests__/serviceConfigurationGenerator.test.ts index fc0c2517..2bb8829b 100644 --- a/packages/desktop-core/src/main/configuration/generators/__tests__/serviceConfigurationGenerator.test.ts +++ b/packages/desktop-core/src/main/configuration/generators/__tests__/serviceConfigurationGenerator.test.ts @@ -29,7 +29,7 @@ describe("generate", () => { test("should generate service config", async () => { const generator = new ServiceConfigurationGenerator(); - const configuration = await generator.generate(ConfigurationKind.Service, "test"); + const configuration = await generator.generate({ kind: ConfigurationKind.Service, name: "test" }); expect(configuration.kind).toBe(ConfigurationKind.Service); }); @@ -37,7 +37,7 @@ describe("generate", () => { test("should set namespace to default", async () => { const generator = new ServiceConfigurationGenerator(); - const configuration = await generator.generate(ConfigurationKind.Service, "test"); + const configuration = await generator.generate({ kind: ConfigurationKind.Service, name: "test" }); expect(configuration.metadata.namespace).toBe(WellKnownNamespaces.default); }); diff --git a/packages/desktop-core/src/main/configuration/generators/applicationConfigurationGenerator.ts b/packages/desktop-core/src/main/configuration/generators/applicationConfigurationGenerator.ts index c20404f0..3d002914 100644 --- a/packages/desktop-core/src/main/configuration/generators/applicationConfigurationGenerator.ts +++ b/packages/desktop-core/src/main/configuration/generators/applicationConfigurationGenerator.ts @@ -1,12 +1,13 @@ import { ConfigurationKind, IConfiguration, WellKnownNamespaces } from "@reactivemarkets/desktop-types"; import { IConfigurationGenerator } from "./iConfigurationGenerator"; +import { IGeneratorOptions } from "./iGeneratorOptions"; export class ApplicationConfigurationGenerator implements IConfigurationGenerator { public canGenerate = (kind: ConfigurationKind) => { return kind === ConfigurationKind.Application; }; - public generate = async (_: ConfigurationKind, name: string, url: string): Promise => { + public generate = async ({ name, url }: IGeneratorOptions): Promise => { return Promise.resolve({ kind: ConfigurationKind.Application, metadata: { @@ -15,7 +16,7 @@ export class ApplicationConfigurationGenerator implements IConfigurationGenerato namespace: WellKnownNamespaces.default, }, spec: { - url, + url: url!, window: { height: 600, width: 800, diff --git a/packages/desktop-core/src/main/configuration/generators/compositeConfigurationGenerator.ts b/packages/desktop-core/src/main/configuration/generators/compositeConfigurationGenerator.ts index 4653c27b..90ef8e5f 100644 --- a/packages/desktop-core/src/main/configuration/generators/compositeConfigurationGenerator.ts +++ b/packages/desktop-core/src/main/configuration/generators/compositeConfigurationGenerator.ts @@ -1,5 +1,6 @@ import { ConfigurationKind, IConfiguration } from "@reactivemarkets/desktop-types"; import { IConfigurationGenerator } from "./iConfigurationGenerator"; +import { IGeneratorOptions } from "./iGeneratorOptions"; export class CompositeConfigurationGenerator implements IConfigurationGenerator { private readonly generators: IConfigurationGenerator[]; @@ -12,7 +13,8 @@ export class CompositeConfigurationGenerator implements IConfigurationGenerator return this.generators.some((g) => g.canGenerate(kind)); } - public async generate(kind: ConfigurationKind, name: string, url: string): Promise { + public async generate(options: IGeneratorOptions): Promise { + const { kind } = options; const generator = this.generators.find((g) => g.canGenerate(kind)); if (generator === undefined) { const error = new Error(`no generator for ${kind}`); @@ -20,6 +22,6 @@ export class CompositeConfigurationGenerator implements IConfigurationGenerator return Promise.reject(error); } - return generator.generate(kind, name, url); + return generator.generate(options); } } diff --git a/packages/desktop-core/src/main/configuration/generators/externalConfigurationGenerator.ts b/packages/desktop-core/src/main/configuration/generators/externalConfigurationGenerator.ts index 8633094b..8d7b97f7 100644 --- a/packages/desktop-core/src/main/configuration/generators/externalConfigurationGenerator.ts +++ b/packages/desktop-core/src/main/configuration/generators/externalConfigurationGenerator.ts @@ -1,12 +1,13 @@ import { ConfigurationKind, IConfiguration, WellKnownNamespaces } from "@reactivemarkets/desktop-types"; import { IConfigurationGenerator } from "./iConfigurationGenerator"; +import { IGeneratorOptions } from "./iGeneratorOptions"; export class ExternalConfigurationGenerator implements IConfigurationGenerator { public canGenerate = (kind: ConfigurationKind) => { return kind === ConfigurationKind.External; }; - public generate = async (_: ConfigurationKind, name: string): Promise => { + public generate = async ({ name }: IGeneratorOptions): Promise => { return Promise.resolve({ kind: ConfigurationKind.External, metadata: { diff --git a/packages/desktop-core/src/main/configuration/generators/iConfigurationGenerator.ts b/packages/desktop-core/src/main/configuration/generators/iConfigurationGenerator.ts index 2c0d8329..733e2ea0 100644 --- a/packages/desktop-core/src/main/configuration/generators/iConfigurationGenerator.ts +++ b/packages/desktop-core/src/main/configuration/generators/iConfigurationGenerator.ts @@ -1,6 +1,7 @@ import { ConfigurationKind, IConfiguration } from "@reactivemarkets/desktop-types"; +import { IGeneratorOptions } from "./iGeneratorOptions"; export interface IConfigurationGenerator { canGenerate(kind: ConfigurationKind): boolean; - generate(kind: ConfigurationKind, name: string, url: string): Promise; + generate(options: IGeneratorOptions): Promise; } diff --git a/packages/desktop-core/src/main/configuration/generators/iGeneratorOptions.ts b/packages/desktop-core/src/main/configuration/generators/iGeneratorOptions.ts new file mode 100644 index 00000000..2e907bde --- /dev/null +++ b/packages/desktop-core/src/main/configuration/generators/iGeneratorOptions.ts @@ -0,0 +1,7 @@ +import { ConfigurationKind } from "@reactivemarkets/desktop-types"; + +export interface IGeneratorOptions { + readonly kind: ConfigurationKind; + readonly name: string; + readonly url?: string; +} diff --git a/packages/desktop-core/src/main/configuration/generators/protocolResolvingConfigurationGenerator.ts b/packages/desktop-core/src/main/configuration/generators/protocolResolvingConfigurationGenerator.ts index 7b24da11..973490d2 100644 --- a/packages/desktop-core/src/main/configuration/generators/protocolResolvingConfigurationGenerator.ts +++ b/packages/desktop-core/src/main/configuration/generators/protocolResolvingConfigurationGenerator.ts @@ -1,6 +1,7 @@ import { ConfigurationKind, IConfiguration } from "@reactivemarkets/desktop-types"; import { parse } from "url"; import { IConfigurationGenerator } from "./iConfigurationGenerator"; +import { IGeneratorOptions } from "./iGeneratorOptions"; export class ProtocolResolvingConfigurationGenerator implements IConfigurationGenerator { private readonly generator: IConfigurationGenerator; @@ -15,15 +16,15 @@ export class ProtocolResolvingConfigurationGenerator implements IConfigurationGe return this.generator.canGenerate(kind); }; - public generate = (kind: ConfigurationKind, name: string, url: string): Promise => { - let absoluteUrl = url; - const urlQuery = parse(url); + public generate = ({ name, kind, url }: IGeneratorOptions): Promise => { + let absoluteUrl = url!; + const urlQuery = parse(absoluteUrl); switch (urlQuery.protocol) { case null: absoluteUrl = `${this.protocol}://${url}`; break; } - return this.generator.generate(kind, name, absoluteUrl); + return this.generator.generate({ kind, name, url: absoluteUrl }); }; } diff --git a/packages/desktop-core/src/main/configuration/generators/serviceConfigurationGenerator.ts b/packages/desktop-core/src/main/configuration/generators/serviceConfigurationGenerator.ts index dce4d69a..4c743c26 100644 --- a/packages/desktop-core/src/main/configuration/generators/serviceConfigurationGenerator.ts +++ b/packages/desktop-core/src/main/configuration/generators/serviceConfigurationGenerator.ts @@ -1,12 +1,13 @@ import { ConfigurationKind, IConfiguration, WellKnownNamespaces } from "@reactivemarkets/desktop-types"; import { IConfigurationGenerator } from "./iConfigurationGenerator"; +import { IGeneratorOptions } from "./iGeneratorOptions"; export class ServiceConfigurationGenerator implements IConfigurationGenerator { public canGenerate = (kind: ConfigurationKind) => { return kind === ConfigurationKind.Service; }; - public generate = async (_: ConfigurationKind, name: string): Promise => { + public generate = async ({ name }: IGeneratorOptions): Promise => { return Promise.resolve({ kind: ConfigurationKind.Service, metadata: { diff --git a/packages/desktop-core/src/main/events/openUrlApplicationEventsHandler.ts b/packages/desktop-core/src/main/events/openUrlApplicationEventsHandler.ts index ae2c6356..06208752 100644 --- a/packages/desktop-core/src/main/events/openUrlApplicationEventsHandler.ts +++ b/packages/desktop-core/src/main/events/openUrlApplicationEventsHandler.ts @@ -7,17 +7,21 @@ import { configurationGenerator } from "../configuration"; import { launcherService } from "../launcher"; export const registerOpenUrlEventsHandler = (app: App) => { - app.on("open-url", async (event, url) => { + app.on("open-url", async (event, desktopUrl) => { try { event.preventDefault(); - const configUrl = url.replace("desktop://", "https://"); + const url = desktopUrl.replace("desktop://", "https://"); const name = uniqueNamesGenerator({ dictionaries: [adjectives, colors, animals], }); - const configuration = await configurationGenerator.generate(ConfigurationKind.Application, name, configUrl); + const configuration = await configurationGenerator.generate({ + kind: ConfigurationKind.Application, + name, + url, + }); await registryService.register(configuration);