diff --git a/packages/core/src/common/create-app.ts b/packages/core/src/common/create-app.ts new file mode 100644 index 0000000000..2696950ce7 --- /dev/null +++ b/packages/core/src/common/create-app.ts @@ -0,0 +1,17 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +import type { DiContainerForInjection } from "@ogre-tools/injectable"; + +export interface ApplicationConfig { + mode: string; +} + +export interface Application { + start: () => Promise; + readonly di: DiContainerForInjection; +} + +export type CreateApplication = (config: ApplicationConfig) => Application; diff --git a/packages/core/src/main/create-app.ts b/packages/core/src/main/create-app.ts index d0cccae5f8..57f764706f 100644 --- a/packages/core/src/main/create-app.ts +++ b/packages/core/src/main/create-app.ts @@ -3,20 +3,17 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ -import type { DiContainer } from "@ogre-tools/injectable"; import { getInjectable } from "@ogre-tools/injectable"; import { runInAction } from "mobx"; +import type { CreateApplication } from "../common/create-app"; import nodeEnvInjectionToken from "../common/vars/node-env-injection-token"; +import { getDi } from "./getDi"; import { registerInjectables } from "./register-injectables"; import startMainApplicationInjectable from "./start-main-application/start-main-application.injectable"; -interface AppConfig { - di: DiContainer; - mode: string; -} - -export function createApp(conf: AppConfig) { - const { di, mode } = conf; +export const createApplication: CreateApplication = (config) => { + const { mode } = config; + const di = getDi(); runInAction(() => { di.register(getInjectable({ @@ -28,9 +25,8 @@ export function createApp(conf: AppConfig) { registerInjectables(di); }); - const startMainApplication = di.inject(startMainApplicationInjectable); - return { - start: () => startMainApplication(), + start: di.inject(startMainApplicationInjectable), + di, }; -} +}; diff --git a/packages/core/src/main/library.ts b/packages/core/src/main/library.ts index a8b4507b69..ab1f56528e 100644 --- a/packages/core/src/main/library.ts +++ b/packages/core/src/main/library.ts @@ -8,7 +8,8 @@ export { afterApplicationIsLoadedInjectionToken } from "./start-main-application export { beforeApplicationIsLoadingInjectionToken } from "./start-main-application/runnable-tokens/before-application-is-loading-injection-token"; export { beforeElectronIsReadyInjectionToken } from "./start-main-application/runnable-tokens/before-electron-is-ready-injection-token"; export { onLoadOfApplicationInjectionToken } from "./start-main-application/runnable-tokens/on-load-of-application-injection-token"; -export { createApp } from "./create-app"; +export { createApplication } from "./create-app"; +export type { CreateApplication, Application, ApplicationConfig } from "../common/create-app"; export * as Mobx from "mobx"; export * as mainExtensionApi from "../extensions/main-api"; export * as commonExtensionApi from "../extensions/common-api"; diff --git a/packages/core/src/renderer/create-app.ts b/packages/core/src/renderer/create-app.ts index 3a511af5ec..e2064a7ae2 100644 --- a/packages/core/src/renderer/create-app.ts +++ b/packages/core/src/renderer/create-app.ts @@ -5,19 +5,16 @@ import "./components/app.scss"; import { bootstrap } from "./bootstrap"; -import type { DiContainer } from "@ogre-tools/injectable"; import { getInjectable } from "@ogre-tools/injectable"; import nodeEnvInjectionToken from "../common/vars/node-env-injection-token"; import { runInAction } from "mobx"; import { registerInjectables } from "./register-injectables"; +import type { CreateApplication } from "../common/create-app"; +import { getDi } from "./getDi"; -interface AppConfig { - di: DiContainer; - mode: string; -} - -export function createApp(conf: AppConfig) { - const { di, mode } = conf; +export const createApplication: CreateApplication = (config) => { + const { mode } = config; + const di = getDi(); runInAction(() => { di.register(getInjectable({ @@ -25,10 +22,12 @@ export function createApp(conf: AppConfig) { instantiate: () => mode, injectionToken: nodeEnvInjectionToken, })); + registerInjectables(di); }); - + return { start: () => bootstrap(di), + di, }; -} +}; diff --git a/packages/core/src/renderer/library.ts b/packages/core/src/renderer/library.ts index be72fc6c89..aa9c1fc907 100644 --- a/packages/core/src/renderer/library.ts +++ b/packages/core/src/renderer/library.ts @@ -14,4 +14,5 @@ export * as ReactRouter from "react-router"; export * as ReactRouterDom from "react-router-dom"; export * as rendererExtensionApi from "../extensions/renderer-api"; export * as commonExtensionApi from "../extensions/common-api"; -export { createApp } from "./create-app"; +export { createApplication } from "./create-app"; +export type { CreateApplication, Application, ApplicationConfig } from "../common/create-app"; diff --git a/packages/open-lens/src/main/index.ts b/packages/open-lens/src/main/index.ts index aca3da2bb4..39500b2240 100644 --- a/packages/open-lens/src/main/index.ts +++ b/packages/open-lens/src/main/index.ts @@ -1,18 +1,15 @@ -import { createContainer } from "@ogre-tools/injectable"; import { autoRegister } from "@ogre-tools/injectable-extension-for-auto-registration"; import { runInAction } from "mobx"; -import { createApp, mainExtensionApi as Main, commonExtensionApi as Common } from "@k8slens/core/main"; +import { createApplication, mainExtensionApi as Main, commonExtensionApi as Common } from "@k8slens/core/main"; -const di = createContainer("main"); -const app = createApp({ - di, +const app = createApplication({ mode: process.env.NODE_ENV || "development" }); runInAction(() => { try { autoRegister({ - di, + di: app.di, targetModule: module, getRequireContexts: () => [ require.context("./", true, CONTEXT_MATCHER_FOR_NON_FEATURES), diff --git a/packages/open-lens/src/renderer/index.ts b/packages/open-lens/src/renderer/index.ts index 2774644e94..ed28f26d3a 100644 --- a/packages/open-lens/src/renderer/index.ts +++ b/packages/open-lens/src/renderer/index.ts @@ -1,18 +1,15 @@ import "@k8slens/core/styles"; -import { createContainer } from "@ogre-tools/injectable"; import { runInAction } from "mobx"; -import { createApp, rendererExtensionApi as Renderer, commonExtensionApi as Common } from "@k8slens/core/renderer"; +import { createApplication, rendererExtensionApi as Renderer, commonExtensionApi as Common } from "@k8slens/core/renderer"; import { autoRegister } from "@ogre-tools/injectable-extension-for-auto-registration"; -const di = createContainer("renderer"); -const app = createApp({ - di, +const app = createApplication({ mode: process.env.NODE_ENV || "development" }); runInAction(() => { autoRegister({ - di, + di: app.di, targetModule: module, getRequireContexts: () => [ require.context("./", true, CONTEXT_MATCHER_FOR_NON_FEATURES),