diff --git a/bin/index.js b/bin/index.js index 305d9ac..c552134 100644 --- a/bin/index.js +++ b/bin/index.js @@ -1,14 +1,30 @@ // Generated by CoffeeScript 1.3.3 (function() { - var FFI, fs, getOutput, libc, tmpDir, uniqId, uniqIdK; + var FFI, fs, getOutput, libc, tmpDir, uniqId, uniqIdK, platforms; FFI = require("ffi"); - libc = new FFI.Library(null, { - "system": ["int32", ["string"]] - }); fs = require("fs"); + + exec = new { + win32: function Win32Platform() { + var lib = new FFI.Library("./lib/proc", { + run: ['int', ['string', 'string']] + }); + this.exec = function (cmd, tmpFilePath) { + return lib.run(cmd, tmpFilePath); + }; + }, + linux: function LinuxPlatform() { + var lib = new FFI.Library(null, { + "system": ["int32", ["string"]] + }); + this.exec = function (cmd, tmpFilePath) { + return lib.system(cmd + ">" + tmpFilePath); + } + } + }[process.platform]().exec uniqIdK = 0; @@ -46,29 +62,17 @@ }; module.exports = function(cmd, returnOutAndErr) { - var dir, error, id, result, stderr, stdout; + var error, id, result, stdout, tmpFilePath; if (returnOutAndErr == null) { returnOutAndErr = false; } id = uniqId(); stdout = id + '.stdout'; - stderr = id + '.stderr'; - dir = tmpDir(); - cmd = "" + cmd + " > " + dir + "/" + stdout + " 2> " + dir + "/" + stderr; - libc.system(cmd); - result = getOutput("" + dir + "/" + stdout); - error = getOutput("" + dir + "/" + stderr); - if (returnOutAndErr) { - return { - stdout: result, - stderr: error - }; - } else { - if (error !== '') { - throw new Error(error); - } - return result; + tmpFilePath = tmpDir() + "/" + stdout; + if(exec(cmd, tmpFilePath)) { + throw new Error("Process ends with not null error code"); } + return fs.readFileSync(tmpFilePath).toString().replace(/(\n|\r)+$/g,''); }; }).call(this); diff --git a/lib/proc.dll b/lib/proc.dll new file mode 100644 index 0000000..982fa9d Binary files /dev/null and b/lib/proc.dll differ