Make it possible to reload hook-files

This commit is contained in:
Jan Oberhauser 2020-11-25 15:42:33 +01:00
parent dfd054f036
commit e428ccffba

View file

@ -20,6 +20,19 @@ class ExternalHooksClass implements IExternalHooksClass {
return;
}
await this.loadHooksFiles();
this.initDidRun = true;
}
async reload() {
this.externalHooks = {};
await this.loadHooksFiles(true);
}
async loadHooksFiles(reload = false) {
const externalHookFiles = config.get('externalHookFiles').split(':');
// Load all the provided hook-files
@ -27,6 +40,11 @@ class ExternalHooksClass implements IExternalHooksClass {
hookFilePath = hookFilePath.trim();
if (hookFilePath !== '') {
try {
if (reload === true) {
delete require.cache[require.resolve(hookFilePath)];
}
const hookFile = require(hookFilePath);
for (const resource of Object.keys(hookFile)) {
@ -46,10 +64,9 @@ class ExternalHooksClass implements IExternalHooksClass {
}
}
}
this.initDidRun = true;
}
async run(hookName: string, hookParameters?: any[]): Promise<void> { // tslint:disable-line:no-any
const externalHookFunctions: IExternalHooksFunctions = {
dbCollections: Db.collections,
@ -64,6 +81,7 @@ class ExternalHooksClass implements IExternalHooksClass {
}
}
exists(hookName: string): boolean {
return !!this.externalHooks[hookName];
}