diff --git a/package-lock.json b/package-lock.json index 4dd3a0bb7a..655fe9d8e5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -34981,6 +34981,7 @@ } }, "packages/infrastructure/eslint-config": { + "name": "@k8slens/eslint-config", "version": "^6.5.0-alpha.0", "license": "MIT", "bin": { @@ -37032,6 +37033,7 @@ "license": "MIT", "devDependencies": { "@async-fn/jest": "^1.6.4", + "@k8slens/eslint-config": "*", "@ogre-tools/test-utils": "^15.1.2" }, "peerDependencies": { @@ -37046,6 +37048,9 @@ "name": "@k8slens/feature-core", "version": "6.5.0-alpha.0", "license": "MIT", + "devDependencies": { + "@k8slens/eslint-config": "*" + }, "peerDependencies": { "@ogre-tools/injectable": "^15.1.2" } diff --git a/packages/technical-features/application/agnostic/.eslintrc.js b/packages/technical-features/application/agnostic/.eslintrc.js new file mode 100644 index 0000000000..fae474aa7d --- /dev/null +++ b/packages/technical-features/application/agnostic/.eslintrc.js @@ -0,0 +1,6 @@ +module.exports = { + extends: "@k8slens/eslint-config/eslint", + parserOptions: { + project: "./tsconfig.json", + }, +}; diff --git a/packages/technical-features/application/agnostic/.prettierrc b/packages/technical-features/application/agnostic/.prettierrc new file mode 100644 index 0000000000..edd47b479e --- /dev/null +++ b/packages/technical-features/application/agnostic/.prettierrc @@ -0,0 +1 @@ +"@k8slens/eslint-config/prettier" diff --git a/packages/technical-features/application/agnostic/jest.config.js b/packages/technical-features/application/agnostic/jest.config.js index 6d3d6ff231..c6074967eb 100644 --- a/packages/technical-features/application/agnostic/jest.config.js +++ b/packages/technical-features/application/agnostic/jest.config.js @@ -1,2 +1 @@ -module.exports = - require("@k8slens/jest").monorepoPackageConfig(__dirname).configForNode; +module.exports = require("@k8slens/jest").monorepoPackageConfig(__dirname).configForNode; diff --git a/packages/technical-features/application/agnostic/package.json b/packages/technical-features/application/agnostic/package.json index fc9318e286..0350a0f83e 100644 --- a/packages/technical-features/application/agnostic/package.json +++ b/packages/technical-features/application/agnostic/package.json @@ -26,7 +26,9 @@ "scripts": { "build": "webpack", "dev": "webpack --mode=development --watch", - "test": "jest --coverage --runInBand" + "test": "jest --coverage --runInBand", + "lint": "lens-lint", + "lint:fix": "lens-lint --fix" }, "peerDependencies": { "@k8slens/feature-core": "^6.5.0-alpha.0", @@ -37,6 +39,7 @@ }, "devDependencies": { - "@async-fn/jest": "^1.6.4" + "@async-fn/jest": "^1.6.4", + "@k8slens/eslint-config": "*" } } diff --git a/packages/technical-features/application/agnostic/src/application-information-token.ts b/packages/technical-features/application/agnostic/src/application-information-token.ts index c2035d45a3..ad92e21451 100644 --- a/packages/technical-features/application/agnostic/src/application-information-token.ts +++ b/packages/technical-features/application/agnostic/src/application-information-token.ts @@ -10,15 +10,15 @@ export type ApplicationInformation = { productName: string; copyright: string; description: string; - k8sProxyVersion: string, - bundledKubectlVersion: string, - bundledHelmVersion: string, - sentryDsn: string, - contentSecurityPolicy: string, - welcomeRoute: string, + k8sProxyVersion: string; + bundledKubectlVersion: string; + bundledHelmVersion: string; + sentryDsn: string; + contentSecurityPolicy: string; + welcomeRoute: string; updatingIsEnabled: boolean; dependencies: Partial>; -} +}; export const applicationInformationToken = getInjectionToken({ id: "application-information-token", diff --git a/packages/technical-features/application/agnostic/src/feature.ts b/packages/technical-features/application/agnostic/src/feature.ts index 3ad2a495e6..8a193df498 100644 --- a/packages/technical-features/application/agnostic/src/feature.ts +++ b/packages/technical-features/application/agnostic/src/feature.ts @@ -9,9 +9,7 @@ export const applicationFeature = getFeature({ di, targetModule: module, - getRequireContexts: () => [ - require.context("./", true, /\.injectable\.(ts|tsx)$/), - ], + getRequireContexts: () => [require.context("./", true, /\.injectable\.(ts|tsx)$/)], }); }, }); diff --git a/packages/technical-features/application/agnostic/src/start-application/starting-of-application.test.ts b/packages/technical-features/application/agnostic/src/start-application/starting-of-application.test.ts index 3593eb188a..69219d4a08 100644 --- a/packages/technical-features/application/agnostic/src/start-application/starting-of-application.test.ts +++ b/packages/technical-features/application/agnostic/src/start-application/starting-of-application.test.ts @@ -1,8 +1,4 @@ -import { - createContainer, - DiContainer, - getInjectable, -} from "@ogre-tools/injectable"; +import { createContainer, DiContainer, getInjectable } from "@ogre-tools/injectable"; import { registerFeature } from "@k8slens/feature-core"; import { applicationFeature } from "../feature"; import { startApplicationInjectionToken } from "./start-application.injectable"; @@ -46,7 +42,7 @@ describe("starting-of-application", () => { di.register( beforeApplicationIsLoadingInjectable, onLoadOfApplicationInjectable, - afterApplicationIsLoadedInjectable + afterApplicationIsLoadedInjectable, ); }); @@ -54,7 +50,7 @@ describe("starting-of-application", () => { beforeEach(() => { const startApplication = di.inject(startApplicationInjectionToken); - startApplication(); + void startApplication(); }); it("calls runnables registered in before application is loading", () => { diff --git a/packages/technical-features/application/agnostic/tsconfig.json b/packages/technical-features/application/agnostic/tsconfig.json index a4f6fa613e..1819203dc1 100644 --- a/packages/technical-features/application/agnostic/tsconfig.json +++ b/packages/technical-features/application/agnostic/tsconfig.json @@ -1,3 +1,4 @@ { - "extends": "@k8slens/typescript/config/base.json" + "extends": "@k8slens/typescript/config/base.json", + "include": ["**/*.ts"] } diff --git a/packages/technical-features/application/electron-main/.eslintrc.js b/packages/technical-features/application/electron-main/.eslintrc.js new file mode 100644 index 0000000000..fae474aa7d --- /dev/null +++ b/packages/technical-features/application/electron-main/.eslintrc.js @@ -0,0 +1,6 @@ +module.exports = { + extends: "@k8slens/eslint-config/eslint", + parserOptions: { + project: "./tsconfig.json", + }, +}; diff --git a/packages/technical-features/application/electron-main/.prettierrc b/packages/technical-features/application/electron-main/.prettierrc new file mode 100644 index 0000000000..edd47b479e --- /dev/null +++ b/packages/technical-features/application/electron-main/.prettierrc @@ -0,0 +1 @@ +"@k8slens/eslint-config/prettier" diff --git a/packages/technical-features/application/electron-main/index.ts b/packages/technical-features/application/electron-main/index.ts index 8d853ee1e3..238b564a47 100644 --- a/packages/technical-features/application/electron-main/index.ts +++ b/packages/technical-features/application/electron-main/index.ts @@ -4,4 +4,4 @@ export * from "./src/start-application/time-slots"; export { applicationFeatureForElectronMain } from "./src/feature"; -export const testUtils = { overrideSideEffectsWithFakes } +export const testUtils = { overrideSideEffectsWithFakes }; diff --git a/packages/technical-features/application/electron-main/jest.config.js b/packages/technical-features/application/electron-main/jest.config.js index 6d3d6ff231..c6074967eb 100644 --- a/packages/technical-features/application/electron-main/jest.config.js +++ b/packages/technical-features/application/electron-main/jest.config.js @@ -1,2 +1 @@ -module.exports = - require("@k8slens/jest").monorepoPackageConfig(__dirname).configForNode; +module.exports = require("@k8slens/jest").monorepoPackageConfig(__dirname).configForNode; diff --git a/packages/technical-features/application/electron-main/package.json b/packages/technical-features/application/electron-main/package.json index f68cc3edd8..6ee3987e47 100644 --- a/packages/technical-features/application/electron-main/package.json +++ b/packages/technical-features/application/electron-main/package.json @@ -26,7 +26,9 @@ "scripts": { "build": "webpack", "dev": "webpack --mode=development --watch", - "test": "jest --coverage --runInBand" + "test": "jest --coverage --runInBand", + "lint": "lens-lint", + "lint:fix": "lens-lint --fix" }, "peerDependencies": { "@k8slens/feature-core": "^6.5.0-alpha.0", @@ -38,6 +40,7 @@ "devDependencies": { "@async-fn/jest": "^1.6.4", - "@ogre-tools/test-utils": "^15.1.2" + "@ogre-tools/test-utils": "^15.1.2", + "@k8slens/eslint-config": "*" } } diff --git a/packages/technical-features/application/electron-main/src/electron/electron-app.test.ts b/packages/technical-features/application/electron-main/src/electron/electron-app.test.ts index ed3e6d40a6..677141d9f3 100644 --- a/packages/technical-features/application/electron-main/src/electron/electron-app.test.ts +++ b/packages/technical-features/application/electron-main/src/electron/electron-app.test.ts @@ -1,14 +1,14 @@ import electronAppInjectable from "./electron-app.injectable"; -import { app } from 'electron'; +import { app } from "electron"; import { createContainer } from "@ogre-tools/injectable"; import { applicationFeatureForElectronMain } from "../feature"; import { registerFeature } from "@k8slens/feature-core"; -describe('electron-app', () => { - it('is electron app', () => { - const di = createContainer('irrelevant'); +describe("electron-app", () => { + it("is electron app", () => { + const di = createContainer("irrelevant"); - registerFeature(di, applicationFeatureForElectronMain) + registerFeature(di, applicationFeatureForElectronMain); const actual = di.inject(electronAppInjectable); diff --git a/packages/technical-features/application/electron-main/src/feature.ts b/packages/technical-features/application/electron-main/src/feature.ts index 597d7625f9..de65fae5ac 100644 --- a/packages/technical-features/application/electron-main/src/feature.ts +++ b/packages/technical-features/application/electron-main/src/feature.ts @@ -1,6 +1,6 @@ import { getFeature } from "@k8slens/feature-core"; import { autoRegister } from "@ogre-tools/injectable-extension-for-auto-registration"; -import { applicationFeature } from '@k8slens/application'; +import { applicationFeature } from "@k8slens/application"; export const applicationFeatureForElectronMain = getFeature({ id: "application-for-electron-main", @@ -10,13 +10,9 @@ export const applicationFeatureForElectronMain = getFeature({ di, targetModule: module, - getRequireContexts: () => [ - require.context("./", true, /\.injectable\.(ts|tsx)$/), - ], + getRequireContexts: () => [require.context("./", true, /\.injectable\.(ts|tsx)$/)], }); }, - dependencies: [ - applicationFeature - ] + dependencies: [applicationFeature], }); diff --git a/packages/technical-features/application/electron-main/src/override-side-effects-with-fakes.ts b/packages/technical-features/application/electron-main/src/override-side-effects-with-fakes.ts index 0d33c944d8..a795dbd04b 100644 --- a/packages/technical-features/application/electron-main/src/override-side-effects-with-fakes.ts +++ b/packages/technical-features/application/electron-main/src/override-side-effects-with-fakes.ts @@ -2,5 +2,5 @@ import type { DiContainer } from "@ogre-tools/injectable"; import whenAppIsReadyInjectable from "./start-application/when-app-is-ready.injectable"; export const overrideSideEffectsWithFakes = (di: DiContainer) => { - di.override(whenAppIsReadyInjectable, () => () => Promise.resolve()) + di.override(whenAppIsReadyInjectable, () => () => Promise.resolve()); }; diff --git a/packages/technical-features/application/electron-main/src/start-application/when-app-is-ready.test.ts b/packages/technical-features/application/electron-main/src/start-application/when-app-is-ready.test.ts index f0dc073c59..af65f275e5 100644 --- a/packages/technical-features/application/electron-main/src/start-application/when-app-is-ready.test.ts +++ b/packages/technical-features/application/electron-main/src/start-application/when-app-is-ready.test.ts @@ -18,10 +18,7 @@ describe("when-app-is-ready", () => { whenReadyMock = asyncFn(); - di.override( - electronAppInjectable, - () => ({ whenReady: whenReadyMock } as unknown) - ); + di.override(electronAppInjectable, () => ({ whenReady: whenReadyMock } as unknown)); }); describe("when called", () => { diff --git a/packages/technical-features/application/electron-main/src/starting-of-electron-main-application.test.ts b/packages/technical-features/application/electron-main/src/starting-of-electron-main-application.test.ts index f5e067f9ae..de2bbd2306 100644 --- a/packages/technical-features/application/electron-main/src/starting-of-electron-main-application.test.ts +++ b/packages/technical-features/application/electron-main/src/starting-of-electron-main-application.test.ts @@ -1,8 +1,4 @@ -import { - createContainer, - DiContainer, - getInjectable, -} from "@ogre-tools/injectable"; +import { createContainer, DiContainer, getInjectable } from "@ogre-tools/injectable"; import { registerFeature } from "@k8slens/feature-core"; import { applicationFeatureForElectronMain } from "./feature"; import { @@ -52,7 +48,7 @@ describe("starting-of-electron-main-application", () => { di.register( beforeAnythingIsLoadingInjectable, beforeElectronIsReadyIsLoadingInjectable, - beforeApplicationIsLoadingInjectable + beforeApplicationIsLoadingInjectable, ); di.override(whenAppIsReadyInjectable, () => whenAppIsReadyMock); @@ -62,7 +58,7 @@ describe("starting-of-electron-main-application", () => { beforeEach(() => { const startApplication = di.inject(startApplicationInjectionToken); - startApplication(); + void startApplication(); }); it("calls for synchronous runnables for before anything", () => { diff --git a/packages/technical-features/application/electron-main/tsconfig.json b/packages/technical-features/application/electron-main/tsconfig.json index a4f6fa613e..1819203dc1 100644 --- a/packages/technical-features/application/electron-main/tsconfig.json +++ b/packages/technical-features/application/electron-main/tsconfig.json @@ -1,3 +1,4 @@ { - "extends": "@k8slens/typescript/config/base.json" + "extends": "@k8slens/typescript/config/base.json", + "include": ["**/*.ts"] } diff --git a/packages/technical-features/feature-core/.eslintrc.js b/packages/technical-features/feature-core/.eslintrc.js new file mode 100644 index 0000000000..fae474aa7d --- /dev/null +++ b/packages/technical-features/feature-core/.eslintrc.js @@ -0,0 +1,6 @@ +module.exports = { + extends: "@k8slens/eslint-config/eslint", + parserOptions: { + project: "./tsconfig.json", + }, +}; diff --git a/packages/technical-features/feature-core/.prettierrc b/packages/technical-features/feature-core/.prettierrc new file mode 100644 index 0000000000..edd47b479e --- /dev/null +++ b/packages/technical-features/feature-core/.prettierrc @@ -0,0 +1 @@ +"@k8slens/eslint-config/prettier" diff --git a/packages/technical-features/feature-core/jest.config.js b/packages/technical-features/feature-core/jest.config.js index 23be80353b..38d54ab7b6 100644 --- a/packages/technical-features/feature-core/jest.config.js +++ b/packages/technical-features/feature-core/jest.config.js @@ -1,2 +1 @@ -module.exports = - require("@k8slens/jest").monorepoPackageConfig(__dirname).configForReact; +module.exports = require("@k8slens/jest").monorepoPackageConfig(__dirname).configForReact; diff --git a/packages/technical-features/feature-core/package.json b/packages/technical-features/feature-core/package.json index 506e39d773..99e41c00e4 100644 --- a/packages/technical-features/feature-core/package.json +++ b/packages/technical-features/feature-core/package.json @@ -28,9 +28,15 @@ "scripts": { "build": "webpack", "dev": "webpack --mode=development --watch", - "test": "jest --coverage --runInBand" + "test": "jest --coverage --runInBand", + "lint": "lens-lint", + "lint:fix": "lens-lint --fix" }, "peerDependencies": { "@ogre-tools/injectable": "^15.1.2" + }, + + "devDependencies": { + "@k8slens/eslint-config": "*" } } diff --git a/packages/technical-features/feature-core/src/deregister-feature.ts b/packages/technical-features/feature-core/src/deregister-feature.ts index 3d77759659..4476120ab7 100644 --- a/packages/technical-features/feature-core/src/deregister-feature.ts +++ b/packages/technical-features/feature-core/src/deregister-feature.ts @@ -2,25 +2,31 @@ import type { DiContainer } from "@ogre-tools/injectable"; import type { Feature } from "./feature"; import { featureContextMapInjectable } from "./feature-context-map-injectable"; -export const deregisterFeature = (di: DiContainer, ...features: Feature[]) => { - features.forEach((feature) => { - deregisterFeatureRecursed(di, feature); - }); +const getDependingFeaturesFor = ( + featureContextMap: Map }>, +) => { + const getDependingFeaturesForRecursion = (feature: Feature, atRoot = true): string[] => { + const context = featureContextMap.get(feature); + + if (context?.dependedBy.size) { + return [...context?.dependedBy.entries()].flatMap(([dependant]) => + getDependingFeaturesForRecursion(dependant, false), + ); + } + + return atRoot ? [] : [feature.id]; + }; + + return getDependingFeaturesForRecursion; }; -const deregisterFeatureRecursed = ( - di: DiContainer, - feature: Feature, - dependedBy?: Feature -) => { +const deregisterFeatureRecursed = (di: DiContainer, feature: Feature, dependedBy?: Feature) => { const featureContextMap = di.inject(featureContextMapInjectable); const featureContext = featureContextMap.get(feature); if (!featureContext) { - throw new Error( - `Tried to deregister feature "${feature.id}", but it was not registered.` - ); + throw new Error(`Tried to deregister feature "${feature.id}", but it was not registered.`); } featureContext.numberOfRegistrations--; @@ -33,15 +39,15 @@ const deregisterFeatureRecursed = ( throw new Error( `Tried to deregister Feature "${ feature.id - }", but it is the dependency of Features "${dependingFeatures.join( - ", " - )}"` + }", but it is the dependency of Features "${dependingFeatures.join(", ")}"`, ); } if (dependedBy) { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const oldNumberOfDependents = featureContext.dependedBy.get(dependedBy)!; const newNumberOfDependants = oldNumberOfDependents - 1; + featureContext.dependedBy.set(dependedBy, newNumberOfDependants); if (newNumberOfDependants === 0) { @@ -60,23 +66,8 @@ const deregisterFeatureRecursed = ( }); }; -const getDependingFeaturesFor = ( - featureContextMap: Map }> -) => { - const getDependingFeaturesForRecursion = ( - feature: Feature, - atRoot = true - ): string[] => { - const context = featureContextMap.get(feature); - - if (context?.dependedBy.size) { - return [...context!.dependedBy.entries()].flatMap(([dependant]) => - getDependingFeaturesForRecursion(dependant, false) - ); - } - - return atRoot ? [] : [feature.id]; - }; - - return getDependingFeaturesForRecursion; +export const deregisterFeature = (di: DiContainer, ...features: Feature[]) => { + features.forEach((feature) => { + deregisterFeatureRecursed(di, feature); + }); }; diff --git a/packages/technical-features/feature-core/src/feature-context-map-injectable.ts b/packages/technical-features/feature-core/src/feature-context-map-injectable.ts index 8c34524fee..e444c06bba 100644 --- a/packages/technical-features/feature-core/src/feature-context-map-injectable.ts +++ b/packages/technical-features/feature-core/src/feature-context-map-injectable.ts @@ -11,10 +11,9 @@ export type FeatureContextMap = Map< } >; -export const featureContextMapInjectionToken = - getInjectionToken({ - id: "feature-context-map-injection-token", - }); +export const featureContextMapInjectionToken = getInjectionToken({ + id: "feature-context-map-injection-token", +}); const featureContextMapInjectable = getInjectable({ id: "feature-store", diff --git a/packages/technical-features/feature-core/src/feature-dependencies.test.ts b/packages/technical-features/feature-core/src/feature-dependencies.test.ts index cdb7267419..8ddf6e11b8 100644 --- a/packages/technical-features/feature-core/src/feature-dependencies.test.ts +++ b/packages/technical-features/feature-core/src/feature-dependencies.test.ts @@ -1,14 +1,9 @@ -import { - createContainer, - DiContainer, - getInjectable, - Injectable, -} from "@ogre-tools/injectable"; +import { createContainer, DiContainer, getInjectable, Injectable } from "@ogre-tools/injectable"; import type { Feature } from "./feature"; import { registerFeature } from "./register-feature"; import { deregisterFeature } from "./deregister-feature"; -import { getFeature } from "./feature" ; +import { getFeature } from "./feature"; describe("feature-dependencies", () => { describe("given a parent Feature with another Features as dependency", () => { @@ -55,7 +50,7 @@ describe("feature-dependencies", () => { expect(() => { deregisterFeature(di, someDependencyFeature); }).toThrow( - 'Tried to deregister Feature "some-dependency-feature", but it is the dependency of Features "some-feature"' + 'Tried to deregister Feature "some-dependency-feature", but it is the dependency of Features "some-feature"', ); }); @@ -65,7 +60,7 @@ describe("feature-dependencies", () => { expect(() => { deregisterFeature(di, someDependencyFeature); }).toThrow( - 'Tried to deregister feature "some-dependency-feature", but it was not registered.' + 'Tried to deregister feature "some-dependency-feature", but it was not registered.', ); }); @@ -74,9 +69,7 @@ describe("feature-dependencies", () => { expect(() => { di.inject(someInjectableInDependencyFeature); - }).toThrow( - 'Tried to inject non-registered injectable "irrelevant" -> "some-injectable".' - ); + }).toThrow('Tried to inject non-registered injectable "irrelevant" -> "some-injectable".'); }); }); @@ -112,7 +105,7 @@ describe("feature-dependencies", () => { expect(() => { deregisterFeature(di, someFeature1); }).toThrow( - 'Tried to deregister Feature "some-feature-1", but it is the dependency of Features "some-feature-2"' + 'Tried to deregister Feature "some-feature-1", but it is the dependency of Features "some-feature-2"', ); }); @@ -170,7 +163,7 @@ describe("feature-dependencies", () => { expect(() => { deregisterFeature(di, someSharedDependencyFeature); }).toThrow( - 'Tried to deregister Feature "some-dependency-feature", but it is the dependency of Features "some-feature-1, some-feature-2"' + 'Tried to deregister Feature "some-dependency-feature", but it is the dependency of Features "some-feature-1, some-feature-2"', ); }); @@ -188,7 +181,7 @@ describe("feature-dependencies", () => { expect(() => { di.inject(someInjectableInDependencyFeature); }).toThrow( - 'Tried to inject non-registered injectable "irrelevant" -> "some-injectable-in-dependency-feature".' + 'Tried to inject non-registered injectable "irrelevant" -> "some-injectable-in-dependency-feature".', ); }); }); @@ -231,19 +224,14 @@ describe("feature-dependencies", () => { dependencies: [someFeatureForAdditionalHierarchy], }); - registerFeature( - di, - someFeature1, - someFeature2, - someSharedDependencyFeature - ); + registerFeature(di, someFeature1, someFeature2, someSharedDependencyFeature); }); it("when the shared Feature is deregistered, throws", () => { expect(() => { deregisterFeature(di, someSharedDependencyFeature); }).toThrow( - 'Tried to deregister Feature "some-dependency-feature", but it is the dependency of Features "some-feature-1, some-feature-2"' + 'Tried to deregister Feature "some-dependency-feature", but it is the dependency of Features "some-feature-1, some-feature-2"', ); }); @@ -264,17 +252,12 @@ describe("feature-dependencies", () => { }); it("given all of the Features get deregistered, when injecting an injectable from the shared Feature, throws", () => { - deregisterFeature( - di, - someFeature1, - someFeature2, - someSharedDependencyFeature - ); + deregisterFeature(di, someFeature1, someFeature2, someSharedDependencyFeature); expect(() => { di.inject(someInjectableInDependencyFeature); }).toThrow( - 'Tried to inject non-registered injectable "irrelevant" -> "some-injectable-in-dependency-feature".' + 'Tried to inject non-registered injectable "irrelevant" -> "some-injectable-in-dependency-feature".', ); }); }); diff --git a/packages/technical-features/feature-core/src/feature.ts b/packages/technical-features/feature-core/src/feature.ts index 6a8084c92a..2f9814bf68 100644 --- a/packages/technical-features/feature-core/src/feature.ts +++ b/packages/technical-features/feature-core/src/feature.ts @@ -8,5 +8,4 @@ export interface Feature { export interface GetFeatureArgs extends Feature {} -export const getFeature = (getFeatureArgs: GetFeatureArgs): Feature => - getFeatureArgs; +export const getFeature = (getFeatureArgs: GetFeatureArgs): Feature => getFeatureArgs; diff --git a/packages/technical-features/feature-core/src/register-feature.ts b/packages/technical-features/feature-core/src/register-feature.ts index 391bd60fa3..f02b050002 100644 --- a/packages/technical-features/feature-core/src/register-feature.ts +++ b/packages/technical-features/feature-core/src/register-feature.ts @@ -6,58 +6,6 @@ import { featureContextMapInjectionToken, } from "./feature-context-map-injectable"; -export const registerFeature = (di: DiContainer, ...features: Feature[]) => { - features.forEach((feature) => { - registerFeatureRecursed(di, feature); - }); -}; - -const registerFeatureRecursed = ( - di: DiContainer, - feature: Feature, - dependedBy?: Feature -) => { - const featureContextMaps = di.injectMany(featureContextMapInjectionToken); - - if (featureContextMaps.length === 0) { - di.register(featureContextMapInjectable); - } - - const featureContextMap = di.inject(featureContextMapInjectable); - - const existingFeatureContext = featureContextMap.get(feature); - if ( - !dependedBy && - existingFeatureContext && - existingFeatureContext.dependedBy.size === 0 - ) { - throw new Error( - `Tried to register feature "${feature.id}", but it was already registered.` - ); - } - - const featureContext = - existingFeatureContext || createFeatureContext(feature, di); - - featureContext.numberOfRegistrations++; - - if (dependedBy) { - const oldNumberOfDependents = - featureContext.dependedBy.get(dependedBy) || 0; - - const newNumberOfDependants = oldNumberOfDependents + 1; - featureContext.dependedBy.set(dependedBy, newNumberOfDependants); - } - - if (!existingFeatureContext) { - featureContext.register(); - } - - feature.dependencies?.forEach((dependency) => { - registerFeatureRecursed(di, dependency, feature); - }); -}; - const createFeatureContext = (feature: Feature, di: DiContainer) => { const featureContextInjectable = getInjectable({ id: feature.id, @@ -88,3 +36,45 @@ const createFeatureContext = (feature: Feature, di: DiContainer) => { return featureContext; }; + +const registerFeatureRecursed = (di: DiContainer, feature: Feature, dependedBy?: Feature) => { + const featureContextMaps = di.injectMany(featureContextMapInjectionToken); + + if (featureContextMaps.length === 0) { + di.register(featureContextMapInjectable); + } + + const featureContextMap = di.inject(featureContextMapInjectable); + + const existingFeatureContext = featureContextMap.get(feature); + + if (!dependedBy && existingFeatureContext && existingFeatureContext.dependedBy.size === 0) { + throw new Error(`Tried to register feature "${feature.id}", but it was already registered.`); + } + + const featureContext = existingFeatureContext || createFeatureContext(feature, di); + + featureContext.numberOfRegistrations++; + + if (dependedBy) { + const oldNumberOfDependents = featureContext.dependedBy.get(dependedBy) || 0; + + const newNumberOfDependants = oldNumberOfDependents + 1; + + featureContext.dependedBy.set(dependedBy, newNumberOfDependants); + } + + if (!existingFeatureContext) { + featureContext.register(); + } + + feature.dependencies?.forEach((dependency) => { + registerFeatureRecursed(di, dependency, feature); + }); +}; + +export const registerFeature = (di: DiContainer, ...features: Feature[]) => { + features.forEach((feature) => { + registerFeatureRecursed(di, feature); + }); +}; diff --git a/packages/technical-features/feature-core/src/registration-of-feature.test.ts b/packages/technical-features/feature-core/src/registration-of-feature.test.ts index ee7d6d7c86..41b377bb25 100644 --- a/packages/technical-features/feature-core/src/registration-of-feature.test.ts +++ b/packages/technical-features/feature-core/src/registration-of-feature.test.ts @@ -1,10 +1,5 @@ import { registerFeature } from "./register-feature"; -import { - createContainer, - DiContainer, - getInjectable, - Injectable, -} from "@ogre-tools/injectable"; +import { createContainer, DiContainer, getInjectable, Injectable } from "@ogre-tools/injectable"; import type { Feature } from "./feature"; import { getFeature } from "./feature"; import { deregisterFeature } from "./deregister-feature"; @@ -90,9 +85,7 @@ describe("register-feature", () => { it("when a Feature is registered again, throws", () => { expect(() => { registerFeature(di, someFeature); - }).toThrow( - 'Tried to register feature "some-feature-1", but it was already registered.' - ); + }).toThrow('Tried to register feature "some-feature-1", but it was already registered.'); }); it("given a Feature deregistered, when deregistered again, throws", () => { @@ -100,15 +93,14 @@ describe("register-feature", () => { expect(() => { deregisterFeature(di, someFeature); - }).toThrow( - 'Tried to deregister feature "some-feature-1", but it was not registered.' - ); + }).toThrow('Tried to deregister feature "some-feature-1", but it was not registered.'); }); }); it("given di-container and registered Features with injectables forming a cycle, when an injectable is injected, throws with namespaced error about cycle", () => { const someInjectable: Injectable = getInjectable({ id: "some-injectable-1", + // eslint-disable-next-line @typescript-eslint/no-use-before-define instantiate: (di) => di.inject(someInjectable2), }); @@ -140,8 +132,9 @@ describe("register-feature", () => { expect(() => { di.inject(someInjectable); }).toThrow( - // 'Cycle of injectables encountered: "some-container" -> "some-feature-1:some-injectable-1" -> "some-feature-2:some-injectable-2" -> "some-feature-1:some-injectable-1"' - 'Maximum call stack size exceeded' + // 'Cycle of injectables encountered: "some-container" -> "some-feature-1:some-injectable-1" + // -> "some-feature-2:some-injectable-2" -> "some-feature-1:some-injectable-1"' + "Maximum call stack size exceeded", ); }); }); diff --git a/packages/technical-features/feature-core/tsconfig.json b/packages/technical-features/feature-core/tsconfig.json index a4f6fa613e..1819203dc1 100644 --- a/packages/technical-features/feature-core/tsconfig.json +++ b/packages/technical-features/feature-core/tsconfig.json @@ -1,3 +1,4 @@ { - "extends": "@k8slens/typescript/config/base.json" + "extends": "@k8slens/typescript/config/base.json", + "include": ["**/*.ts"] }