deref-command.js 762 B

123456789101112131415161718192021222324252627
  1. // de-reference abbreviations and shorthands into canonical command name
  2. const { aliases, cmdList, plumbing } = require('../utils/cmd-list.js')
  3. const aliasNames = Object.keys(aliases)
  4. const fullList = cmdList.concat(aliasNames).filter(c => !plumbing.includes(c))
  5. const abbrev = require('abbrev')
  6. const abbrevs = abbrev(fullList)
  7. module.exports = c => {
  8. if (!c || typeof c !== 'string')
  9. return ''
  10. if (c.match(/[A-Z]/))
  11. c = c.replace(/([A-Z])/g, m => '-' + m.toLowerCase())
  12. if (plumbing.indexOf(c) !== -1)
  13. return c
  14. // first deref the abbrev, if there is one
  15. // then resolve any aliases
  16. // so `npm install-cl` will resolve to `install-clean` then to `ci`
  17. let a = abbrevs[c]
  18. while (aliases[a])
  19. a = aliases[a]
  20. return a || ''
  21. }