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

Statements: 58.02% (47 / 81)      Branches: 28.57% (12 / 42)      Functions: 50% (9 / 18)      Lines: 57.5% (46 / 80)      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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136    1 1 1 1 25 25   1 3 3 3 3 3 1     2 2     3 2     1   3   1                         1 5 5   1       1 5 5 5 5 5         5         5 5 5         5           5 5                         5                             5   1                 1                 1 31   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, this.getArgumentsAsQuotedString(args));
        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, this.getArgumentsAsQuotedString(args));
        return child_process.spawn(command, args, options);
    };
    ChildProcess.prototype.fork = function (modulePath, args, options) {
        this.$logger.debug("fork: %s %s", modulePath, this.getArgumentsAsQuotedString(args));
        return child_process.fork(modulePath, 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 = "";
        Iif (childProcess.stdout) {
            childProcess.stdout.on("data", function (data) {
                capturedOut += data;
            });
        }
        Iif (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
            };
            Iif (spawnFromEventOptions && spawnFromEventOptions.throwError === false) {
                if (!future.isResolved()) {
                    future.return(result);
                }
            }
            else {
                Eif (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()) {
                if (spawnFromEventOptions && spawnFromEventOptions.throwError === false) {
                    var result = {
                        stdout: capturedOut,
                        stderr: err.message,
                        exitCode: err.code
                    };
                    future.return(result);
                }
                else {
                    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);
        }
    };
    ChildProcess.prototype.getArgumentsAsQuotedString = function (args) {
        return args.map(function (argument) { return ("\"" + argument + "\""); }).join(" ");
    };
    return ChildProcess;
})();
exports.ChildProcess = ChildProcess;
$injector.register("childProcess", ChildProcess);