UNPKG

alsatian

Version:

TypeScript and JavaScript testing framework for beautiful and readable tests

117 lines 4.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const duplicate_cli_argument_error_1 = require("./errors/duplicate-cli-argument-error"); const invalid_argument_names_error_1 = require("./errors/invalid-argument-names-error"); const invalid_timeout_value_error_1 = require("./errors/invalid-timeout-value-error"); const missing_argument_value_error_1 = require("./errors/missing-argument-value-error"); const unused_1 = require("../core/unused"); class AlsatianCliOptions { constructor(args) { this.timeout = null; this.tap = false; this.versionRequested = false; this.helpRequested = false; this.hideProgress = false; const a = this.extractTap(args); this.tap = a.value; const b = this.extractVersionRequested(a.args); this.versionRequested = b.value; const c = this.extractHelpRequested(b.args); this.helpRequested = c.value; const d = this.extractFileGlobs(c.args); this.fileGlobs = d.value; const e = this.extractTimeout(d.args); this.timeout = e.value; const f = this.extractHideProgress(e.args); this.hideProgress = f.value; if (f.args.length > 0) { throw new invalid_argument_names_error_1.InvalidArgumentNamesError(f.args); } } extractFileGlobs(args) { const fileGlobs = args.filter((value, index) => { const previousArgument = args[index - 1]; if ((!previousArgument || previousArgument[0]) !== "-" && value[0] !== "-") { return true; } return false; }); args = args.filter(value => fileGlobs.indexOf(value) === -1); return { value: fileGlobs, args }; } isInvalidTimeoutValue(timeoutValue) { const timeout = parseInt(timeoutValue, 10); return (isNaN(timeout) || timeout < 1 || timeout.toString() !== timeoutValue); } extractTimeout(args) { const timeoutValue = this.getArgumentValueFromArgumentList(args, "timeout", "t"); if (timeoutValue !== null) { if (this.isInvalidTimeoutValue(timeoutValue)) { throw new invalid_timeout_value_error_1.InvalidTimeoutValueError(timeoutValue); } const argumentIndex = this.getArgumentIndexFromArgumentList(args, "timeout", "t"); args = args.filter((value, index) => { unused_1.Unused(value); return index !== argumentIndex && index !== argumentIndex + 1; }); } return { value: parseInt(timeoutValue, 10) || null, args }; } extractTap(args) { return this.extractArgumentFromList(args, "tap", "T"); } extractHideProgress(args) { return this.extractArgumentFromList(args, "hide-progress", "H"); } extractVersionRequested(args) { return this.extractArgumentFromList(args, "version", "v"); } extractHelpRequested(args) { return this.extractArgumentFromList(args, "help", "h"); } extractArgumentFromList(args, argumentName, argumentShorthand) { const argumentIndex = this.getArgumentIndexFromArgumentList(args, argumentName, argumentShorthand); args = args.filter((value, index) => { unused_1.Unused(value); return index !== argumentIndex; }); return { value: argumentIndex !== -1, args }; } getArgumentIndexFromArgumentList(args, argumentName, argumentShorthand) { const matchingArguments = args.filter(value => 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]); } getArgumentValueFromArgumentList(args, argumentName, argumentShorthand) { const argumentIndex = this.getArgumentIndexFromArgumentList(args, argumentName, argumentShorthand); if (argumentIndex === -1) { return null; } const valueArgument = args[argumentIndex + 1]; if (valueArgument && (valueArgument[0] !== "-" || !isNaN(parseInt(valueArgument, 10)))) { return valueArgument; } else { throw new missing_argument_value_error_1.MissingArgumentValueError(argumentName); } } } exports.AlsatianCliOptions = AlsatianCliOptions; //# sourceMappingURL=alsatian-cli-options.js.map