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

Force a full restart? #2186

Closed
mauroobento opened this issue Feb 23, 2024 · 7 comments
Closed

Force a full restart? #2186

mauroobento opened this issue Feb 23, 2024 · 7 comments
Labels
discussion / question stale no activity for 2 weeks

Comments

@mauroobento
Copy link

mauroobento commented Feb 23, 2024

TL;DR

1 how to run a yarn command ONCE when nodemon restart?
2 how to change the output color?


Hey ...

not really sure if this is a real issue or something, but more a help / helping hand to solve probably a recurrent question of mine. Imagine the following scenario

You have a some file that nodemon can't really see its changes, but this file needs to be ran thru some other script in order to see the changes. So you might think "oh, ok, i will create a nodemon.json file, add this script to the restart event and it will be ran every time nodemon restarts...

Right? So... not quite... If I add a simple command yarn docs, where docs is to swagger-cli to regenerate json from yaml files, it runs forever and ever, until eventually crashes... Then you might think ""humm... You can add this same command in start event in nodemon.json file, so it will ran anyway"" ... And crashes too because it will build up the server, then crashes due using the same port, then keeps in this loop. Then you think ""ok ok, we have all this hassle... Simply split your terminal, one side running the start:dev command and the other one to regenerate the docs...""... And, well... Nodemon goes crazy restarting the server all the time endlessly if you have the nodemon.json file with yarn docs in any of the events...

Then you might say ""Ok, add the yaml yml extensions to the options.extension part, so every time you save a yaml file, it will trigger the restart event and you will be fine... but no, it doesn't work... So, I'm kind lost of ways to auto run this script to regenerate the json file every time I save it instead forcing stop and rerun the server.

  • nodemon -v: ^3.0.3
  • node -v: v20.10.0
  • Operating system/terminal environment: MacOS Ventura 13.4
  • Using Docker? What image: Yes, but only for MySQL mysql:8.3.0
  • Command you ran: "swagger-cli bundle src/swagger/index.yaml --outfile src/swagger/index.json"

Expected behaviour

Run the command only once and code

Actual behaviour

Restarts the server until it crashes

Steps to reproduce

nodemon.json file

{
  "restartable": "rs",
  "ignore": [".git", "node_modules/**/node_modules"],
  "verbose": true,
  "script": "src/index.js",
  "execMap": {
    "js": "node --harmony"
    "js": "yarn docs && node --harmony" //reloads the application until it crashes
    "js": "node --harmony && yarn docs" // nothing happens
  },
  "events": {
    "restart": "yarn docs", // restarts endlessly
    "restart": "yarn docs && clear && .... ", // restarts endlessly
    "restart": "clear && yarn docs && ...", // restarts endlessly
    "restart": "yarn docs && echo \" restart\" ", // restarts endlessly
    "crash": "osascript -e 'display notification \"App restarted due to:\n'$FILENAME'\" with title \"nodemon\"'",
    "start": "echo \"Application started\"",
    "exit": "echo \"Application is done\" ",
  },
  "watch": [],
  "env": {
    "NODE_ENV": "development"
  },
  "options": {
    "extensions": "js json yaml yml"
  }
}

And... two quick questions.... I didn't quite get the part in how to get the colors in the terminal... I know there is in the docs here but I still haven't figured it out how to really use it properly. Reading this reply, it says I should hookup the events that emits and customize the color it doesn't works (most likely I'm doing something wrong). See last line in nodemon.json right above, last line of restart event.

And, second one is, how to use nodemon as a module? I tried here in order to see if I could use exec to force a full restart or something but it didn't worked. Following this readme here. I kind tried doing some small modifications but nothing worked as expected

const { exec } = require('child_process');
const nodemon = require('nodemon');

nodemon({
  script: 'src/app.js',
  ext: 'js json yaml yml',
  stdout: true,
  signal: rs,
  colours: {
    reset: '\x1b[0m',
    bright: '\x1b[1m',
    dim: '\x1b[2m',
    underscore: '\x1b[4m',
    blink: '\x1b[5m',
    reverse: '\x1b[7m',
    hidden: '\x1b[8m',
    red: '\x1b[41m',
  },
})
  .on('start', function () {
    console.log( 
nodemon['colours'].red,  //doesn't change the color
colours.red,  //doesn't change the color
nodemon('colours').red, //doesn't change the color
nodemon({ colours: red }) //doesn't change the color
nodemon.colours.red //doesn't change the color
'nodemon starting application from js');
  })
  .on('crash', function () {
    console.log('script crashed for some reason from js');
  })
  .on('restart', function () {
    console.log('Application restarted from js'.red.bold);
  });

const n = 'yarn run docs';
// force a restart
nodemon.emit('restart', function () {
  console.log('Restart nodemon emit');
  nodemon.reset();

  exec('yarn run docs', (error, stdout, stderror) => {
    if (error) console.log('\x1b[31m', `docs generated from yarn`);
  });
});

// force a quit
nodemon.emit('quit');

// crash
nodemon.emit('crash');

nodemon.reset(() => {
  console.log('nodemon force reset');
});

I went to the log.js file to understand how it is used but its completely different from docs... So, no use at all


Sorry for huge question =)

Copy link

github-actions bot commented Mar 9, 2024

This issue has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automtically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and @remy will (try!) to follow up.
Thank you for contributing <3

@github-actions github-actions bot added the stale no activity for 2 weeks label Mar 9, 2024
@mauroobento
Copy link
Author

@remy any thoughts here?

@github-actions github-actions bot removed the stale no activity for 2 weeks label Mar 13, 2024
@remy
Copy link
Owner

remy commented Mar 25, 2024

Rewinding right up to your original problem - isn't it dropping into a loop because nodemon is also watching json files, and your generated output is a json file?

Equally, have you tried ignoring the output directory in nodemon so that you can hook the restart and generate your docs (via the nodemon.json)?

@mauroobento
Copy link
Author

Actually, for some reason, nodemon can't even ""see"" these files for some reason... The json and yaml files are present in the project but, when I save them, nodemon does literally nothin. I can even record my screen so you can see it.

Since nodemon can't ""see"" the folder, ignoring or not the folder makes 0 difference. The json isn't the real problem here. The main issue is the need to run a npm command right after being saved, which doesn't happen. At least not in the way I was imagining or something alike. I can create a sample project here easily so you can play around as much as you want and still, nothing will be different.

@remy
Copy link
Owner

remy commented Mar 25, 2024

Can you make a very, very pared down version that shows the problem, then we can see where the problem is.

Copy link

github-actions bot commented Apr 8, 2024

This issue has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automtically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and @remy will (try!) to follow up.
Thank you for contributing <3

@github-actions github-actions bot added the stale no activity for 2 weeks label Apr 8, 2024
Copy link

github-actions bot commented May 8, 2024

Automatically closing this issue due to lack of activity

@github-actions github-actions bot closed this as completed May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion / question stale no activity for 2 weeks
Projects
None yet
Development

No branches or pull requests

2 participants