Calling processing-java from nodejs on Windows 10 - node.js

I am having some problems to execute the processing-java on windows from nodejs. When I use the following
processing-java --sketch=$pwd/sketch/ --run
in the console it works fine and the sketch is executed, window opens and the result is a file written to a directory where I need it.
But when I use the same as a string in the node file to execute it, it does not open a window and no file has been written, but the callback function processOutput is executed, hence the somehow the execution happened.
I added
const exec = require('child_process').exec;
to the js file which is used for executing child processes in node.
$pwd is the PowerShell equivalent to pwd on MacOS.
exec should execute the shell command and call the callback function. The callback function is executed, but not the sketch.
const cmd = 'processing-java --sketch=$pwd/sketch/ --run';
exec(cmd, processOutput);
What am I missing? Any help appreceated.
Many thanks, C.

child_process.exec() does not execute in a powershell but in a CMD terminal. Thus you can't use powershell variables like $pwd. You could try using %CD% instead
const cmd = 'processing-java --sketch=%CD%/sketch/ --run';
exec(cmd, processOutput);

Related

About script execution permissions on Linux shell

I've just created a script, let's say, "helloworld.sh".
The script doesn't yet have execution permissons: -rw-rw-r--
If I try to execute that script with: "./helloword.sh", I'll get an error message, as expected. But, if I try to execute that same script as: . helloword, it will execute with no problems.
How? Why does that happen?
This happens because on Linux the "." (dot) alone is a built-in command that execute the script within your current session with your current shell. This is the same as calling the script with source command (the BSD default method). It's almost the same than execute with bash helloworld.sh.
When you call the script with ./helloworld.sh or /root/helloworld.sh the shell will try to figure out how to execute it, if the file is a binary, it will simply run, if it is a script, the shell will read the first line looking for the interpreter. To do this, you'll need execution permission.
To simplify:
One is a command;
The other one is a path.
You can even run:
. --help
About . against bash:
This is why we use . or source to load variables from a file in our session, for example, when we change ~/.bashrc and reload it without login again.
You can see this happens when you execute:
. /etc/os-release
All variables defined inside this file will be loaded and available in your current shell session.
The same will not happen if you execute:
bash /etc/os-release
Because you opened a "new session" inside that bash that you called, the new bash executes and close, cleaning the session.
The same process happen if you give execute permission +x to the script, because when you call the script with ./ or something like that, a new session will be created too.

Expect command does not see stdout when executing with nodejs?

I wrote a bash file with expect command, expecting some output and do actions based on that it works when I execute it in the terminal but when I execute it using exec in nodejs it does not work. I think the output of commands I run in bash file cannot be seen by expect
I usually use, shelljs Package, for executing commands or shell scripts via nodejs.
const shell = require('shelljs')
shell.exec('./path_to_your_file')

How can I change actual shell cd with nodejs

I am creating a command line program to act as a shortcut for my environment. Commands like $ cd $enterprise/products/<product> are so common, that I like to compile into: $ enterprise product.
My problem is: I cannot change shell directory directly, like running $ cd $enterprise/products/<product> with process.chdir.
Using console.log(process.cwd()) shows that the directory has changed, but not on shell, only on nodejs internal process(I think it's running on a own shell).
Typing $ pwd in shell shows that I still are on the same folder.
I was searching for a solution, like a shell script that interprets the output of a nodejs file, then source the output.
Thanks.
This is actually bit trickier than it sounds.
You can't just change the working directory of the shell that is running the process, without making assumptions about which shell it is or the OS (I personally use applescript to spawn new terminal tabs).
What we can do, however, is spawn a new shell!
let child_process = require('child_process');
child_process.spawn(
// With this variable we know we use the same shell as the one that started this process
process.env.SHELL,
{
// Change the cwd
cwd: `${process.cwd()}/products/${product_id}`,
// This makes this process "take over" the terminal
stdio: 'inherit',
// If you want, you can also add more environment variables here, but you can also remove this line
env: { ...process.env, extra_environment: 'some value' },
},
);
When you run this, it seems like you cd into a directory, but actually you are still running inside nodejs!
You can't do this; every child process has its own working directory inherited from the parent. In this case, your cd gets its working directory from its parent (your shell). A child process can't change the directory – or any other state – of the parent process.

Using node.js and coffeescript for executing Ubuntu commands

I am using child_process.exec to execute Ubuntu commands with node.js in coffeescript. When I execute the following commands:
list = child_process.exec("ls")
print list
It prints this:
[Object Object]
Why isn't a proper output of ls command printed? What should I do to get a proper output for commands?
You're attempting to run an asynchronous function synchronously. The correct way to do this is:
var exec = require('child_process').exec;
exec('ls', function (error, stdout, stderr) {
console.log(stdout);
});
Source: https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
If you really wish to execute a command synchronously, you can use execSync. However, I'd advise against that, since it blocks your node code from doing anything until the process finishes.
ExecSync: https://nodejs.org/api/child_process.html#child_process_child_process_execsync_command_options
Found it!
Can be accessed using ->
print list.main.<attribute_name>

Launch node script from bash script

I've my program in bash and I want to launch a node program to get the string that it return, like in this way:
#!/bin/bash
mystring=$( node getString.js)
mplayer $mystring
Googling I found that I should inlcude
#!/usr/bin/env node
But I need to use string to give it to mplayer.. any ideas?
Solution
As Zac suggesting (and thanks to this link) I solved my problem in this way:
script.sh
#!/bin/bash
mplayer ${1}
script.js
/* do whatever you need */
var output="string"
var sys = require('sys');
var exec = require('child_process').exec;
function puts(error, stdout, stderr) { sys.puts(stdout); }
exec("./script.sh " + output, puts);
Consider simply writing an executable Node script (with the #!/bin/env node line), and, instead of using Bash, just use Node to run the external UNIX command. You can use the child_process module for this, as illustrated in this example. This question is also helpful when debugging shell-style subcommands in Node scripts.
If your example really is all you need to do in Bash, this should be sufficient. The #!/bin/env node line allows your script, once marked as executable, to run as its own program, without having to be invoked with node.

Resources