1

When my windows server receives a post request, I need it to execute a command:

cd D:\project
git pull
mvn clean compile
cd target
java -jar app.jar -argument

I wrote the code, but it does not work:

const nodeCmd = require('node-cmd')

app.post((req, res) => {
  let command = `cd D:\project\my_project
                 git pull
                 mvn clean compile
                 cd target
                 java -jar app.jar ${req.body.arg}`;
  nodeCmd.get(command, (err, data, stderr) => {
    if(data) { 
     return res.json(data);
    }
    return err;
 });
})

Here is the error message:

{ Error: Command failed: cd D:projectmy_project && dir
The system cannot find the path specified.

    at ChildProcess.exithandler (child_process.js:294:12)
    at ChildProcess.emit (events.js:198:13)
    at maybeClose (internal/child_process.js:982:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)
  killed: false,
  code: 1,
  signal: null,
  cmd: 'cd D:Tempautobooker && dir' }
2
  • Can you show us the error message ? Commented Oct 17, 2019 at 21:50
  • 1
    I have update my question with error stack trace Commented Oct 17, 2019 at 22:01

4 Answers 4

1

The backslashes in your command string are not escaped. You can use string.raw to instruct the JS engine to treat a template literal as a raw string.

let command = String.raw`cd D:\project\my_project
                         git pull
                         mvn clean compile
                         cd target
                         java -jar app.jar ${req.body.arg}`;
Sign up to request clarification or add additional context in comments.

2 Comments

escaping did help, just getting empty response, no errors or anything, tried with simple command cd D:\\projects\\my_project && dir
ok, this one helped now, the problem was as you said with the fact that I am using `
1

I don't know much about running this kind of task in Windows, but I have a suspicion you may need to break these up into multiple calls or && between them.

3 Comments

Tried that as well, no luck
That’s a bit beyond my expertise then, sorry about that. Have you tried using child_process straight up?
I just saw your error stack, you’re not escaping your backslashes `\` in your directory string. Try that.
1

according to node-cmd documentation, your code is perfect , but what I believe is your problem lies in your project path which means your first command is not executed and this because your backslash not escaped, according to your error message your path is projectmy_project which is incorrect, you need to update your path to be like this D:\\project\\my_project

2 Comments

Escaping did not help too, I tried with the simple command: cd D:\\projects\\my_project && dir, which returned empty result, although there are several files and folders.
the empty result doesn't mean your command failed, try to add another command after cd to print the current path, try to add echo %cd% after cd D:\\projects\\my_project , and let us see what it print
0

An alternative to the initial method would be splitting up that command and feeding it into a persistent shell (here's a great example of a persistent shell). I use this solution to work with virtual environments, and in your case it would look like this:

const shell = getPersistentShell();
shell.execCmd('cd D:\project');
shell.execCmd('git pull');
shell.execCmd('mvn clean compile');
shell.execCmd('cd target');
shell.execCmd('java -jar app.jar -argument');
shell.execCmd(`exit`);

console.log(await shell.finalResult);

Aside from the path to the folder, this will run nicely on all platforms.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.