Nodejs projet hot reloader - reload your packages, files and caches easily.
npm i hotreload
const { hotReload, Utils } = require("hotreload");
hotReload
Function(hotReloadOptions)Utils
: all Util Functions:loadAllPaths(paths, functionPaths)
, -> for Each Path: require(path) + execute the first callbackFunction of where the pathGlob matches the current pathfilterStrings(str)
-> matching function to filter an array to only contain valid strings,
interface hotReloadOptions {
excluded?: string[];
onlyReload?: string[];
functionsToLoad?: { pathGlob: string, callbackFunction: string }[];
}[];
excluded
string[] --> array of string-globs to not reload file-paths matching that globonlyReload
string[] --> array of string-globs to only reload file-paths matching that glob | Default: "*" aka everythingfunctionsToLoad
reloadFunctionObject[] --> array of string-glfunctionsToLoad reloadFunctionObject
:pathGlob
: string --> string-glob to execute the callbackFunctioncallbackFunction
: Function(path, pull) -> callback Function to execute, once the pathGlob is matching
interface hotReloadReturnData {
success: string[]; // strings of paths which were "successful"
failed: { path: string, error: TypeError }[]; // path + error for each "failed" reload
}
- You can easily execute scripts upon specific Path-Globs
- With that you can update Caches / Savings of pulls
- With that you can technically restart express apps too!
- You could restart websocket-/EventEmitter listeners (make sure to clear them first)
const { hotReload } = require("hotreload");
const res = await hotReload({
excluded: [ "**/node_modules/**" ], // don't reload stuff with node_modules in path
onlyReload: [ "**/*.js", "**/*.json" ], // only reload stuff which ends with *.js or **/*.json
functionsToLoad: [ // array of objects, with pathGlobs, and callbackFunction, which get's executed when the pathGlob match is true
{
pathGlob: "**/exports/**/*.js", callbackFunction: (path, pull) => { // pull = require(path);
if(path.includes("defaults")) pull.default(); // you can execute any functions from that pull: module.exports = { default: () => {} }
else pull(); // or even direct execute the pull. if that file: module.exports = () => { ... };
}
}
]
})
console.log(`Successfully reloaded ${res.success.length} Paths, and failed on ${res.failed.length}`)
res.failed.forEach(data => console.error(data));
const { hotReload } = require("hotreload");
// if you reload events / listeners, make sure to clear them first!
await client.removeAllListeners();
// reloader function
const res = await hotReload({ // don't reload the node_modules, the bot.js and the sharder Files
excluded: ["**/node_modules/**", `${process.cwd()}/bot.js`, `${process.cwd()}/Shard.js`],
onlyReload: [ "**/*.js", "**/*.json" ], // reload .js and .json files
functionsToLoad: [
{ // simple execute "module.exports () => {}" functions with parameters
pathGlob: `${process.cwd()}/extenders/**`, callbackFunction: (path, pull) => pull(client) // pull = require(path)
},
{ // store pull data in a cache, + use the path to get the filename (aka eventName) + then execute the requirement
pathGlob: `${process.cwd()}/events/**`, callbackFunction: (path, event) => { // pull = require(path)
const eventName = path.split("/").pop().replace(".js", "");
client.eventPaths.set(eventName, { eventName, path: resolve(path) });
event(client);
}
},
{ // just update the cache for client.commands, with the pull ;)
pathGlob: `${process.cwd()}/commands/**`, callbackFunction: (path, cmd) => client.commands.set(cmd.name, cmd); // const pull = require(path);
},
]
});
console.log(res, res.success.length, res.failed.length)