Run shell command from JavaScript in node-webkit? - javascript

Is there any way to run shell command from JavaScript in node-webkit?
There is a lot of similar questions, but it didn't help me.
I'm trying to build simple desktop app for listing installed tools.
I've created node module 'tools-v' which is installed globally and works when I run it in command line.
This module run several commands: npm -v, node -v, git -v etc.
I'm on Windows 7.
//var sys = require('sys');
var exec = require('child_process').exec;
//var toolsv = (process.platform === "win32" ? "tools-v.cmd" : "tools-v");
$(document).ready(function() {
//myCmd = "C:\\Users\\win7\\AppData\\Roaming\\npm\\tools-v.cmd";
//myCmd = toolsv;
myCmd = 'tools-v';
//gui.Shell.openItem('firefox',function(error, stdout, stderr) { });
//opening Firefox works.
exec(myCmd, function (error, stdout, stderr) {
//detached: true;
console.log('stdout: ' + stdout);
$('#output').append('stdout: ' + stdout)
if (error !== null) {
console.log('exec error: ' + error);
}
});
});
I'm always getting error:
""exec error: Error: spawn ENOENT""
I tried spawn instead of exec. I also tried several other commands, beside node module.
Thanks.

Actually, this code works. I just didn't built full app, I tested it trough sublime build for node-webkit. Preforming full build with grunt solved every spawn issues.

Related

how to exec orderly by using the module (child_process) of node?

const exec = require('child_process').exec;
exec('npm -v && npm -v ', function (err, stdout, stderr) {
if (err) {
console.error('Error: ' + err);
return;
}
console.log('Stdout: \n' + stdout);
});
the output of the code above is like this:
Stdout:
v16.14.2
7.17.0
Obviously, it executed the command npm -v firstly and node -v secondly, its ok.
But now I want to do something different :
execute the command of step 1, but it needs a password that the user inputs, if the password is correct, it will move on the step 2, step 3, step 4, ....., otherwise it will never process.
Please tell me if someone knows the way achieving it.
Thank very much!
something about this part has written in last part(DETAILS OF PROBLEM)

How to run a command using javascript in protractor

I have an application which needs to run with the following command:
be-engine.exe -u producer -c D:\Workspace\V114_new\KinesisChannel\kinesis.cdd D:\Workspace\V114_new\KinesisChannel.ear -n logs002
Here i need to navigate to the bin director of the installed application and the run the be-engine.exe passing other file paths as well in the command on windows.
I want to automate this using protractor so that on start the above commands run automatically and engine gets started.
Is there a way to do this in protractor using javascript.
NodeJS has exec method for this:
const { exec } = require('child_process');
exec('be-engine.exe -u producer -c D:\Workspace\V114_new\KinesisChannel\kinesis.cdd D:\Workspace\V114_new\KinesisChannel.ear -n logs002', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
});
https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
Check also this answer: Execute a command line binary with Node.js

How to call Shell script or python script in from a Atom electron app

I'm trying to use the Atom electron to write a Desktop App for both Mac and Windows.
What I need here is :
A button.
And when the user click the button it runs the following shell (or python script):
ping x.x.x.x
And the result will be displayed in a TextArea.
I tried to use [shelljs] and [yargs] but it seems like it is not workable with Atom electron.
All I want is to use JAVASCRIPT to write Desktop App (with GUI of course) that calls some script (shell && python) to do some automation work.
Any suggestion will be appreciated, thanks :)
It can be done directly with Node, you can use the child_process module. Please notice this is asynchronous.
const exec = require('child_process').exec;
function execute(command, callback) {
exec(command, (error, stdout, stderr) => {
callback(stdout);
});
};
// call the function
execute('ping -c 4 0.0.0.0', (output) => {
console.log(output);
});
I encourage you to also have a look at npm, there are tons of modules that could help you to do what you want, without calling a python script.
Try node-powershell npm. You can directly execute shell script commands and display result.
var shell = require('node-powershell')
var ps = new shell()
ps.addCommand('ping -c 4 0.0.0.0')
ps.invoke()
.then(function (output) {
console.log(output)
})
.catch(function (err) {
console.log(err)
ps.dispose()
})
See: https://www.npmjs.com/package/node-powershell
you could use child_process to archive what you are trying to do by using the following code
var exec = require('child_process').exec
function Callback(err, stdout, stderr) {
if (err) {
console.log(`exec error: ${err}`);
return;
}else{
console.log(`${stdout}`);
}
}
res = exec('ping xxx.xxx.xxx', Callback);

Execute Command from Node Webkit

I want to run a command inside of a script tag within my index.html file using node webkit. Is such a thing possible and how would the code look like if I wanted to execute the command 'pwd' for example?
Thanks in advance
Does something like this not work?
var sys = require('sys')
var exec = require('child_process').exec;
var child;
// executes `pwd`
child = exec("pwd", function (error, stdout, stderr) {
sys.print('stdout: ' + stdout);
sys.print('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
The documentation for node webkit states:
Complete support for Node.js APIs and all its third party modules.
Which would indicate that you could use the node childprocess api:
http://nodejs.org/api/child_process.html

Node.js exec with wget

I'm trying to download a lot of files using nodejs and the exec command, simplified like this:
var cmd = 'wget -O output.csv URL';
var child = exec(cmd, function(err) {
console.log('DONE');
});
However, the callback is triggered before the file was actually downloaded through wget, leading to a file that contains garbage like '��0O�6D�1n�]v�����#�'. Shouldn't the callback be triggered once wget is done? When running the same command on the command line it takes rougly 5 seconds, since the file has several MB.
Btw: I'm not using the request module since it's slower and I ran into emitter listener issues (EventEmitter memory leak detected. 11 listeners added).
Thanks!
This will involve some debugging.
Can you please try running your script as:
var cmd = 'wget -O output.csv URL';
var child = exec(
cmd,
function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
}
);
It would be interesting to see what stdout and stderr say.
Right, you provided me your stderr which said:
http://productdata.zanox.com/exportservice/v1/rest/22791753C32335607.csv?ticket=BC4B91472561713FD43BA766542E9240AFDD01B95B123E40B2C0375E3A68C142
This URL the command line gets is missing everything after the ampersand (& character). This indicates a problem with escaping.
To get around this try replacing \& with \\\&.

Categories

Resources