2020-05-03 23:56:01 -07:00
|
|
|
import {
|
|
|
|
Db,
|
2020-05-05 15:59:58 -07:00
|
|
|
IExternalHooksFunctions,
|
|
|
|
IExternalHooksClass,
|
2020-05-04 16:23:54 -07:00
|
|
|
} from './';
|
2020-05-03 23:56:01 -07:00
|
|
|
|
|
|
|
import * as config from '../config';
|
|
|
|
|
|
|
|
|
2020-05-05 15:59:58 -07:00
|
|
|
class ExternalHooksClass implements IExternalHooksClass {
|
2020-05-03 23:56:01 -07:00
|
|
|
|
|
|
|
externalHooks: {
|
|
|
|
[key: string]: Array<() => {}>
|
|
|
|
} = {};
|
2020-05-05 15:59:58 -07:00
|
|
|
initDidRun = false;
|
2020-05-03 23:56:01 -07:00
|
|
|
|
|
|
|
|
|
|
|
async init(): Promise<void> {
|
2020-05-05 15:59:58 -07:00
|
|
|
if (this.initDidRun === true) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-04 16:23:54 -07:00
|
|
|
const externalHookFiles = config.get('externalHookFiles').split(':');
|
2020-05-03 23:56:01 -07:00
|
|
|
|
2020-05-04 16:23:54 -07:00
|
|
|
// Load all the provided hook-files
|
2020-05-03 23:56:01 -07:00
|
|
|
for (let hookFilePath of externalHookFiles) {
|
|
|
|
hookFilePath = hookFilePath.trim();
|
|
|
|
if (hookFilePath !== '') {
|
2020-05-04 16:23:54 -07:00
|
|
|
try {
|
|
|
|
const hookFile = require(hookFilePath);
|
|
|
|
|
|
|
|
for (const resource of Object.keys(hookFile)) {
|
|
|
|
for (const operation of Object.keys(hookFile[resource])) {
|
|
|
|
// Save all the hook functions directly under their string
|
|
|
|
// format in an array
|
|
|
|
const hookString = `${resource}.${operation}`;
|
|
|
|
if (this.externalHooks[hookString] === undefined) {
|
|
|
|
this.externalHooks[hookString] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
this.externalHooks[hookString].push.apply(this.externalHooks[hookString], hookFile[resource][operation]);
|
2020-05-03 23:56:01 -07:00
|
|
|
}
|
|
|
|
}
|
2020-05-04 16:23:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
throw new Error(`Problem loading external hook file "${hookFilePath}": ${error.message}`);
|
2020-05-03 23:56:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 15:59:58 -07:00
|
|
|
|
|
|
|
this.initDidRun = true;
|
2020-05-03 23:56:01 -07:00
|
|
|
}
|
|
|
|
|
2020-05-04 16:23:54 -07:00
|
|
|
async run(hookName: string, hookParameters?: any[]): Promise<void> { // tslint:disable-line:no-any
|
2020-05-05 15:59:58 -07:00
|
|
|
const externalHookFunctions: IExternalHooksFunctions = {
|
|
|
|
dbCollections: Db.collections,
|
2020-05-03 23:56:01 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (this.externalHooks[hookName] === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(const externalHookFunction of this.externalHooks[hookName]) {
|
2020-05-04 16:23:54 -07:00
|
|
|
await externalHookFunction.apply(externalHookFunctions, hookParameters);
|
2020-05-03 23:56:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 10:01:40 -07:00
|
|
|
exists(hookName: string): boolean {
|
|
|
|
return !!this.externalHooks[hookName];
|
|
|
|
}
|
|
|
|
|
2020-05-03 23:56:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let externalHooksInstance: ExternalHooksClass | undefined;
|
|
|
|
|
|
|
|
export function ExternalHooks(): ExternalHooksClass {
|
|
|
|
if (externalHooksInstance === undefined) {
|
|
|
|
externalHooksInstance = new ExternalHooksClass();
|
|
|
|
}
|
|
|
|
|
|
|
|
return externalHooksInstance;
|
|
|
|
}
|