2021-08-29 11:58:11 -07:00
|
|
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
|
|
/* eslint-disable import/no-dynamic-require */
|
|
|
|
/* eslint-disable no-restricted-syntax */
|
|
|
|
// eslint-disable-next-line import/no-cycle
|
|
|
|
import { Db, IExternalHooksClass, IExternalHooksFileData, IExternalHooksFunctions } from '.';
|
2020-05-03 23:56:01 -07:00
|
|
|
|
2022-04-08 14:32:08 -07:00
|
|
|
import config from '../config';
|
2020-05-03 23:56:01 -07:00
|
|
|
|
2020-05-05 15:59:58 -07:00
|
|
|
class ExternalHooksClass implements IExternalHooksClass {
|
2020-05-03 23:56:01 -07:00
|
|
|
externalHooks: {
|
2021-08-29 11:58:11 -07:00
|
|
|
[key: string]: Array<() => {}>;
|
2020-05-03 23:56:01 -07:00
|
|
|
} = {};
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
initDidRun = false;
|
2020-05-03 23:56:01 -07:00
|
|
|
|
|
|
|
async init(): Promise<void> {
|
2021-08-29 11:58:11 -07:00
|
|
|
if (this.initDidRun) {
|
2020-05-05 15:59:58 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-25 06:42:33 -08:00
|
|
|
await this.loadHooksFiles();
|
|
|
|
|
|
|
|
this.initDidRun = true;
|
|
|
|
}
|
|
|
|
|
2020-12-30 02:45:29 -08:00
|
|
|
async reload(externalHooks?: IExternalHooksFileData) {
|
2020-11-25 06:42:33 -08:00
|
|
|
this.externalHooks = {};
|
2020-12-30 02:45:29 -08:00
|
|
|
|
|
|
|
if (externalHooks === undefined) {
|
|
|
|
await this.loadHooksFiles(true);
|
|
|
|
} else {
|
|
|
|
this.loadHooks(externalHooks);
|
|
|
|
}
|
2020-11-25 06:42:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async loadHooksFiles(reload = false) {
|
2022-04-08 10:37:27 -07:00
|
|
|
const externalHookFiles = config.getEnv('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 {
|
2021-08-29 11:58:11 -07:00
|
|
|
if (reload) {
|
2020-11-25 06:42:33 -08:00
|
|
|
delete require.cache[require.resolve(hookFilePath)];
|
|
|
|
}
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line import/no-dynamic-require
|
|
|
|
// eslint-disable-next-line global-require
|
2020-12-30 02:45:29 -08:00
|
|
|
const hookFile = require(hookFilePath) as IExternalHooksFileData;
|
|
|
|
this.loadHooks(hookFile);
|
2020-05-04 16:23:54 -07:00
|
|
|
} catch (error) {
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions, @typescript-eslint/no-unsafe-member-access
|
2020-05-04 16:23:54 -07:00
|
|
|
throw new Error(`Problem loading external hook file "${hookFilePath}": ${error.message}`);
|
2020-05-03 23:56:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-30 02:45:29 -08:00
|
|
|
loadHooks(hookFileData: IExternalHooksFileData) {
|
|
|
|
for (const resource of Object.keys(hookFileData)) {
|
|
|
|
for (const operation of Object.keys(hookFileData[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] = [];
|
|
|
|
}
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line prefer-spread
|
|
|
|
this.externalHooks[hookString].push.apply(
|
|
|
|
this.externalHooks[hookString],
|
|
|
|
hookFileData[resource][operation],
|
|
|
|
);
|
2020-12-30 02:45:29 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
async run(hookName: string, hookParameters?: any[]): Promise<void> {
|
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;
|
|
|
|
}
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
for (const externalHookFunction of this.externalHooks[hookName]) {
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
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;
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
2020-05-03 23:56:01 -07:00
|
|
|
export function ExternalHooks(): ExternalHooksClass {
|
|
|
|
if (externalHooksInstance === undefined) {
|
|
|
|
externalHooksInstance = new ExternalHooksClass();
|
|
|
|
}
|
|
|
|
|
|
|
|
return externalHooksInstance;
|
|
|
|
}
|