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

100% Statements 53/53
100% Branches 30/30
100% Functions 12/12
100% Lines 49/49
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       10×     33×       33× 33×   23×       33× 33× 67×   67× 18×     49×     67×     33×   28×   13×   13×         16×       15×       65×   41× 15×   26×     23×     33×   30× 15×     15×   15× 13×          
import { InvalidArgumentNamesError } from "./errors/invalid-argument-names-error";
import { InvalidTimeoutValueError } from "./errors/invalid-timeout-value-error";
import { DuplicateCliArgumentError } from "./errors/duplicate-cli-argument-error";
import { MissingArgumentValueError } from "./errors/missing-argument-value-error";
 
export class AlsatianCliOptions {
 
   private _fileGlobs: Array<string>;
   public get fileGlobs(): Array<string> {
      return this._fileGlobs;
   }
 
   private _timeout: number = null;
   public get timeout(): number {
      return this._timeout;
   }
 
   public constructor(args: Array<string>) {
 
      args = this._extractFileGlobs(args);
      args = this._extractTimeout(args);
 
      if (args.length > 0) {
         throw new InvalidArgumentNamesError(args);
      }
   }
 
   private _extractFileGlobs(args: Array<string>) {
      this._fileGlobs = args.filter((value, index) => {
         const previousArgument = args[index - 1];
 
         if ((!previousArgument || previousArgument[0]) !== "-" && value[0] !== "-") {
            return true;
         }
 
         return false;
      });
 
      return args.filter(value => this._fileGlobs.indexOf(value) === -1);
   }
 
   private _extractTimeout(args: Array<string>) {
      let timeoutValue = this._getArgumentValueFromArgumentList(args, "timeout", "t");
 
      if (timeoutValue !== null) {
 
         const timeout = parseInt(timeoutValue);
 
         if (isNaN(timeout) || timeout < 1 || timeout.toString() !== timeoutValue) {
            throw new InvalidTimeoutValueError(timeoutValue);
         }
 
         this._timeout = timeout;
 
         const argumentIndex = this._getArgumentIndexFromArgumentList(args, "timeout", "t");
 
         return args.filter((value, index) => {
            return index !== argumentIndex && index !== argumentIndex + 1;
         });
      }
 
      return args;
   }
 
   private _getArgumentIndexFromArgumentList(args: Array<string>, argumentName: string, argumentShorthand?: string): number {
 
      const matchingArguments = args.filter((value, index) => value === "--" + argumentName || value === "-" + argumentShorthand);
 
      if (matchingArguments.length === 0) {
         return -1;
      }
      else if (matchingArguments.length > 1) {
         throw new DuplicateCliArgumentError(argumentName);
      }
 
      return args.indexOf(matchingArguments[0]);
   }
 
   private _getArgumentValueFromArgumentList(args: Array<string>, argumentName: string, argumentShorthand?: string): string {
      const argumentIndex = this._getArgumentIndexFromArgumentList(args, argumentName, argumentShorthand);
 
      if (argumentIndex === -1) {
         return null;
      }
 
      const valueArgument = args[argumentIndex + 1];
 
      if (valueArgument && (valueArgument[0] !== "-" || !isNaN(parseInt(valueArgument)))) {
         return valueArgument;
      }
      else {
         throw new MissingArgumentValueError(argumentName);
      }
   }
}