All files / src parserSync.js

100% Statements 42/42
100% Branches 33/33
100% Functions 23/23
100% Lines 38/38

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 1195x 5x 5x 5x 5x 5x   5x 58x   90x     5x 61x   94x     5x           10x                 78x   78x                         106x 106x   106x 106x   106x 147x   28x       28x   28x   28x 28x           119x             106x 106x 147x                       78x 78x 105x     78x 78x 105x                     147x            
const {fromArgs: FROM_ARGS} = require('./fromArgs')
const {pipe}                = require('./pipe')
const {then}                = require('./then')
const {toArgs:   TO_ARGS}   = require('./toArgs')
const {toArgv:   TO_ARGV}   = require('./toArgv')
const {toOpts:   TO_OPTS}   = require('./toOpts')
 
const Sync = {
  resolve: a => a,
  then: pipe,
  all: a => a
}
 
const Async = {
  resolve: a => Promise.resolve(a),
  then,
  all: a => Promise.all(a)
}
 
module.exports = {
  parser:     parser(Async),
  parserSync: parser(Sync)
}
 
function parser (Mode) {
  return (stages = {}, substages = {}) => {
    const {
      toArgv   = TO_ARGV,
      argv     = [],
      toOpts   = TO_OPTS,
      opts     = [],
      toArgs   = TO_ARGS,
      args     = [],
      fromArgs = FROM_ARGS
    } = stages
  
    return opt => (any, errs = []) => Mode.then(
      toArgv,
      ...argv,
      toOpts(opt),
      recurseOpts(Mode)(opts, substages),
      toArgs,
      transformArgs(Mode)(args),
      fromArgs
    )({errs, any})
  }
}
 
function recurseOpts (Mode) {
  return (optsStages, substages) => ({errs = [], opts = []} = {errs: [], opts: []}) => {
    const promise1 = Mode.then(...optsStages)({errs, opts})
    
    return Mode.then(result => {
      const {errs: errs3 = [], opts: opts3 = []} = result || {errs, opts: []}
 
      const promises = opts3.map(opt => {
        if (isSubcommand(opt)) {
          const stages = (
            Array.isArray(substages[opt.key]) ? substages[opt.key] :
            Array.isArray(substages._)        ? substages._
                                              : optsStages
          )
          const substages2 = substages[opt.key] || {}
  
          const promise2 = recurseOpts(Mode)(stages, substages2)({errs: [], opts: opt.values})
          
          return Mode.then(({errs: errs4, opts: opts4}) => {
            return {
              errs: errs4,
              opts: [{...opt, values: opts4}]
            }
          })(promise2)
        } else {
          return Mode.resolve({
            errs: [],
            opts: [opt]
          })
        }
      })
 
      return Mode.then(results => {
        return results.reduce(
          ({errs, opts}, {errs: errs2, opts: opts2}) => ({
            errs: [...errs, ...errs2],
            opts: [...opts, ...opts2],
          }),
          {errs: errs3, opts: []}
        )
      })(Mode.all(promises))
    })(promise1)
  }
}
 
function transformArgs (Mode) {
  return (argsStages) => ({errs = [], args = []} = {errs: [], args: []}) => {
    const promises = args.map(arg => {
      return Mode.then(...argsStages)({errs: [], args: arg})
    })
 
    return Mode.then(results => {
      return results.reduce(
        ({errs, args}, {errs: errs2, args: args2}) => ({
          errs: [...errs, ...errs2],
          args: [...args, args2]
        }),
        {errs, args: []}
      )
    })(Mode.all(promises))
  }
}
 
function isSubcommand ({key, args, types, opts}) {
  return (
    typeof key === 'string' &&
    Array.isArray(args) && args.length > 0 &&
    typeof types === 'undefined' &&
    Array.isArray(opts)
  )
}