From 6d1ec017976b4aa0f2fc27abf617371a13e40f8a Mon Sep 17 00:00:00 2001 From: Nurutomo Date: Wed, 14 Oct 2020 22:58:34 +0700 Subject: [PATCH 1/2] Allowing './handler/message' to auto-update Re-cache handler if you change the code while the bot is running --- index.js | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 8631bb2..ce93f1a 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,10 @@ const { create, Client } = require('@open-wa/wa-automate') const { color } = require('./utils') const options = require('./utils/options') -const msgHandler = require('./handler/message') +const fs = require('fs') +// Cache handler and check for file change +require('./handler/message') +nocache('./handler/message', module => console.log(`'${module}' Updated!`)) const start = (client = new Client()) => { console.log('[DEV]', color('Red Emperor', 'yellow')) @@ -22,8 +25,8 @@ const start = (client = new Client()) => { client.cutMsgCache() } }) - // Message Handler - msgHandler(client, message) + // Message Handler (Loaded from recent cache) + require('./handler/message')(client, message) }) // listen group invitation @@ -53,6 +56,34 @@ const start = (client = new Client()) => { }) } +/** + * Uncache if there is file change + * @param {string} module Module name or path + * @param {function} cb + */ +function nocache(module, cb = () => { }) { + console.log('Module', `'${module}'`, 'is now being watched for changes') + fs.watchFile(require.resolve(module), async () => { + await uncache(require.resolve(module)) + cb(module) + }) +} + +/** + * Uncache a module + * @param {string} module Module name or path + */ +function uncache(module = '.') { + return new Promise((resolve, reject) => { + try { + delete require.cache[require.resolve(module)] + resolve() + } catch (e) { + reject(e) + } + }) +} + create('Imperial', options(true, start)) .then((client) => start(client)) .catch((err) => new Error(err)) From 0c431e666408ae8ab109cd8e0b016caacf2bcd01 Mon Sep 17 00:00:00 2001 From: Nurutomo Date: Wed, 14 Oct 2020 23:04:19 +0700 Subject: [PATCH 2/2] fix typing --- index.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index ce93f1a..8417282 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,8 @@ const { create, Client } = require('@open-wa/wa-automate') const { color } = require('./utils') const options = require('./utils/options') -const fs = require('fs') -// Cache handler and check for file change + +// cache handler and check for file change require('./handler/message') nocache('./handler/message', module => console.log(`'${module}' Updated!`)) @@ -57,21 +57,21 @@ const start = (client = new Client()) => { } /** - * Uncache if there is file change - * @param {string} module Module name or path - * @param {function} cb + * uncache if there is file change + * @param {string} module module name or path + * @param {function} cb when module updated */ function nocache(module, cb = () => { }) { console.log('Module', `'${module}'`, 'is now being watched for changes') - fs.watchFile(require.resolve(module), async () => { + require('fs').watchFile(require.resolve(module), async () => { await uncache(require.resolve(module)) cb(module) }) } /** - * Uncache a module - * @param {string} module Module name or path + * uncache a module + * @param {string} module module name or path */ function uncache(module = '.') { return new Promise((resolve, reject) => {