Skip to content
This repository has been archived by the owner on Jul 26, 2023. It is now read-only.

Commit

Permalink
feat(core): configuration generator now takes an object
Browse files Browse the repository at this point in the history
This makes it easier to expand the generators.
  • Loading branch information
markmcdowell committed Jun 17, 2020
1 parent 1cb4f8a commit 607878e
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/desktop-core/src/main/commands/init/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 5 additions & 1 deletion packages/desktop-core/src/main/commands/start/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,23 @@ 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);
});

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);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ 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);
});

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);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ 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);
});

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);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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<IConfiguration> => {
public generate = async ({ name, url }: IGeneratorOptions): Promise<IConfiguration> => {
return Promise.resolve({
kind: ConfigurationKind.Application,
metadata: {
Expand All @@ -15,7 +16,7 @@ export class ApplicationConfigurationGenerator implements IConfigurationGenerato
namespace: WellKnownNamespaces.default,
},
spec: {
url,
url: url!,
window: {
height: 600,
width: 800,
Expand Down
Original file line number Diff line number Diff line change
@@ -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[];
Expand All @@ -12,14 +13,15 @@ export class CompositeConfigurationGenerator implements IConfigurationGenerator
return this.generators.some((g) => g.canGenerate(kind));
}

public async generate(kind: ConfigurationKind, name: string, url: string): Promise<IConfiguration> {
public async generate(options: IGeneratorOptions): Promise<IConfiguration> {
const { kind } = options;
const generator = this.generators.find((g) => g.canGenerate(kind));
if (generator === undefined) {
const error = new Error(`no generator for ${kind}`);

return Promise.reject(error);
}

return generator.generate(kind, name, url);
return generator.generate(options);
}
}
Original file line number Diff line number Diff line change
@@ -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<IConfiguration> => {
public generate = async ({ name }: IGeneratorOptions): Promise<IConfiguration> => {
return Promise.resolve({
kind: ConfigurationKind.External,
metadata: {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<IConfiguration>;
generate(options: IGeneratorOptions): Promise<IConfiguration>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ConfigurationKind } from "@reactivemarkets/desktop-types";

export interface IGeneratorOptions {
readonly kind: ConfigurationKind;
readonly name: string;
readonly url?: string;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,15 +16,15 @@ export class ProtocolResolvingConfigurationGenerator implements IConfigurationGe
return this.generator.canGenerate(kind);
};

public generate = (kind: ConfigurationKind, name: string, url: string): Promise<IConfiguration> => {
let absoluteUrl = url;
const urlQuery = parse(url);
public generate = ({ name, kind, url }: IGeneratorOptions): Promise<IConfiguration> => {
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 });
};
}
Original file line number Diff line number Diff line change
@@ -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<IConfiguration> => {
public generate = async ({ name }: IGeneratorOptions): Promise<IConfiguration> => {
return Promise.resolve({
kind: ConfigurationKind.Service,
metadata: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 607878e

Please sign in to comment.