From e428ccffbae6511c0e3719655ff07b140aca3107 Mon Sep 17 00:00:00 2001 From: Jan Oberhauser Date: Wed, 25 Nov 2020 15:42:33 +0100 Subject: [PATCH] :zap: Make it possible to reload hook-files --- packages/cli/src/ExternalHooks.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/ExternalHooks.ts b/packages/cli/src/ExternalHooks.ts index acdae264ab..89dbc54045 100644 --- a/packages/cli/src/ExternalHooks.ts +++ b/packages/cli/src/ExternalHooks.ts @@ -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 { // 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]; }