Node: Move the current directory of the interactive shell - node.js

I'm writing a little CLI tool for Node.js. There are some situations where I'd like to run modify the current directory of the shell that runs my program:
some/location$ myProg moveToSomewhere
some/other/location$
child_process can't help here, since its commands run in a different shell.
Is there a way to do this?

Related

Inconsistencies between node's `spawn` and terminal

I'm trying to run a script from node, and I'm seeing different behaviour compared to if I run it from the terminal and I don't understand why.
I'm trying to do something a little bit out there, in that to save people typing pnpm {scriptName} --filter {packageName} I created a little script that they could run instead. It basically takes scriptName and lists all the available packages they can select from, and then trigger spawns a new process calling that command. Something like:
spawn("pnpm", ["--filter " + packageName, scriptName], { stdio: "inherit" });
Example:
In my particular case, I'm trying to test a script that ends up generating a pnpm --filter #ig/main test:debug.
I'm struggling a little though, in that if I invoke that via a terminal it works fine (test:debug is defined both in .\package.json and .\apps\main\package.json). However if I invoke it via the spawn command in node, then for some reason it invoking the test:debug script in the root, rather than just in apps/main. Does anyone know what that might be the case?
Turns out this was down to the current working directory not being set correctly when using spawn.

Trying to run a shell script with node.js using sudo -S

I'm trying to run a shell script with sudo access which executes a python script using Node.js on Mac. This was a big help How to run shell script file using nodejs? but I've gotten stuck:
Shell script:
#!/bin/sh
sudo -S python [pathToPythonScript]/someScripts.py
Node.js code:
const shell = require('shelljs');
shell.exec("./[pathToShellScript]");
When I run this in WebStorm, I am prompted to enter my password. I do so, but nothing happens; the script isn't executed. Can anybody help me with this?
The issue appears to be that you are not executed the node script with root permissions: sudo node filename.js.
Furthermore, it could be that the child process you are spawning with the shelljs module does not inherit the effective UID of the parent node process.
If sudo node filename.js does not solve your problem, I would recommend using the execa package instead of shelljs for better debugging and instrumentation of the spawned child-process: https://www.npmjs.com/package/execa
You can define the uid, gid of the child process as well as the stdin stream, which you could set to ignore such that no interactive prompts can occur (the sudo command would recognize that no stdin is provided and therefore not open an interactive prompt).

What exactly does it mean to run command via shell and without in Node.js?

I don't understand what it means to run command without a shell. I came from Windows OS where all commands run via shell e.g. cmd.exe, powershell. Maybe, I'm mistaken and commands can run without a shell.
Consider this code:
spawn('some-cmd', ['some-args'], {shell:true})
vs
spawn('some-cmd', ['some-args'])
So, what does it mean running command without a shell?
Is this concept relates to Node.js only or belongs to OS(Windows, Linux)? Pros and cons of running command with/without a shell?

node.js attach REPL to script

How do I start a node.js script and still be able to execute commands into the terminal ? I am looking for a node.js REPL that is also there for my custom script, so that I can inspect/log the state of my program for instance.
This is something similar to this JVM question, but for node.js.
I have tried node -i server.js without results. Do I need to have custom code in my script or is it feasible without that ? I saw this post, but it requires custom code, which I'd like to avoid.
Also, bonus points for reattaching a node script launched by an init script (I can see it in the process list : node -i server.js).
You can start a repl loop from within your program
http://nodejs.org/api/repl.html
Does the other way round work for you?
Start the REPL and then load the script and then execute your commands. Use load to load your script.
Inside REPL, try
.load server.js

Node-Webkit Child Process Exec

I want to execute a homebrew command for example
brew list
I followed the documentation and executed it like this:
child = exec('brew', function (error, stdout, stderr) {
console.log(stdout);
console.log(stderr);
});
I am getting a command not found error, and realized that if I do /usr/local/bin/brew as the command it works. However simply using 'brew' should work as well since I can run 'brew' from the command line just as such.
Why is this the case and what does it take to make 'brew' run as a child process in node? I have a feeling part of the issue because the command on node-webkit seems to execute from bin/sh.
Thanks
It may depend on how you're starting node-webkit and how you're setting your PATH. When I start from the command line, it inherits the environment variables from my command-line environment, including PATH. If I start by double clicking in a gui, it inherits from the system (presumably /etc/paths), and any additions I make in my .bashrc/.bash_profile have no effect.
Also, I'm no security expert, but my understanding of best practices would include using an absolute path to the executable you're running, so it's harder to spoof by setting an environment variable. By that measure, you're better off using the full path to brew anyway.

Resources