All files / src/toOpts verifyCommand.js

100% Statements 57/57
100% Branches 52/52
100% Functions 18/18
100% Lines 48/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                      6x     133x   133x 130x   130x 130x   130x   130x 3x 127x 11x         116x     3x       6x         130x 130x   130x 1x   129x 150x                   150x 66x 31x 51x 1x 1x         66x   66x 66x 66x   66x             31x   31x   31x             51x   51x 51x   51x             117x       196x       181x       97x       135x             53x             150x             84x             806x       545x  
const {
  CommandExpected,
  InvalidArgs,
  InvalidKey,
  InvalidNestedCommand,
  InvalidOpts,
  InvalidTypes,
  OptionExpected,
  PosArgExpected,
  SubcommandExpected,
  UnknownCommandLineOptionType
} = require('../errors')
 
function verifyCommand (opt) {
  const err = CommandExpected({opt})
 
  if (isCommand(opt)) {
    const errs = []
 
    if (invalidKey(opt))  errs.push(InvalidKey({opt}))
    if (invalidOpts(opt)) errs.push(InvalidOpts({opt}))
 
    const {errs: errs2, opts} = verifyOpts({errs, opts: [opt]})
 
    if (errs.length > 0) {
      return {errs: [err, ...errs]}
    } else if (errs2.length > 0) {
      return {
        errs: errs2,
        opt: {...opt, opts}
      }
    } else {
      return {errs: [], opt}
    }
  } else {
    return {errs: [err]}
  }
}
 
module.exports = {
  verifyCommand
}
 
function verifyOpts ({errs, opts}) {
  const [opt] = opts
  const {opts: opts2} = opt
 
  if (!Array.isArray(opts2)) {
    return {errs: [...errs, InvalidOpts({opt})], opts: []}
  } else {
    return opts2.map(verifyOpt).reduce(
      ({errs, opts}, {errs: errs2, opts: opts2}) => ({
        errs: [...errs, ...errs2],
        opts: [...opts, ...opts2]
      }),
      {errs, opts: []}
    )
  }
}
 
function verifyOpt (opt) {
  switch (true) {
    case isOption(opt):     return verifyOption(opt)
    case isPosArg(opt):     return verifyPosArg(opt)
    case isSubcommand(opt): return verifySubcommand(opt)
    case isCommand(opt):    return {errs: [InvalidNestedCommand({opt})],         opts: []}
    default:                return {errs: [UnknownCommandLineOptionType({opt})], opts: []}
  }
}
 
function verifyOption (opt) {
  const errs = []
 
  if (invalidKey(opt))   errs.push(InvalidKey({opt}))
  if (invalidArgs(opt))  errs.push(InvalidArgs({opt}))
  if (invalidTypes(opt)) errs.push(InvalidTypes({opt}))
 
  return (
    errs.length > 0 ? {errs: [OptionExpected({opt}), ...errs], opts: []   }
                    : {errs: [],                               opts: [opt]}
  )
}
 
function verifyPosArg (opt) {
  const errs = []
 
  if (invalidTypes(opt)) errs.push(InvalidTypes({opt}))
 
  return (
    errs.length > 0 ? {errs: [PosArgExpected({opt}), ...errs], opts: []   }
                    : {errs: [],                               opts: [opt]}
  )
}
 
function verifySubcommand (opt) {
  const errs = []
 
  if (invalidArgs(opt)) errs.push(InvalidArgs({opt}))
  if (invalidOpts(opt)) errs.push(InvalidOpts({opt}))
 
  return (
    errs.length > 0 ? {errs: [SubcommandExpected({opt}), ...errs], opts: []   }
                    : {errs: [],                                   opts: [opt]}
  )
}
 
function invalidArgs ({args}) {
  return !Array.isArray(args) || args.length === 0 || args.some(arg => arg.includes(' '))
}
 
function invalidKey ({key}) {
  return typeof key !== 'string' || key === '_' || key === '--' || key.includes(' ')
}
 
function invalidOpts ({opts}) {
  return !Array.isArray(opts)
}
 
function invalidTypes ({types}) {
  return !Array.isArray(types) && typeof types !== 'undefined'
}
 
function isCommand ({key, opts, args, types}) {
  return (
    [key, opts].every(isDefined) &&
    [args, types].every(isUndefined)
  )
}
 
function isSubcommand ({key, opts, args, types}) {
  return (
    [key, args, opts].every(isDefined) &&
    [types].every(isUndefined)
  )
}
 
function isOption ({key, args, opts}) {
  return (
    [key, args].every(isDefined) &&
    [opts].every(isUndefined)
  )
}
 
function isPosArg ({key, args, opts}) {
  return (
    [key].every(isDefined) &&
    [args, opts].every(isUndefined)
  )
}
 
function isDefined (val) {
  return typeof val !== 'undefined'
}
 
function isUndefined (val) {
  return typeof val === 'undefined'
}