如何从Atom电子应用程序调用Shell脚本或python脚本

sup*_*oon 12 javascript python shell node.js electron

我正在尝试使用Atom电子为Mac和Windows编写桌面应用程序.

我需要的是:

一个按钮.

当用户单击该按钮时,它将运行以下shell(或python脚本):

ping x.x.x.x
Run Code Online (Sandbox Code Playgroud)

结果将显示在TextArea中.

我尝试使用[shelljs]和[yargs],但似乎它对Atom电子不起作用.

我想要的只是使用JAVASCRIPT来编写桌面应用程序(当然还有GUI),它调用一些脚本(shell && python)来做一些自动化工作.

任何建议将不胜感激,谢谢:)

mar*_*pie 29

它可以直接用Node完成,你可以使用该child_process模块.请注意这是异步的.

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);
});
Run Code Online (Sandbox Code Playgroud)

我鼓励你也看看npm,有很多模块可以帮助你做你想做的事情,而不需要调用python脚本.


Vis*_*ori 5

试试node-powershell npm。您可以直接执行 shell 脚本命令并显示结果。

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()
})
Run Code Online (Sandbox Code Playgroud)

请参阅: https : //www.npmjs.com/package/node-powershell