Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow modify and reload configTrust without restart perseo-fe #757

Closed
AlvaroVega opened this issue Nov 16, 2023 · 2 comments
Closed

allow modify and reload configTrust without restart perseo-fe #757

AlvaroVega opened this issue Nov 16, 2023 · 2 comments

Comments

@AlvaroVega
Copy link
Member

reload configTrust.js

#745

@AlvaroVega
Copy link
Member Author

AlvaroVega commented Nov 16, 2023

Suggestion for implementation:

const fs = require('fs');

const path = require('path');
 
const filePath = './yourModule.js'; // Replace with the path to your module
 
function requireUncached(module) {

    delete require.cache[require.resolve(module)];

    return require(module);

}
 
function watchFile() {

    fs.watchFile(path.resolve(filePath), { persistent: true, interval: 1000 }, (curr, prev) => {

        if (curr.mtime > prev.mtime) {

            console.log(`Reloading module '${filePath}'...`);

            try {

                const updatedModule = requireUncached(filePath);

                // Now you can use the updated module

            } catch (err) {

                console.error('Error reloading module:', err);

            }

        }

    });

}
 
// Start watching the file for changes

watchFile();

In Node.js, you can use the fs (file system) module along with require.cache to achieve dynamic reloading of a local module when it changes. Here's a simple example:

javascriptCopy codeconst fs = require('fs');const path = require('path');
const filePath = './yourModule.js'; // Replace with the path to your modulefunction requireUncached(module) {
    delete require.cache[require.resolve(module)];
    return require(module);
}
function watchFile() {
    fs.watchFile(path.resolve(filePath), { persistent: true, interval: 1000 }, (curr, prev) => {
        if (curr.mtime > prev.mtime) {
            console.log(`Reloading module '${filePath}'...`);
            try {
                const updatedModule = requireUncached(filePath);
                // Now you can use the updated module            } catch (err) {
                console.error('Error reloading module:', err);
            }
        }
    });
}
// Start watching the file for changes
watchFile();

In this example:
The watchFile function uses fs.watchFile to watch for changes to the specified file at the given interval (1000 milliseconds in this case).
When a change is detected (curr.mtime > prev.mtime), it triggers the reloading process.
The requireUncached function removes the module from the cache using delete require.cache[require.resolve(module)], and then re-requires it with require(module).
You can then use the updated module as needed.

@fgalan
Copy link
Member

fgalan commented Nov 30, 2023

Implemented by PR #758

@fgalan fgalan closed this as completed Nov 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants