Code coverage report for lib/common/options.js

Statements: 83.7% (77 / 92)      Branches: 63.89% (23 / 36)      Functions: 86.96% (20 / 23)      Lines: 83.52% (76 / 91)      Ignored: none     

All files » lib/common/ » options.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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173    1 1 1 1 1   1 1 1 1 1 1   1 1 1 70 70 70 70 70 70 70   1                           1   70                                                     1   2387         1 1550 1550   1 23 23         23 23 1357   23 1357 590   767 767 606     606 606 606     606     606           1 2317 2317   1 606 606   1 6026 138   1 606 69 69   537   1 2317 2317       2317   1 70 70   1 70 70 3570     193     48         1                   1   1  
///<reference path=".d.ts"/>
"use strict";
var util = require("util");
var helpers = require("./helpers");
var yargs = require("yargs");
var OptionType = (function () {
    function OptionType() {
    }
    OptionType.String = "string";
    OptionType.Boolean = "boolean";
    OptionType.Number = "number";
    OptionType.Array = "array";
    OptionType.Object = "object";
    return OptionType;
})();
exports.OptionType = OptionType;
var OptionsBase = (function () {
    function OptionsBase(options, defaultProfileDir, $errors, $staticConfig) {
        this.options = options;
        this.defaultProfileDir = defaultProfileDir;
        this.$errors = $errors;
        this.$staticConfig = $staticConfig;
        this.optionsWhiteList = ["ui", "recursive", "reporter", "require", "timeout", "_", "$0"];
        _.extend(this.options, this.commonOptions, OptionsBase.GLOBAL_OPTIONS);
        this.setArgv();
    }
    Object.defineProperty(OptionsBase.prototype, "shorthands", {
        get: function () {
            var _this = this;
            var result = [];
            _.each(_.keys(this.options), function (optionName) {
                if (_this.options[optionName].alias) {
                    result.push(_this.options[optionName].alias);
                }
            });
            return result;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(OptionsBase.prototype, "commonOptions", {
        get: function () {
            return {
                "json": { type: OptionType.Boolean },
                "watch": { type: OptionType.Boolean },
                "avd": { type: OptionType.String },
                "timeout": { type: OptionType.String },
                "device": { type: OptionType.String },
                "availableDevices": { type: OptionType.Boolean },
                "appid": { type: OptionType.String },
                "geny": { type: OptionType.String },
                "debugBrk": { type: OptionType.Boolean },
                "debugPort": { type: OptionType.Number },
                "getPort": { type: OptionType.Boolean },
                "start": { type: OptionType.Boolean },
                "stop": { type: OptionType.Boolean },
                "ddi": { type: OptionType.String },
                "justlaunch": { type: OptionType.Boolean },
                "file": { type: OptionType.String },
                "force": { type: OptionType.Boolean, alias: "f" },
                "companion": { type: OptionType.Boolean },
                "emulator": { type: OptionType.Boolean },
                "sdk": { type: OptionType.String },
                var: { type: OptionType.Object },
            };
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(OptionsBase.prototype, "optionNames", {
        get: function () {
            return _.keys(this.options);
        },
        enumerable: true,
        configurable: true
    });
    OptionsBase.prototype.getOptionValue = function (optionName) {
        optionName = this.getCorrectOptionName(optionName);
        return this.argv[optionName];
    };
    OptionsBase.prototype.validateOptions = function (commandSpecificDashedOptions) {
        var _this = this;
        Iif (commandSpecificDashedOptions) {
            this.options = OptionsBase.GLOBAL_OPTIONS;
            _.extend(this.options, commandSpecificDashedOptions);
            this.setArgv();
        }
        var parsed = Object.create(null);
        _.each(_.keys(this.argv), function (optionName) {
            parsed[optionName] = _this.getOptionValue(optionName);
        });
        _.each(parsed, function (value, originalOptionName) {
            if (value === undefined) {
                return;
            }
            var optionName = _this.getCorrectOptionName(originalOptionName);
            if (!_.contains(_this.optionsWhiteList, optionName)) {
                Iif (!_this.isOptionSupported(optionName)) {
                    _this.$errors.failWithoutHelp("The option '" + originalOptionName + "' is not supported. To see command's options, use '$ " + _this.$staticConfig.CLIENT_NAME.toLowerCase() + " help " + process.argv[2] + "'. To see all commands use '$ " + _this.$staticConfig.CLIENT_NAME.toLowerCase() + " help'.");
                }
                var optionType = _this.getOptionType(optionName);
                var optionValue = parsed[optionName];
                Iif (_.isArray(optionValue) && optionType !== OptionType.Array) {
                    _this.$errors.fail("You have set the %s option multiple times. Check the correct command syntax below and try again.", originalOptionName);
                }
                else Iif (optionType === OptionType.String && helpers.isNullOrWhitespace(optionValue)) {
                    _this.$errors.failWithoutHelp("The option '%s' requires non-empty value.", originalOptionName);
                }
                else Iif (optionType === OptionType.Array && optionValue.length === 0) {
                    _this.$errors.failWithoutHelp("The option '" + originalOptionName + "' requires one or more values, separated by a space.");
                }
            }
        });
    };
    OptionsBase.prototype.getCorrectOptionName = function (optionName) {
        var secondaryOptionName = this.getSecondaryOptionName(optionName);
        return _.contains(this.optionNames, secondaryOptionName) ? secondaryOptionName : optionName;
    };
    OptionsBase.prototype.getOptionType = function (optionName) {
        var option = this.options[optionName] || this.tryGetOptionByAliasName(optionName);
        return option ? option.type : "";
    };
    OptionsBase.prototype.tryGetOptionByAliasName = function (aliasName) {
        var option = _.find(this.options, function (opt) { return opt.alias === aliasName; });
        return option;
    };
    OptionsBase.prototype.isOptionSupported = function (option) {
        if (!this.options[option]) {
            var opt = this.tryGetOptionByAliasName(option);
            return !!opt;
        }
        return true;
    };
    OptionsBase.prototype.getSecondaryOptionName = function (optionName) {
        var matchUpperCaseLetters = optionName.match(/(.+?)([-])([a-zA-Z])(.*)/);
        Iif (matchUpperCaseLetters) {
            var secondaryOptionName = util.format("%s%s%s", matchUpperCaseLetters[1], matchUpperCaseLetters[3].toUpperCase(), matchUpperCaseLetters[4] || '');
            return this.getSecondaryOptionName(secondaryOptionName);
        }
        return optionName;
    };
    OptionsBase.prototype.setArgv = function () {
        this.argv = yargs(process.argv.slice(2)).options(this.options).argv;
        this.adjustDashedOptions();
    };
    OptionsBase.prototype.adjustDashedOptions = function () {
        this.argv["profileDir"] = this.argv["profileDir"] || this.defaultProfileDir;
        _.each(this.optionNames, function (optionName) {
            Object.defineProperty(OptionsBase.prototype, optionName, {
                configurable: true,
                get: function () {
                    return this.getOptionValue(optionName);
                },
                set: function (value) {
                    this.argv[optionName] = value;
                }
            });
        });
    };
    OptionsBase.GLOBAL_OPTIONS = {
        "log": { type: OptionType.String },
        "verbose": { type: OptionType.Boolean, alias: "v" },
        "version": { type: OptionType.Boolean },
        "help": { type: OptionType.Boolean, alias: "h" },
        "profileDir": { type: OptionType.String },
        "analyticsClient": { type: OptionType.String },
        "path": { type: OptionType.String, alias: "p" },
        "_": { type: OptionType.String }
    };
    return OptionsBase;
})();
exports.OptionsBase = OptionsBase;