Code coverage report for lib/common/child-process.js

Statements: 38.03% (27 / 71)      Branches: 16.67% (6 / 36)      Functions: 26.67% (4 / 15)      Lines: 38.03% (27 / 71)      Ignored: none     

All files » lib/common/ » child-process.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117    1 1 1 1 19 19   1 11 11 11 11 11 1     10 10     11 10     1   11   1                         1       1                                                                                             1                 1                 1   1 1  
///<reference path=".d.ts"/>
"use strict";
var Future = require("fibers/future");
var child_process = require("child_process");
var ChildProcess = (function () {
    function ChildProcess($logger, $errors) {
        this.$logger = $logger;
        this.$errors = $errors;
    }
    ChildProcess.prototype.exec = function (command, options, execOptions) {
        var _this = this;
        var future = new Future();
        var callback = function (error, stdout, stderr) {
            _this.$logger.trace("Exec %s \n stdout: %s \n stderr: %s", command, stdout.toString(), stderr.toString());
            if (error) {
                future.throw(error);
            }
            else {
                var output = execOptions && execOptions.showStderr ? { stdout: stdout, stderr: stderr } : stdout;
                future.return(output);
            }
        };
        if (options) {
            child_process.exec(command, options, callback);
        }
        else {
            child_process.exec(command, callback);
        }
        return future;
    };
    ChildProcess.prototype.execFile = function (command, args) {
        this.$logger.debug("execFile: %s %s", command, args.join(" "));
        var future = new Future();
        child_process.execFile(command, args, function (error, stdout) {
            if (error) {
                future.throw(error);
            }
            else {
                future.return(stdout);
            }
        });
        return future;
    };
    ChildProcess.prototype.spawn = function (command, args, options) {
        this.$logger.debug("spawn: %s %s", command, args.join(" "));
        return child_process.spawn(command, args, options);
    };
    ChildProcess.prototype.spawnFromEvent = function (command, args, event, options, spawnFromEventOptions) {
        var future = new Future();
        var childProcess = this.spawn(command, args, options);
        var capturedOut = "";
        var capturedErr = "";
        if (childProcess.stdout) {
            childProcess.stdout.on("data", function (data) {
                capturedOut += data;
            });
        }
        if (childProcess.stderr) {
            childProcess.stderr.on("data", function (data) {
                capturedErr += data;
            });
        }
        childProcess.on(event, function (arg) {
            var exitCode = typeof arg === "number" ? arg : arg && arg.code;
            var result = {
                stdout: capturedOut,
                stderr: capturedErr,
                exitCode: exitCode
            };
            if (spawnFromEventOptions && spawnFromEventOptions.throwError === false) {
                future.return(result);
            }
            else {
                if (exitCode === 0) {
                    future.return(result);
                }
                else {
                    var errorMessage = "Command " + command + " failed with exit code " + exitCode;
                    if (capturedErr) {
                        errorMessage += " Error output: \n " + capturedErr;
                    }
                    if (!future.isResolved()) {
                        future.throw(new Error(errorMessage));
                    }
                }
            }
        });
        childProcess.once("error", function (err) {
            if (!future.isResolved()) {
                future.throw(err);
            }
        });
        return future;
    };
    ChildProcess.prototype.tryExecuteApplication = function (command, args, event, errorMessage, condition) {
        var _this = this;
        return (function () {
            var childProcess = _this.tryExecuteApplicationCore(command, args, event, errorMessage).wait();
            if (condition && condition(childProcess)) {
                _this.$errors.fail(errorMessage);
            }
        }).future()();
    };
    ChildProcess.prototype.tryExecuteApplicationCore = function (command, args, event, errorMessage) {
        try {
            return this.spawnFromEvent(command, args, event, undefined, { throwError: false });
        }
        catch (e) {
            var message = (e.code === "ENOENT") ? errorMessage : e.message;
            this.$errors.failWithoutHelp(message);
        }
    };
    return ChildProcess;
})();
exports.ChildProcess = ChildProcess;
$injector.register("childProcess", ChildProcess);