Hubotで外部コマンドを実行する
更新日:2023.05.05
作成日:2016.09.03

Node.jsで外部コマンドを実行するために、Child Process
を利用する。
Child Process | Node.js v6.5.0 Documentation
const exec = require('child_process').exec;
exec('cat *.js bad_file | wc -l', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
Hubotの例
child_process = require 'child_process'
module.exports = (robot) ->
robot.hear /ruby/i, (res) ->
child_process.exec "ruby -v", (error, stdout, stderr) ->
if !error
output = stdout+''
res.send output
else
res.send 'error'
参考
Related contents

TECH
2016.08.28
AceエディタをElectronに組み込む
TECH
2016.08.27
javascript this bind

TECH
2016.08.24
React + ElectronでFlickr連携可能なMarkdown Editorを作った

TECH
2016.05.11
Electronがv1.0.0になったのでMithril.jsと合わせてAmazonアフィリエイトリンクビルダーを作った

TECH
2016.02.20
ElectronでAmazonアフィリエイトリンクビルダーを作った

TECH
2016.02.07
D3.jsで東京の地価データを可視化する

TECH
2016.02.07
JavaScript Ninjaの極意 「関数の呼び出し」

TECH
2016.02.06
D3.jsチュートリアル