All files / lib/utils deref-command.js

100% Statements 18/18
100% Branches 10/10
100% Functions 3/3
100% Lines 16/16

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    1x 1x 119x 1x 1x   1x 6x 2x   4x 1x   4x 1x         3x 3x 1x   3x    
// de-reference abbreviations and shorthands into canonical command name
 
const { aliases, cmdList, plumbing } = require('../utils/cmd-list.js')
const aliasNames = Object.keys(aliases)
const fullList = cmdList.concat(aliasNames).filter(c => !plumbing.includes(c))
const abbrev = require('abbrev')
const abbrevs = abbrev(fullList)
 
module.exports = c => {
  if (!c || typeof c !== 'string')
    return ''
 
  if (c.match(/[A-Z]/))
    c = c.replace(/([A-Z])/g, m => '-' + m.toLowerCase())
 
  if (plumbing.indexOf(c) !== -1)
    return c
 
  // first deref the abbrev, if there is one
  // then resolve any aliases
  // so `npm install-cl` will resolve to `install-clean` then to `ci`
  let a = abbrevs[c]
  while (aliases[a])
    a = aliases[a]
 
  return a || ''
}