chokidar = require('chokidar');
const commands = new Multimap();
function getOldAndNewModule(modulepath) {
let prev, next
const cache = require.cache[require.resolve(modulepath)]
prev = cache ? cache.exports : null;
delete require.cache[require.resolve(modulepath)];
try {
next = require(modulepath);
} catch {
next = null
}
return { next, prev }
}
function refreshModule(modulepath) {
const { prev, next } = getOldAndNewModule(modulepath)
if (prev) commands.delete(prev.name, prev);
if (next) commands.set(next.name, next);
}
const root = path.resolve("modules")
const watcher = chokidar.watch(root, { persistent: true, depth: 2, ignoreInitial: true })
watcher.on("all", (event, path) => {
console.log(path)
refreshModule(path)
show()
});
fs.readdirSync(root).forEach(function (name) {
const fullname = path.join(root, name)
refreshModule(fullname)
})
show()
function show() {
console.log("------------------")
for (const key of commands.keys()) {
const handlers = commands.get(key)
console.log(key, "=>", handlers)
console.log("Executing")
handlers.forEach(x => x.run())
}
console.log("------------------")
}
Тут правильная
Обсуждают сегодня