-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
feat(core): add server:watch
script with automatic restart on file changes
#3920
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
base: develop
Are you sure you want to change the base?
feat(core): add server:watch
script with automatic restart on file changes
#3920
Conversation
You can reload the browser side anytime, ctl-r, or f5 it only loads the web side. You have to stop server and start to reload node_helpers |
What does server dev do? dev was only for web side if you open your own browser it doesn’t get that parm |
The scripted installer handles node install plus other things |
Yes, but stopping the server and then restarting it quickly becomes redundant when we make a lot of modifications and tests on the module side in development.... |
Yes, but stopping the server and then restarting it quickly becomes redundant when we make a lot of modifications and tests on the module side in development....
I don't understand |
So again, change module, ctrl-r if cursor is over web side, f5 if over dev window side what does server:dev do? in run server mode dev mode curl-shift-I , toggles the dev window open or closed |
To start MagicMirror in server mode + Nodemon keeps an eye on server-side files, and whenever a change is detected the server restarts automatically. |
Ah |
.nvmrc
file and create server:dev
scriptserver:dev
script
Thanks for the changes 🙂 I would like to test something relevant. Give me some time for the review. |
…g and port management - Add port availability checks before restart to prevent race conditions - Add error handler for spawn failures - Extract hardcoded values to constants (RESTART_DELAY_MS, PORT_CHECK_*) - Add getConfigFilePath() helper for cleaner config path resolution - Watch js/ and serveronly/ directories in addition to modules/ and config/ - Watch all JS files (.js, .mjs, .cjs) instead of just node_helper.js - Improve restart mechanism with isRestarting flag - Better error messages for watcher and spawn failures - Remove unused app.restart() method from app.js These changes make the watcher more robust and easier to maintain.
206421c
to
c2103f5
Compare
server:watch
scriptserver:watch
script with automatic restart on file changes
I have re-based the branch. During my tests, I encountered various issues. For example, the restart was triggered by files that my editor created temporarily when saving. Furthermore, the restart did not wait until the old instance released the port, so the new instance could not use it. I have adjusted this and other details in one commit. Please feel free to give feedback on my changes. From my point of view, everything looks good now 😃 However, I would wait for the opinion of at least one other contributor on the current status. Edit: I removed the manual restart option since it would be another feature. One feature per PR is easier to handle. |
Nice jobs ! Thanks for the changes ! I see that I don't have the whole history of the project when I see all the changes you made 🫣 Ok for the removal of manual reload and sorry for my english in But i don't understand why on your local you have this bug with my version of watcher 🤔 Edit: I change the description of the PR |
// We watch specific directories instead of the whole project root to avoid | ||
// watching unnecessary files like node_modules (even though we filter it), | ||
// tests, translations, css, fonts, vendor, etc. | ||
watchDir(path.join(__dirname, "..", "modules")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpicking stuff from my side ...
AFAIR only modules/default
is fix, the modules path for 3rd-party can be changed in config.js
(see foreignModulesDir
defined in defaults.js
). There is an environment variable MM_MODULES_DIR
too. You can search for modulesDir
in the project to see the construction ...
Other point: You excluded css in the comment above, but what about custom.css
? If this file should be watched: Same construction as for modules, customCss
in defaults.js
, env var MM_CUSTOMCSS_FILE
...
So better approach here would be (untested):
const { getEnvVarsAsObj } = require("#server_functions");
const env = getEnvVarsAsObj();
watchDir(path.join(__dirname, "..", env.modulesDir));
watchDir(path.join(__dirname, "..", env.customCss));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You excluded css in the comment above, but what about custom.css?
Hm... If the CSS changes and the watcher restarts the server, the frontend would not restart/reload. That's why I removed the CSS monitoring, as it would not make sense with the current logic.
AFAIR only modules/default is fix, the modules path for 3rd-party can be changed in config.js (see foreignModulesDir defined in defaults.js). ...
Good point. That should be manageable.
I just had an idea for how to create a common watcher that would work in both server and electron mode and can reload the browser/electron. I'll give it a try, but that needs some refactoring.
I think a watch mode also for electron would definitely be nice. Don't you think? I'll try to look into that over the next few days.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the frontend will restart after server restart (with a little delay), AFAIR this was implemented some times ago. I use this in my setup (Browser gets refreshed when docker container was restarted).
Watching/Restarting is also possible with pm2 ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the frontend will restart after server restart
Okay, I'll test it. My previous tests were probably too short.
Watching/Restarting is also possible with pm2 ...
Or do you think a watch mode in the core is unnecessary? I find it kind of interesting to be able to do this without nodemon or pm2. But of course, we don't want the core to complex.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pm2 is already in this project (not happy with this because I remove this when building the container images) so was only meant as hint before reinventing the wheel ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hence the fact that I proposed Nodemon to have everything already built without reinventing the wheel.
Nodemon already handles these reloading aspects for CSS, js, etc.
What I'm afraid of is that the watcher.js
will become complex... Nodemon can be implemented in devDependencies
and therefore not installed during a Prod installation?
Description
This pull request aims to add new features for contribution as well as local use :
npm run server:watch
➡️ This script run server only with live reload support for usage on development mode. Example for developers who develop modules and want to update them automatically when a change occurs 🚀Test this pull-request
Hot reload
npm run server:watch