2020-05-03 23:56:01 -07:00
|
|
|
import {
|
|
|
|
Db,
|
2020-05-04 16:23:54 -07:00
|
|
|
IExternalHookFunctions,
|
2020-05-03 23:56:01 -07:00
|
|
|
IExternalHooks,
|
2020-05-04 16:23:54 -07:00
|
|
|
} from './';
|
2020-05-03 23:56:01 -07:00
|
|
|
|
|
|
|
import * as config from '../config';
|
|
|
|
|
|
|
|
// export EXTERNAL_HOOK_FILES=/data/packages/cli/dist/src/externalHooksTemp/test-hooks.js
|
|
|
|
|
|
|
|
class ExternalHooksClass implements IExternalHooks {
|
|
|
|
|
|
|
|
externalHooks: {
|
|
|
|
[key: string]: Array<() => {}>
|
|
|
|
} = {};
|
|
|
|
|
|
|
|
|
|
|
|
async init(): Promise<void> {
|
|
|
|
console.log('ExternalHooks.init');
|
|
|
|
|
2020-05-04 16:23:54 -07:00
|
|
|
const externalHookFiles = config.get('externalHookFiles').split(':');
|
2020-05-03 23:56:01 -07:00
|
|
|
|
|
|
|
console.log('externalHookFiles');
|
|
|
|
console.log(externalHookFiles);
|
|
|
|
|
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 !== '') {
|
|
|
|
console.log(' --- load: ' + 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-04 16:23:54 -07:00
|
|
|
async run(hookName: string, hookParameters?: any[]): Promise<void> { // tslint:disable-line:no-any
|
2020-05-03 23:56:01 -07:00
|
|
|
console.log('RUN NOW: ' + hookName);
|
|
|
|
|
2020-05-04 16:23:54 -07:00
|
|
|
const externalHookFunctions: IExternalHookFunctions = {
|
2020-05-03 23:56:01 -07:00
|
|
|
DbCollections: Db.collections,
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let externalHooksInstance: ExternalHooksClass | undefined;
|
|
|
|
|
|
|
|
export function ExternalHooks(): ExternalHooksClass {
|
|
|
|
if (externalHooksInstance === undefined) {
|
|
|
|
externalHooksInstance = new ExternalHooksClass();
|
|
|
|
}
|
|
|
|
|
|
|
|
return externalHooksInstance;
|
|
|
|
}
|