1

I'm developing a discord bot for discord at the moment, and to run the bot, I have a few different files. I've run into some problems along the way that all ended up having the same fix, or at least the only fix that I could come up with. That fix would be, instead of running them in my main file (index.js), I could just run 3 separate batch files using nodemon (https://www.npmjs.com/package/nodemon). I currently have 3 batch files to do so. Those batch files look like this:

nodemon index.js
pause

This would be for the main file.

nodemon logs.js
pause

This would be for the file that logs messages.

nodemon welcome and goodbye.js
pause

And this would be for the welcome and goodbye logs.

The only issue is, is that it clutters up my desktop with 3 different prompts, making it confusing as to what does what. I was wondering if it'd be possible with nodemon (or any other npm like this) for me to run all 3 of those batch files in one single command prompt.

And of course I'm open to other npms, but if I'd have to use another one, please include a way for it to automatically restart the server when I save the file in it as well (if possible). Since that's the main reason I like nodemon. I'd also like to be able to use a command prompt/batch file rather than the Visual Studio Code terminal.

If you're wondering what I mean when I say, "...automatically restart the server when I save the file," I mean this. (https://i.stack.imgur.com/fMk4y.jpg if it doesn't show, don't know how to format this stuff) Whenever I save my code in Visual Studio it will restart and load the new code.

I'm still a little new to coding and stuff like that, so if my terminology is off I do apologize. If I did use a term incorrectly and you are confused/have a question about it, leave a comment and I will do my best too explain what I mean.

Thank you for taking time to read this.

Peppa Papa
  • 85
  • 4
  • 13

2 Answers2

4

Welcome :-)

I think you mean bash file (.sh) instead of batch file. So, in the terminal, you can && commands to chain them together.

For example, you could chain them together like this:

nodemon ./appOne/index.js && nodemon ./appTwo/index.js && nodemon welcome and goodbye.js

and they will all execute at one time. Additionally, you could make a bash file somewhere on your computer that did exactly what I typed above :-)

Now if you wanted them to go to the background and you wanted to forget about them, you could put them in the background with one ampersand... like so:

nodemon index.js &

and it would disappear and you'd have your console back. It would also print out a process ID (PID) of what that node process is under, so you can find it and kill it later (via kill -9 processid)

Jess
  • 3,097
  • 2
  • 16
  • 42
  • 1
    ```nodemon ./appOne/index.js && nodemon ./appTwo/logs.js && nodemon welcome and goodbye.js``` So like this? Or would I change the ```./appOne``` stuff to be the whole directory or something else? And then would I save it as a .bat file? That is what I did with the others at first. – Peppa Papa Nov 17 '19 at 07:52
  • 2
    Oh my. You're on windows! .bat _is_ a batch file. Ahhh. Everything I wrote is probably wrong. – Jess Nov 17 '19 at 07:53
  • Gimme a hot second. – Jess Nov 17 '19 at 07:53
  • 1
    For windows, single ampersand will chain commands together regardless of the command's success/failure `&` https://stackoverflow.com/q/8055371/3622606 – Jess Nov 17 '19 at 07:55
  • Generally, you can just keep making script files though -- .bat, .sh, whatever. Script files can execute other script files. If you're annoyed with how many commands you have to type, wrap it all up in a nice script file. – Jess Nov 17 '19 at 07:58
  • 1
    Okay well the first one you sent did work so thank you. And I don't mind it being there since I still use the rs command sometimes. It just got confusing with which window was which. Thanks a bunch for the quick replies, especially with how late it is. :) – Peppa Papa Nov 17 '19 at 08:05
4

You can use concurrently or parallel in order to make it cross platform

example with concurrently:

package.json
{
  "scripts": {
    "serve": "nodemon index.js",
    "logs": "nodemon logs.js",
    "start": "concurrently \"npm:serve\" \"npm:logs\""
  },
  "devDependencies": {
    "concurrently": "^5.0.0",
  }
}

Edit: you may want to take a look at the VSCode Compound tasks

Here's an example setup

Teneff
  • 30,564
  • 13
  • 72
  • 103