diff --git a/extension/README.md b/extension/README.md index 6dda2848..33cfb34c 100644 --- a/extension/README.md +++ b/extension/README.md @@ -7,7 +7,7 @@ See sample extension at https://github.com/bpatrik/pigallery2-sample-extension. # Extension Usage Extension folder can be set through config. For the docker-ised version, -they live under the `config/extension` folder in their own subdirectory. +they live under the `config/extension` folder in their own subdirectory. # Extension development @@ -21,16 +21,20 @@ You need at least a `server.js` in your extension folder that exports a `init(ex /myextension/package.js <- this is optional. You can add extra npm packages here /myextension/server.js <- this is needed ``` + Where `` is what you set in the config and `myextension` is the name of your extension. -Note: you do not need to add your `node_modules` folder. The app will call `npm intall` when initializing your extension. +Note: you do not need to add your `node_modules` folder. The app will call `npm install` when initializing your extension. + ## Extension environment The app runs the extension the following way: -* It reads all extensions in `/**` folder -* Checks if `package.js` is present. If yes installs the packages -* Checks if `server.js` is present. If yes, calls the `init` function. + +- It reads all extensions in `/**` folder +- Checks if `package.js` is present. If yes installs the packages +- Checks if `server.js` is present. If yes, calls the `init` function. ### Init and cleanup lifecycle + There is also a `cleanUp` function that you can implement in your extension. The app can call your `init` and `cleanUp` functions any time. Always calls the `init` first then `cleanUp` later. @@ -39,8 +43,7 @@ Main use-case: `init` is called on app startup. `cleanUp` and `init` called late ## Extension interface The app calls the `init` and `cleanUp` function with a `IExtensionObject` object. -See https://github.com/bpatrik/pigallery2/blob/master/src/backend/model/extension/IExtension.ts for details. - +See https://github.com/bpatrik/pigallery2/blob/master/src/backend/model/extension/IExtension.ts for details. `IExtensionObject` exposes lifecycle events, configs, RestAPis with some limitation. Changes made during the these public apis you do not need to clean up in the `cleanUp` function. @@ -50,7 +53,7 @@ App also exposes private `_app` object to provide access to low level API. Any c See sample server.js at https://github.com/bpatrik/pigallery2-sample-extension. -It is recommended to do the development in `ts`, so creating a `server.ts`. +It is recommended to do the development in `ts`, so creating a `server.ts`. Note: You need to manually transpile your `server.ts` file to `server.js` as the app does not do that for you. This doc assumes you do the development in `ts`. @@ -59,23 +62,23 @@ This doc assumes you do the development in `ts`. You can import package from both the main app package.json and from your extension package.json. To import packages from the main app, you import as usual. For packages from the extension, you always need to write relative path. i.e.: prefix with `./node_modules` + ```ts // Including dev-kit interfaces. It is not necessary, only helps development with types. // You need to prefix them with ./node_modules -import {IExtensionObject} from './node_modules/pigallery2-extension-kit'; +import { IExtensionObject } from "./node_modules/pigallery2-extension-kit"; // Including prod extension packages. You need to prefix them with ./node_modules // lodash does not have types // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore -import * as _ from './node_modules/lodash'; +import * as _ from "./node_modules/lodash"; // Importing packages that are available in the main app (listed in the packages.json in pigallery2) -import {Column, Entity, Index, PrimaryGeneratedColumn} from 'typeorm'; +import { Column, Entity, Index, PrimaryGeneratedColumn } from "typeorm"; ``` - -#### pigallery2 extension dev-kit +#### pigallery2 extension dev-kit It is recommended to use the `pigallery2-extension-kit` node package. `npm install pigallery2-extension-kit --save` to your extension. @@ -87,17 +90,14 @@ You can then `import {IExtensionObject} from './node_modules/pigallery2-extensio See https://github.com/bpatrik/pigallery2/blob/master/src/backend/model/extension/IExtension.ts to understand what contains `IExtensionObject`. -NOTE: this is not needed to create an extension it only helps your IDE and your development. These type definitions are removed when you compile `ts` to `js`. +NOTE: this is not needed to create an extension it only helps your IDE and your development. These type definitions are removed when you compile `ts` to `js`. #### `init` function You need to implement the `init` function for a working extension: ```ts - -export const init = async (extension: IExtensionObject): Promise => { - -} +export const init = async (extension: IExtensionObject): Promise => {}; ``` #### pigallery2 lifecycle `events` @@ -105,5 +105,4 @@ export const init = async (extension: IExtensionObject): Promise => Tha app exposes multiple interfaces for the extensions to interact with the main app. `events` are one of the main interfaces. Here are their flow: - ![events_lifecycle](events.png) 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/ProjectPath.ts b/src/backend/ProjectPath.ts index 7bfc56a0..b0837be0 100644 --- a/src/backend/ProjectPath.ts +++ b/src/backend/ProjectPath.ts @@ -1,6 +1,6 @@ import * as path from 'path'; import * as fs from 'fs'; -import {Config} from '../common/config/private/Config'; +import {PrivateConfigClass} from '../common/config/private/PrivateConfigClass'; export class ProjectPathClass { public Root: string; @@ -11,8 +11,10 @@ export class ProjectPathClass { public FrontendFolder: string; public ExtensionFolder: string; public DBFolder: string; + private cfg: PrivateConfigClass; - constructor() { + init(cfg: PrivateConfigClass) { + this.cfg = cfg; this.reset(); } @@ -31,12 +33,12 @@ export class ProjectPathClass { reset(): void { this.Root = path.join(__dirname, '/../../'); this.FrontendFolder = path.join(this.Root, 'dist'); - this.ImageFolder = this.getAbsolutePath(Config.Media.folder); - this.TempFolder = this.getAbsolutePath(Config.Media.tempFolder); + this.ImageFolder = this.getAbsolutePath(this.cfg.Media.folder); + this.TempFolder = this.getAbsolutePath(this.cfg.Media.tempFolder); this.TranscodedFolder = path.join(this.TempFolder, 'tc'); this.FacesFolder = path.join(this.TempFolder, 'f'); - this.DBFolder = this.getAbsolutePath(Config.Database.dbFolder); - this.ExtensionFolder = this.getAbsolutePath(Config.Extensions.folder); + this.DBFolder = this.getAbsolutePath(this.cfg.Database.dbFolder); + this.ExtensionFolder = this.getAbsolutePath(this.cfg.Extensions.folder); // create thumbnail folder if not exist if (!fs.existsSync(this.TempFolder)) { 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..eab65fd4 100644 --- a/src/backend/model/extension/ExtensionConfigTemplateLoader.ts +++ b/src/backend/model/extension/ExtensionConfigTemplateLoader.ts @@ -2,11 +2,8 @@ import {PrivateConfigClass} from '../../../common/config/private/PrivateConfigCl import * as fs from 'fs'; import * as path from 'path'; import {ServerExtensionsEntryConfig} from '../../../common/config/private/subconfigs/ServerExtensionsConfig'; -import * as child_process from 'child_process'; +import {ProjectPath} from '../../ProjectPath'; -const execSync = child_process.execSync; - -const LOG_TAG = '[ExtensionConfigTemplateLoader]'; /** * This class decouples the extension management and the config. @@ -16,7 +13,6 @@ const LOG_TAG = '[ExtensionConfigTemplateLoader]'; export class ExtensionConfigTemplateLoader { private static instance: ExtensionConfigTemplateLoader; - private extensionsFolder: string; private loaded = false; private extensionList: string[] = []; @@ -31,29 +27,26 @@ export class ExtensionConfigTemplateLoader { } - init(extensionsFolder: string) { - this.extensionsFolder = extensionsFolder; - } public loadExtensionTemplates(config: PrivateConfigClass) { - if (!this.extensionsFolder) { + if (!ProjectPath.ExtensionFolder) { throw new Error('Unknown extensions folder.'); } // already loaded if (!this.loaded) { this.extensionTemplates = []; - if (fs.existsSync(this.extensionsFolder)) { + if (fs.existsSync(ProjectPath.ExtensionFolder)) { this.extensionList = (fs - .readdirSync(this.extensionsFolder)) + .readdirSync(ProjectPath.ExtensionFolder)) .filter((f): boolean => - fs.statSync(path.join(this.extensionsFolder, f)).isDirectory() + fs.statSync(path.join(ProjectPath.ExtensionFolder, f)).isDirectory() ); this.extensionList.sort(); for (let i = 0; i < this.extensionList.length; ++i) { const extFolder = this.extensionList[i]; - const extPath = path.join(this.extensionsFolder, extFolder); + const extPath = path.join(ProjectPath.ExtensionFolder, extFolder); const configExtPath = path.join(extPath, 'config.js'); const serverExtPath = path.join(extPath, 'server.js'); @@ -92,16 +85,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 +106,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/Config.ts b/src/common/config/private/Config.ts index 7a14e5c0..d9232fac 100644 --- a/src/common/config/private/Config.ts +++ b/src/common/config/private/Config.ts @@ -1,8 +1,7 @@ import {ExtensionConfigWrapper} from '../../../backend/model/extension/ExtensionConfigWrapper'; import {PrivateConfigClass} from './PrivateConfigClass'; import {ConfigClassBuilder} from 'typeconfig/node'; -import {ExtensionConfigTemplateLoader} from '../../../backend/model/extension/ExtensionConfigTemplateLoader'; -import * as path from 'path'; +import {ProjectPath} from '../../../backend/ProjectPath'; // we need to know the location of the extensions to load the full config (including the extensions) const pre = ConfigClassBuilder.attachPrivateInterface(new PrivateConfigClass()); @@ -10,6 +9,9 @@ try { pre.loadSync({preventSaving: true}); } catch (e) { /* empty */ } -ExtensionConfigTemplateLoader.Instance.init(path.join(__dirname, '/../../../../', pre.Extensions.folder)); +// load extension paths before full config load +ProjectPath.init(pre); export const Config = ExtensionConfigWrapper.originalSync(true); +// set actual config +ProjectPath.init(Config); 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({