Created
April 8, 2020 18:09
-
-
Save goliatone/6ff39962e885af674efcbaf035567e86 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
/** | |
* This plugin add hot command reload by watching | |
* file changes on any file under `./commands` and | |
* issuing a `reloadCommand` to the context. | |
* This | |
*/ | |
module.exports.init = function(context, config) { | |
if (process.env.NODE_ENV !== 'development') return {}; | |
const logger = context.getLogger(config.moduleid); | |
context.once('modules.resolved', _ => { | |
logger.info('Initialize command module reloader!'); | |
/** | |
* Should we provide this as a facility at the | |
* app level? | |
*/ | |
const watcher = require('chokidar'); | |
const commandReload = watcher.watch(context._commandspath, { | |
depth: 0, | |
ignoreInitial: false, | |
awaitForFinish: true | |
}); | |
// context.commandReloadWatcher = commandReload; | |
commandReload.on('change', (filepath, stats) => { | |
logger.info('Command updated: %s', filepath); | |
context.reloadCommand(filepath, requireUncached(filepath)); | |
}); | |
function requireUncached(mdl) { | |
delete require.cache[require.resolve(mdl)]; | |
return require(mdl); | |
} | |
}); | |
return {}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment