diff --git a/packages/cli/src/ExternalHooks.ts b/packages/cli/src/ExternalHooks.ts index 89dbc54045..aa14662a60 100644 --- a/packages/cli/src/ExternalHooks.ts +++ b/packages/cli/src/ExternalHooks.ts @@ -1,6 +1,7 @@ import { Db, IExternalHooksClass, + IExternalHooksFileData, IExternalHooksFunctions, } from './'; @@ -26,9 +27,14 @@ class ExternalHooksClass implements IExternalHooksClass { } - async reload() { + async reload(externalHooks?: IExternalHooksFileData) { this.externalHooks = {}; - await this.loadHooksFiles(true); + + if (externalHooks === undefined) { + await this.loadHooksFiles(true); + } else { + this.loadHooks(externalHooks); + } } @@ -45,20 +51,8 @@ class ExternalHooksClass implements IExternalHooksClass { delete require.cache[require.resolve(hookFilePath)]; } - 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]); - } - } + const hookFile = require(hookFilePath) as IExternalHooksFileData; + this.loadHooks(hookFile); } catch (error) { throw new Error(`Problem loading external hook file "${hookFilePath}": ${error.message}`); } @@ -67,6 +61,22 @@ class ExternalHooksClass implements IExternalHooksClass { } + 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] = []; + } + + this.externalHooks[hookString].push.apply(this.externalHooks[hookString], hookFileData[resource][operation]); + } + } + } + + async run(hookName: string, hookParameters?: any[]): Promise { // tslint:disable-line:no-any const externalHookFunctions: IExternalHooksFunctions = { dbCollections: Db.collections, diff --git a/packages/cli/src/Interfaces.ts b/packages/cli/src/Interfaces.ts index b808a73564..443bee6539 100644 --- a/packages/cli/src/Interfaces.ts +++ b/packages/cli/src/Interfaces.ts @@ -219,6 +219,12 @@ export interface IExternalHooks { }; } +export interface IExternalHooksFileData { + [key: string]: { + [key: string]: Array<(...args: any[]) => Promise>; //tslint:disable-line:no-any + }; +} + export interface IExternalHooksFunctions { dbCollections: IDatabaseCollections; }