Skip to content

Instantly share code, notes, and snippets.

@goliatone
Created April 8, 2020 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goliatone/6ff39962e885af674efcbaf035567e86 to your computer and use it in GitHub Desktop.
Save goliatone/6ff39962e885af674efcbaf035567e86 to your computer and use it in GitHub Desktop.
'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