Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -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')

// 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'))
Expand All @@ -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
Expand Down Expand Up @@ -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 when module updated <optional>
*/
function nocache(module, cb = () => { }) {
console.log('Module', `'${module}'`, 'is now being watched for changes')
require('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))