remy / nodemon

Monitor for any changes in your node.js application and automatically restart the server - perfect for development
http://nodemon.io/
MIT License
26.31k stars 1.73k forks source link

A way to use shell functions #2069

Closed kqvanity closed 2 years ago

kqvanity commented 2 years ago

Expected behaviour

Nodemon to execute a shell function that compile a java file named passed to it, then the functions also runs the resulting class file with its filename truncated (i.e. excluding the .class file extension]. It would be really useful if such a feature of having the file name passed have a respective placeholder at the json file, for other manipulations. I usually use temporary source files without build systems or single makes files to carry the heavy lifting.

javab()
{
   local javafile="$1";
   javac ${javafile} && java $javafile%.*}
}

Actual behaviour

An error stating that

[nodemon] 2.0.20
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: java,json
[nodemon] starting `javab Animal.java`
sh: 1: javab: not found
[nodemon] failed to start process, "javab" exec not found
[nodemon] Error
    at Bus.<anonymous> (/home/hostmachine/.local/lib/node_modules/nodemon/lib/nodemon.js:158:25)
    at Bus.emit (node:events:539:35)
    at ChildProcess.<anonymous> (/home/hostmachine/.local/lib/node_modules/nodemon/lib/monitor/run.js:190:11)
    at ChildProcess.emit (node:events:527:28)
    at Process.ChildProcess._handle.onexit (node:internal/child_process:291:12)

Also specifying bash -c java in ~/nodemon.json results in a similar error.

Steps to reproduce

Have a shell function, then try to assign it to a file extension in the ~/nodemon.json file. Try to run a java file afterwards, and it won't work.

remy commented 2 years ago

This is a feature of your shell, not nodemon. In that a new (sub) shell session doesn't inherit your functions.

This can be tested by writing a vanilla shell script - you'll find you can't call your javab function.

I can't remember how you get around it, but I'd start looking there.

kqvanity commented 2 years ago

This is a feature of your shell, not nodemon. In that a new (sub) shell session doesn't inherit your functions.

I did export the function using export -f javab after defining it, so that other child process including subshells can read it. I've also tried spawning one from within the shell session, and it did work with them.

remy commented 2 years ago

Can you share the node script you used to spawn to then call your function? I can use it for my own test and if it's something that's fixable, I'll reopen this ticket and see what we can do.

kqvanity commented 2 years ago

Can you share the node script you used to spawn to then call your function?

I haven't used one at all. I've just defined the function & exported it at my bashrc. Invoked another shell session, then attempted to issue the function from within the initial shell, then a child one, and it worked in both cases.

~/.bashrc entails

function javab(){
   local javafile="$1";
   javac ${javafile} && java $javafile%.*}
} && export -f javab

~/nodemon.json

{
  "execMap": {
    "java": "bash -c javab",
  }
}

node script

const { spawn } = require('node:child_process')
spawn('bash', ['-c', 'javab VariableTypes.java']).stdout
    .on('data', (data) => {
        console.log(data.toString())
    })