Skip to content

Commit

Permalink
Improving extension loading. It mostly solves #847 and #784
Browse files Browse the repository at this point in the history
  • Loading branch information
bpatrik committed Apr 12, 2024
1 parent 1ae44e8 commit 055862f
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 25 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"reflect-metadata": "0.1.13",
"sharp": "0.31.3",
"ts-node-iptc": "1.0.11",
"typeconfig": "2.2.13",
"typeconfig": "2.2.15",
"typeorm": "0.3.12",
"xml2js": "0.6.2"
},
Expand Down
23 changes: 15 additions & 8 deletions src/backend/model/extension/ExtensionConfigTemplateLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ 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';

const execSync = child_process.execSync;

Check warning on line 7 in src/backend/model/extension/ExtensionConfigTemplateLoader.ts

View workflow job for this annotation

GitHub Actions / test (18.x)

'execSync' is assigned a value but never used

const LOG_TAG = '[ExtensionConfigTemplateLoader]';

Check warning on line 9 in src/backend/model/extension/ExtensionConfigTemplateLoader.ts

View workflow job for this annotation

GitHub Actions / test (18.x)

'LOG_TAG' is assigned a value but never used

Expand Down Expand Up @@ -52,20 +54,25 @@ export class ExtensionConfigTemplateLoader {
for (let i = 0; i < this.extensionList.length; ++i) {
const extFolder = this.extensionList[i];
const extPath = path.join(this.extensionsFolder, extFolder);
const configExtPath = path.join(extPath, 'config.js');
const serverExtPath = path.join(extPath, 'server.js');

// if server.js is missing, it's not a valid extension
if (!fs.existsSync(serverExtPath)) {
continue;
}


// eslint-disable-next-line @typescript-eslint/no-var-requires
const ext = require(serverExtPath);
if (typeof ext?.initConfig === 'function') {
ext?.initConfig({
setConfigTemplate: (template: { new(): unknown }): void => {
this.extensionTemplates.push({folder: extFolder, template: template});
}
});
if (fs.existsSync(configExtPath)) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const extCfg = require(configExtPath);
if (typeof extCfg?.initConfig === 'function') {
extCfg?.initConfig({
setConfigTemplate: (template: { new(): unknown }): void => {
this.extensionTemplates.push({folder: extFolder, template: template});
}
});
}
} else {
//also create basic config extensions that do not have any
this.extensionTemplates.push({folder: extFolder});
Expand Down
3 changes: 2 additions & 1 deletion src/backend/model/extension/ExtensionConfigWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class ExtensionConfigWrapper {

await pc.load(); // loading the basic configs, but we do not know the extension config hierarchy yet

// TODO make sure that all extensions are present even after loading them from file
} catch (e) {
console.error(LOG_TAG, 'Error during loading config. Reverting to defaults.');
console.error(e);
Expand All @@ -49,7 +50,7 @@ export class ExtensionConfigWrapper {
pc.saveSync();
}
pc.loadSync(); // loading the basic configs, but we do not know the extension config hierarchy yet

// TODO make sure that all extensions are present even after loading them from file
} catch (e) {
console.error(LOG_TAG, 'Error during loading config. Reverting to defaults.');
console.error(e);
Expand Down
22 changes: 16 additions & 6 deletions src/backend/model/extension/IExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,21 +217,31 @@ export interface IExtensionConfigInit<C> {
}

/**
* Extension interface. All extension is expected to implement and export these methods
* Extension interface. All extension is expected to implement and export these methods.
* This is the content of the server.js file
*/
export interface IServerExtension<C> {

/**
* This function can be called any time. It should only set the config template class
* Extension init function. Extension should at minimum expose this function.
* @param extension
*/
initConfig(extension: IExtensionConfigInit<C>): void;
init(extension: IExtensionObject<C>): Promise<void>;

cleanUp?: (extension: IExtensionObject<C>) => Promise<void>;
}


/**
* Extension config interface. All extension can implement and export these methods.
* This is the content of the config.js file.
*/
export interface IServerExtensionConfig<C> {

/**
* Extension init function. Extension should at minimum expose this function.
* This function can be called any time. It should only set the config template class
* @param extension
*/
init(extension: IExtensionObject<C>): Promise<void>;
initConfig(extension: IExtensionConfigInit<C>): void;

cleanUp?: (extension: IExtensionObject<C>) => Promise<void>;
}
6 changes: 4 additions & 2 deletions src/common/config/private/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import {ConfigClassBuilder} from 'typeconfig/node';
import {ExtensionConfigTemplateLoader} from '../../../backend/model/extension/ExtensionConfigTemplateLoader';
import * as path from 'path';

// we need to know the location of the extensions to load the full config (including the extensions)
const pre = ConfigClassBuilder.attachPrivateInterface(new PrivateConfigClass());
try {
pre.loadSync();
} catch (e) { /* empty */ }
pre.loadSync({preventSaving: true});
} catch (e) { /* empty */
}
ExtensionConfigTemplateLoader.Instance.init(path.join(__dirname, '/../../../../', pre.Extensions.folder));

export const Config = ExtensionConfigWrapper.originalSync(true);

0 comments on commit 055862f

Please sign in to comment.