From 012fc1f7b49650e46c13e9e589e284866fb92fa6 Mon Sep 17 00:00:00 2001 From: "Patrik J. Braun" Date: Fri, 26 Apr 2024 10:43:27 +0200 Subject: [PATCH] Move extension config from array to map #847 #784 --- package-lock.json | 14 +++++++------- package.json | 2 +- src/backend/model/extension/ExtensionConfig.ts | 4 ++-- .../extension/ExtensionConfigTemplateLoader.ts | 16 +++++++++------- src/backend/model/extension/ExtensionManager.ts | 10 ++++++---- .../private/subconfigs/ServerExtensionsConfig.ts | 8 +++----- 6 files changed, 28 insertions(+), 26 deletions(-) diff --git a/package-lock.json b/package-lock.json index ee0ed6d5..7f304ed7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,7 +27,7 @@ "reflect-metadata": "0.1.13", "sharp": "0.31.3", "ts-node-iptc": "1.0.11", - "typeconfig": "2.2.15", + "typeconfig": "2.3.1", "typeorm": "0.3.12", "xml2js": "0.6.2" }, @@ -20362,9 +20362,9 @@ } }, "node_modules/typeconfig": { - "version": "2.2.15", - "resolved": "https://registry.npmjs.org/typeconfig/-/typeconfig-2.2.15.tgz", - "integrity": "sha512-aqiuT5BtV0/0MYMMG78c1IqeJrF85r1W1pJckkGolPjHpE0ajA3oOgnRtX5DRDHsn3YzsY5FKMxj1B3J+ISx1g==", + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/typeconfig/-/typeconfig-2.3.1.tgz", + "integrity": "sha512-xBdsf0tK/PcXyZzWq/U2xLNDNrVFkRQ9d8V9x3B5eu1LG5GmeDDK7zLScz79zbl0dCZzhjnvx4RRggY6DZAAZA==", "dependencies": { "minimist": "1.2.8" } @@ -35283,9 +35283,9 @@ } }, "typeconfig": { - "version": "2.2.15", - "resolved": "https://registry.npmjs.org/typeconfig/-/typeconfig-2.2.15.tgz", - "integrity": "sha512-aqiuT5BtV0/0MYMMG78c1IqeJrF85r1W1pJckkGolPjHpE0ajA3oOgnRtX5DRDHsn3YzsY5FKMxj1B3J+ISx1g==", + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/typeconfig/-/typeconfig-2.3.1.tgz", + "integrity": "sha512-xBdsf0tK/PcXyZzWq/U2xLNDNrVFkRQ9d8V9x3B5eu1LG5GmeDDK7zLScz79zbl0dCZzhjnvx4RRggY6DZAAZA==", "requires": { "minimist": "1.2.8" } diff --git a/package.json b/package.json index 9b5c036e..404951f6 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ "reflect-metadata": "0.1.13", "sharp": "0.31.3", "ts-node-iptc": "1.0.11", - "typeconfig": "2.2.15", + "typeconfig": "2.3.1", "typeorm": "0.3.12", "xml2js": "0.6.2" }, diff --git a/src/backend/model/extension/ExtensionConfig.ts b/src/backend/model/extension/ExtensionConfig.ts index b10a2ac1..718f2131 100644 --- a/src/backend/model/extension/ExtensionConfig.ts +++ b/src/backend/model/extension/ExtensionConfig.ts @@ -1,5 +1,6 @@ import {IExtensionConfig} from './IExtension'; import {Config} from '../../../common/config/private/Config'; +import {ServerExtensionsEntryConfig} from '../../../common/config/private/subconfigs/ServerExtensionsConfig'; export class ExtensionConfig implements IExtensionConfig { @@ -8,8 +9,7 @@ export class ExtensionConfig implements IExtensionConfig { public getConfig(): C { - const c = (Config.Extensions.extensions || []) - .find(e => e.path === this.extensionFolder); + const c = Config.Extensions.extensions[this.extensionFolder] as ServerExtensionsEntryConfig; return c?.configs as C; } diff --git a/src/backend/model/extension/ExtensionConfigTemplateLoader.ts b/src/backend/model/extension/ExtensionConfigTemplateLoader.ts index f4235df8..868e7e9a 100644 --- a/src/backend/model/extension/ExtensionConfigTemplateLoader.ts +++ b/src/backend/model/extension/ExtensionConfigTemplateLoader.ts @@ -92,16 +92,20 @@ export class ExtensionConfigTemplateLoader { } const ePaths = this.extensionTemplates.map(et => et.folder); + // delete not existing extensions - config.Extensions.extensions = config.Extensions.extensions - .filter(ec => ePaths.indexOf(ec.path) !== -1); + for (const prop of config.Extensions.extensions.keys()) { + if (ePaths.indexOf(prop) > -1) { + continue; + } + config.Extensions.extensions.removeProperty(prop); + } for (let i = 0; i < this.extensionTemplates.length; ++i) { const ext = this.extensionTemplates[i]; - let c = (config.Extensions.extensions || []) - .find(e => e.path === ext.folder); + let c = config.Extensions.extensions[ext.folder]; // set the new structure with the new def values if (!c) { @@ -109,9 +113,7 @@ export class ExtensionConfigTemplateLoader { if (ext.template) { c.configs = new ext.template(); } - // TODO: this does not hold if the order of the extensions mixes up. - // TODO: experiment with a map instead of an array - config.Extensions.extensions.push(c); + config.Extensions.extensions.addProperty(ext.folder, {type: ServerExtensionsEntryConfig}, c); } } diff --git a/src/backend/model/extension/ExtensionManager.ts b/src/backend/model/extension/ExtensionManager.ts index ab203a41..61db3534 100644 --- a/src/backend/model/extension/ExtensionManager.ts +++ b/src/backend/model/extension/ExtensionManager.ts @@ -12,6 +12,7 @@ import {SQLConnection} from '../database/SQLConnection'; import {ExtensionObject} from './ExtensionObject'; import {ExtensionDecoratorObject} from './ExtensionDecorator'; import * as util from 'util'; +import {ServerExtensionsEntryConfig} from '../../../common/config/private/subconfigs/ServerExtensionsConfig'; // eslint-disable-next-line @typescript-eslint/no-var-requires const exec = util.promisify(require('child_process').exec); @@ -80,7 +81,7 @@ export class ExtensionManager implements IObjectManager { extList.sort(); - Logger.debug(LOG_TAG, 'Extensions found: ', JSON.stringify(Config.Extensions.extensions.map(ec => ec.path))); + Logger.debug(LOG_TAG, 'Extensions found: ', JSON.stringify(Config.Extensions.extensions.keys())); } private createUniqueExtensionObject(name: string, folder: string): IExtensionObject { @@ -99,11 +100,12 @@ export class ExtensionManager implements IObjectManager { private async initExtensions() { - for (let i = 0; i < Config.Extensions.extensions.length; ++i) { - const extFolder = Config.Extensions.extensions[i].path; + for (const prop of Config.Extensions.extensions.keys()) { + const extConf: ServerExtensionsEntryConfig = Config.Extensions.extensions[prop] as ServerExtensionsEntryConfig; + const extFolder = extConf.path; let extName = extFolder; - if (Config.Extensions.extensions[i].enabled === false) { + if (extConf.enabled === false) { Logger.silly(LOG_TAG, `Skipping ${extFolder} initiation. Extension is disabled.`); } const extPath = path.join(ProjectPath.ExtensionFolder, extFolder); diff --git a/src/common/config/private/subconfigs/ServerExtensionsConfig.ts b/src/common/config/private/subconfigs/ServerExtensionsConfig.ts index d84932c7..f8aec92e 100644 --- a/src/common/config/private/subconfigs/ServerExtensionsConfig.ts +++ b/src/common/config/private/subconfigs/ServerExtensionsConfig.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/no-inferrable-types */ -import {ConfigProperty, SubConfigClass} from 'typeconfig/common'; +import {ConfigMap, ConfigProperty, IConfigMap, SubConfigClass} from 'typeconfig/common'; import {ClientExtensionsConfig, ConfigPriority, TAGS} from '../../public/ClientConfig'; import {GenericConfigType} from 'typeconfig/src/GenericConfigType'; @@ -59,16 +59,14 @@ export class ServerExtensionsConfig extends ClientExtensionsConfig { }) folder: string = 'extensions'; - // TODO: this does not hold if the order of the extensions mixes up. - // TODO: experiment with a map instead of an array @ConfigProperty({ - arrayType: ServerExtensionsEntryConfig, + type: ConfigMap, tags: { name: $localize`Installed extensions`, priority: ConfigPriority.advanced } }) - extensions: ServerExtensionsEntryConfig[] = []; + extensions: IConfigMap = new ConfigMap(); @ConfigProperty({