how to control where spawnCommand method executes - node.js

How can I understand and control from where spawnCommand runs?
I am creating a yo generator and as part of the process I need to call an external tool. The way I am attmepting to do this is by calling a powershell script from spawnCommand. I was having trouble with spawnCommand having spaces in paths, so I just encapsulated that within the powershell file. I saw something about superspawn but couldn't get it to work.
Anyway, my main problem is that I am trying to call powershell and give it the script file as an argument, but it can't find the script file. Here is the code:
signatureKeyFile: function() {
var done = this.async();
this.spawnCommand('powershell', ['sign.ps1'], {
cwd: this.destinationPath('.')
}).on('close', done);
},
I tried using procmon to see what directory it executes from and understand it better. It looks like it tries it in a ton of places, but seemingly all environment paths. It doesn't try the source or destination path for the yo generator, or at least that's how it seemed to me.
So my questions are
1) which working directory does spawnCommand run from by default?
2) How can I contorl that so it can find the this powershell file? spawnCommand won't let me use spaces in paths, so I can't feed it a full path or so it seems to me.
Thanks so much!

spawnCommand won't let me use spaces in paths, so I can't feed it a full path or so it seems to me.
That's usually a well known issues of command line. Arguments are separated by spaces, so it just assume your path is actually multiples arguments.
I'm not 100% for windows, but on OSx, you'd espace spaces as some\ path\ with\ spaces.
Another options is to pass arguments as strings. What if you were to wrap your path in double quotes?
this.spawnCommand('powershell', ['"/home/My Home/sign.ps1"']);
FWIW, Yeoman spawnCommand is just a wrapper for Node spawn command where we help normalize a command so it'll run fine on windows. See
https://github.com/yeoman/generator/blob/master/lib/actions/spawn-command.js#L20
https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options
It'll help you to test the correct syntax to pass path if you can reduce the scope of your problem to only the Node spawn command.
[Edited to fix broken link]

We were able to solve it by setting the current working directory or cwd, with the spaces:
this.spawnCommandSync('sn.exe', ['-k', format("{0}\\{1}.snk", this.destinationPath('.'), this.props.myItemName)], {
cwd: 'C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v10.0A\\bin\\NETFX 4.6 Tools'
});

Related

Node and npm are installed but Git bash doesn't recognize 'node' inside 'npm run dev' (other shells work fine)

I have npm and node installed (tried NVM for Windows and direct installations).
When running 'npm -v' or 'node -v' in Git Bash everythings works fine. But when I try to run 'npm run dev' (or any other command) the output says that 'node command is not recognized'.
In other shells (CMD/Powershell) everything works fine.
I've checked Path variable in Windows, Path variable in Git Bash, everything seems to be correct.
Error screenshot
Path variable screenshot
Path variables cmd/procmon
Any help appreciated.
p.s. While I was trying to find an answer I saw the same question from #jameseg , maybe if he sees this one he could help.
Your PATH environment variable is quite a mess. It has duplicate entries, also has an entry C:\Program Files\nodejs\node.exe which is not valid because it should be a folder, not a file, and it has . in the middle which doesn't make much sense either.
But the main problem is that it has a stray doublequote, after C:\Program Files\Java\jdk-13.0.1\bin:
With this, effectively all the paths after it are ignored, because they are treated as part of one big quoted string (which is implicitly terminated by the end of the variable data).
To illustrate what I mean, consider this example:
This correct PATH variable...
C:\a;C:\b;"C:\c 123";C:\d;C:\e
...is interpreted as:
C:\a
C:\b
C:\c 123
C:\d
C:\e
But, this bad PATH variable where I deleted one of the quotes...
C:\a;C:\b;C:\c 123";C:\d;C:\e
...is interpreted like this:
C:\a
C:\b
C:\c 123";C:\d;C:\e
This may at first make only half sense, but it's because of the quirky way Windows parses this variable: When encountering a doublequote, it's removed from the result but toggles a flag that says whether we are now inside a quoted string. And when the flag is set, semicolons are ignored. So even if the stray quote is at the end of a path (or in the middle of it), it will have the effect of essentially quoting the rest of the variable data until the next doublequote or the end of the data.
Confusingly, you may still have where node report that it found node, because the where.exe tool does its own parsing, in a slightly different way (ignoring quotes), so you cannot rely on its output! (For example, try set PATH=c:\win""dows. where explorer will say it can't be found, yet explorer will work to open Explorer. You get the opposite with something like set PATH=x"y;c:\windows - where explorer will list c:\windows\explorer.exe, yet explorer will not work.) The reason why it works in Git Bash is probably the same: when the environment variables are translated to UNIX paths, they are parsed slightly differently than Windows would do it itself, inadvertently correcting the problematic entry in the process.
So, the solution is to remove this doublequote from your path variable.

Unable to execute shell script using deploy command in jboss

I have been looking for solutions to this problem for a couple of days and have not found a suitable one.
Currently i have two lines of code in my shell script file.
cd /app/jboss/sample_project/bin
./jboss-cli.sh -c --controller=$hostnameVal:$jboss_port_no1 --user=$jboss_id --password=$jboss_pwd --command="deploy --force /sample/uploaded/sample-1.0.war --runtime-name=sample-1.0.war"
I am facing this error:
'--force' is assumed to be a command(s) but the commands to execute have been specified by another argument: [deploy]
I think it has something to do with the spaces between deploy and --force but I can't seem to find a solution to this. Would really appreciate if someone can shed some light. Thank you!
The root cause of the issue was due to the space between the deploy command and the file path, therefore jboss was not be able to find the parameter the properly.
Hence, a work around for this which I have found out is to create a cli file, write the deploy command in the cli file and pass the file as an input in the script that i am writing.
Do you observe the same problem in a terminal?

Node app treats escaped chars in path treated differently when installed globally

I am very new to this Node stuff so there is probably a really simple answer to this but anyways...
I am building an app that prompts for a file path. This path is supplied by dropping a file into the Terminal window which gives a path with spaces escaped like this:
Users/[username]/Desktop/test\ file.txt
I then use jsonfile to add this path to an array and write out a JSON file. In this file the path now has the escape escaped and looks like this:
Users/[username]/Desktop/test\\ file.txt
Now I want to grab that path from the JSON and reveal it in Finder and this is where I get lost. I am using mac-open to reveal the path by passing the -R option and this works fine while I am testing and running my code using node bin/my-code.js but as soon I install with npm install -g . it breaks. The install works and I can run the app (there are other functions that work fine) but now if I try to reveal the path I get the error:
{ [Error: Command failed: /bin/sh -c open -a "Finder" -R "/Users/[username]/Desktop/test\\\ file.txt"
The file /Users/[username]/Desktop/test\\ file.txt does not exist.
]
killed: false,
code: 1,
signal: null,
cmd: '/bin/sh -c open -a "Finder" -R "/Users/[username]/Desktop/test\\\\\\ file.txt"' }
I have noticed the varying amounts of escapes in the different path references in the error so my questions are:
Why is this different when I test using node bin/my-code.js
and when I install globally and run that way?
Is there a way that I can make the two behave the same as I don't really want to be installing every time I want to test my code?
Is there a more robust way of storing and/or escaping file paths?
Sorry this has got a bit rambling. Any advice welcome :)
So I haven't exactly answered all the questions I posed but I have fixed the issue and learned what I consider a really valuable lesson in Node development.
Trying to find the problem I actually open the source mac-open and in doing so realised that I didn't actually need to be using the additional module at all. Searching the source led me to look into reading about child_process and eventually sacking off the whole mac-open module for a simple:
var exec = require("child_process").exec;
exec("open " +assetsPath +" -R", function(err){
if(err)
console.log(err);
});
The esson here is not to use 3rd party modules just because they are there. In this situation I didn't need the all-situation covereage that it was trying to provide and the extra complexity actually caused the problem.

Linux shell commands not found even though their paths are listed in the PATH variable

The google app engine cli commands cannot be found in the python sdk. I've already checked the google_appengine file. The shell commands are there, and I already added the app engine file path to the PATH variable. I echo the PATH variable and the directory to the app_engine file shows up, but still shows not found when I try to use a command that's inside the file. If I reference the command directly by using its whole path, the command works, but otherwise it won't. Is there anything else that needs to be done to reference a command in shell?
More error info please,
If I reference the command directly by using its whole path, the command works
I think your python path maybe wrong, Python 2.6 is not supported.
so are you have mulit-version python?
Thank you for your contributions everyone. It turns out the path I was using was wrong. Instead of /home/Programs/Apps/google_appengine it should have been /home/Programs/google_appengine. I guess the shell doesn't check to see if the path you add to the PATH variable actually exists. Who knew? Not me
Until I run into another problem stack overflow--which may be soon
See ya

node.js does not respect all paths in $PATH variable

This question is related to
Nodejs Child Process with Unix Executable.
However I could not find a good solution and thanks to my reputation points, I am unable to post a comment there.
To explain my problem, I have some of my own executables which I need to execute from node.js. The path for these executables is already set in the PATH variable. Hence, I can run my executables without any path-prefix from Terminal.
Inspite of that, node.js receives process.env.PATH set to following value:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
I tried appending the path to it before spawning the process:
process.env.PATH = process.env.PATH + ":<path_to_my_executables>"
but the process exits with 127 Shell code which is "command not found" if I correctly remember.
Any ideas?
Thank you in advance!
Okay, I got it work using a shell script. Definitely there must be a better solution to this.
I exported the required paths to my executable (and its dependencies) in the shell script first and then started execution. Hence the shell script structure is something like:
export JAVA_HOME=<java_path>
export EXECUTABLES_HOME=<executables_path>
export PATH="$PATH:$JAVA_HOME/bin:$EXECUTABLES_HOME"
<execute_required_executable> <arguments>
If someone has a better solution, please do let me know!

Resources