Dear maintainers,
Durning my development process of this project, I have to restart the programme if the codes are changed. Is there any way to implement features such as hot load?
It is found that there is a dev.ts in package.json. Would you please show the contents of it?
Or, is there any way to save sessions so that it will not require QR code scanning while restarting?
Dear @imWildCat ,
There's many ways to "Hot Reload" javascript source code, one of them is import the function from your code file, and re-import it if it updated.
An example should like this: (just write from scratch, not tested)
let hotOnMessage = require('./hotOnMessage')
fs.watch('./hotOnMessage.js', (eventType, filename) => {
if (eventType === 'change') {
hotReload(filename)
}
})
function hotReload(filename) {
// http://stackoverflow.com/a/20790238/1123955
delete require.cache[filename]
hotOnMessage = require('./hotOnMessage')
}
Wechaty.instance()
.on('message', msg => hotOnMessage(msg))
If you could make it work, I'd like to invite you to write an example/hot-reload-bot.ts for Wechaty, because that's also what I want, and will help others as well.
sessions so that it will not require scan QR Code each timeWechaty already support that: use profile to define your bot profile name in Wechaty.instance().
An example should like this:
Wechaty.instance({ profile: 'my-bot-name-which-will-save-session' })
You can have a look inside example directory, almost all bots support that already. There's also a known bug which will cause the session reload fail: sometimes you will be prompted to scan the QR Code again even you specified profile.
Please let me know if you have any other questions, hope you can make hot reloading work soon!
@zixia , many thanks for your detailed response! Have a nice day!
Thanks for the implemention PR from @Gcaufy !
Example is at here: https://github.com/wechaty/wechaty/tree/master/example/hot-reload-bot
Most helpful comment
Thanks for the implemention PR from @Gcaufy !
Example is at here: https://github.com/wechaty/wechaty/tree/master/example/hot-reload-bot