All files / src verifyCommand.js

100% Statements 58/58
100% Branches 53/53
100% Functions 18/18
100% Lines 49/49

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   6x 118x   118x 114x   114x 114x   114x   114x 3x 111x 11x         100x     4x       6x         114x 114x   114x 1x   113x 133x                   133x 68x 20x 43x 1x 1x         68x   68x 68x 68x   68x             20x   20x   20x             43x   43x 43x   43x             111x       182x       157x       88x       120x             45x             133x             65x             698x       469x  
const {
  CommandExpected,
  InvalidArgs,
  InvalidKey,
  InvalidNestedCommand,
  InvalidOpts,
  InvalidTypes,
  OptionExpected,
  PosArgExpected,
  SubcommandExpected,
  UnknownCommandLineOptionType
} = require('./errors')
 
const 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], opt}
  }
}
 
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 (opt = {}) {
  return (
    [opt.key, opt.opts].every(isDefined) &&
    [opt.args, opt.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'
}