NodeJS

2016-09-06 45 views
10

adresinden cygwin komutunu çalıştırın Node'un child_process modülünü kullanarak, cygwin kabuğundan komutları yürütmek istiyorum. Bu benim çalıştığım şey: Ben ne olursa olsun shell seçenek olmaktan setin, bash.exe bu argümanlar ne yapacağını bilmez, Node's child_process.js will add the /s and /c switches görebilirsinizNodeJS

var exec = require('child_process').execSync; 
exec('mkdir -p a/b/c', {shell : 'c:/cygwin64/bin/bash.exe -c'}); 
 
TypeError: invalid data 
    at WriteStream.Socket.write (net.js:641:11) 
    at execSync (child_process.js:503:20) 
    at repl:1:1 
    at REPLServer.defaultEval (repl.js:262:27) 
    at bound (domain.js:287:14) 
    at REPLServer.runBound [as eval] (domain.js:300:12) 
    at REPLServer. (repl.js:431:12) 
    at emitOne (events.js:82:20) 
    at REPLServer.emit (events.js:169:7) 
    at REPLServer.Interface._onLine (readline.js:212:10) 

.

bu sorun için geçici bir iş bulmuş ama gerçekten ideal değil:

exec('c:/cygwin64/bin/bash.exe -c "mkdir -p a/b/c"'); 

besbelli yalnızca Windows değil unix sistemlerde çalışacak yukarıda yapmak.

NodeJS'den cygwin kabuğundaki komutları nasıl uygularım?

cevap

2

daha exec() seçeneklerden bazıları ile yapılabilir gerekir, çünkü bu, tam bir jenerik çözüm değildir, ancak bu daha sonra birbirinden ayırt, sen Unixler Windows ve cygwin üzerinde çalışacak kod yazmak izin vermelidir iki.

Bu çözüm, Cygwin'in cygwin dizesini içeren bir dizine kurulduğunu varsayar.

var child_process = require('child_process') 
    , home = process.env.HOME 
; 

function exec(command, options, next) { 
    if(/cygwin/.test(home)) { 
    command = home.replace(/(cygwin[0-9]*).*/, "$1") + "\\bin\\bash.exe -c '" + command.replace(/\\/g, '/').replace(/'/g, "\'") + "'"; 
    } 

    child_process.exec(command, options, next); 
} 

Cygwin çalıştırırken alternatif şartlı child_process.exec kaçırmak olabilir:

var child_process = require('child_process') 
    , home = process.env.HOME 
; 

if(/cygwin/.test(home)) { 
    var child_process_exec = child_process.exec 
    , bash = home.replace(/(cygwin[0-9]*).*/, "$1") + "\\bin\\bash.exe" 
    ; 

    child_process.exec = function(command, options, next) { 
    command = bash + " -c '" + command.replace(/\\/g, '/').replace(/'/g, "\'") + "'"; 

    child_process_exec(command, options, next) 
    } 
}