all files / cli/ alsatian-cli-options.js

100% Statements 53/53
100% Branches 30/30
100% Functions 12/12
100% Lines 51/51
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  33× 33× 33× 23×       10×                   33× 33× 67× 67× 18×   49×   67×   33× 28× 13× 13×   16×     15×   65× 41× 15×   26×   23×   33× 30× 15×   15× 15× 13×            
"use strict";
var invalid_argument_names_error_1 = require("./errors/invalid-argument-names-error");
var invalid_timeout_value_error_1 = require("./errors/invalid-timeout-value-error");
var duplicate_cli_argument_error_1 = require("./errors/duplicate-cli-argument-error");
var missing_argument_value_error_1 = require("./errors/missing-argument-value-error");
var AlsatianCliOptions = (function () {
    function AlsatianCliOptions(args) {
        this._timeout = null;
        args = this._extractFileGlobs(args);
        args = this._extractTimeout(args);
        if (args.length > 0) {
            throw new invalid_argument_names_error_1.InvalidArgumentNamesError(args);
        }
    }
    Object.defineProperty(AlsatianCliOptions.prototype, "fileGlobs", {
        get: function () {
            return this._fileGlobs;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(AlsatianCliOptions.prototype, "timeout", {
        get: function () {
            return this._timeout;
        },
        enumerable: true,
        configurable: true
    });
    AlsatianCliOptions.prototype._extractFileGlobs = function (args) {
        var _this = this;
        this._fileGlobs = args.filter(function (value, index) {
            var previousArgument = args[index - 1];
            if ((!previousArgument || previousArgument[0]) !== "-" && value[0] !== "-") {
                return true;
            }
            return false;
        });
        return args.filter(function (value) { return _this._fileGlobs.indexOf(value) === -1; });
    };
    AlsatianCliOptions.prototype._extractTimeout = function (args) {
        var timeoutValue = this._getArgumentValueFromArgumentList(args, "timeout", "t");
        if (timeoutValue !== null) {
            var timeout = parseInt(timeoutValue);
            if (isNaN(timeout) || timeout < 1 || timeout.toString() !== timeoutValue) {
                throw new invalid_timeout_value_error_1.InvalidTimeoutValueError(timeoutValue);
            }
            this._timeout = timeout;
            var argumentIndex_1 = this._getArgumentIndexFromArgumentList(args, "timeout", "t");
            return args.filter(function (value, index) {
                return index !== argumentIndex_1 && index !== argumentIndex_1 + 1;
            });
        }
        return args;
    };
    AlsatianCliOptions.prototype._getArgumentIndexFromArgumentList = function (args, argumentName, argumentShorthand) {
        var matchingArguments = args.filter(function (value, index) { return value === "--" + argumentName || value === "-" + argumentShorthand; });
        if (matchingArguments.length === 0) {
            return -1;
        }
        else if (matchingArguments.length > 1) {
            throw new duplicate_cli_argument_error_1.DuplicateCliArgumentError(argumentName);
        }
        return args.indexOf(matchingArguments[0]);
    };
    AlsatianCliOptions.prototype._getArgumentValueFromArgumentList = function (args, argumentName, argumentShorthand) {
        var argumentIndex = this._getArgumentIndexFromArgumentList(args, argumentName, argumentShorthand);
        if (argumentIndex === -1) {
            return null;
        }
        var valueArgument = args[argumentIndex + 1];
        if (valueArgument && (valueArgument[0] !== "-" || !isNaN(parseInt(valueArgument)))) {
            return valueArgument;
        }
        else {
            throw new missing_argument_value_error_1.MissingArgumentValueError(argumentName);
        }
    };
    return AlsatianCliOptions;
}());
exports.AlsatianCliOptions = AlsatianCliOptions;
//# sourceMappingURL=alsatian-cli-options.js.map