cmd-list.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. const abbrev = require('abbrev')
  2. // These correspond to filenames in lib/commands
  3. // Please keep this list sorted alphabetically
  4. const commands = [
  5. 'access',
  6. 'adduser',
  7. 'audit',
  8. 'bugs',
  9. 'cache',
  10. 'ci',
  11. 'completion',
  12. 'config',
  13. 'dedupe',
  14. 'deprecate',
  15. 'diff',
  16. 'dist-tag',
  17. 'docs',
  18. 'doctor',
  19. 'edit',
  20. 'exec',
  21. 'explain',
  22. 'explore',
  23. 'find-dupes',
  24. 'fund',
  25. 'get',
  26. 'help',
  27. 'help-search',
  28. 'init',
  29. 'install',
  30. 'install-ci-test',
  31. 'install-test',
  32. 'link',
  33. 'll',
  34. 'login',
  35. 'logout',
  36. 'ls',
  37. 'org',
  38. 'outdated',
  39. 'owner',
  40. 'pack',
  41. 'ping',
  42. 'pkg',
  43. 'prefix',
  44. 'profile',
  45. 'prune',
  46. 'publish',
  47. 'query',
  48. 'rebuild',
  49. 'repo',
  50. 'restart',
  51. 'root',
  52. 'run',
  53. 'sbom',
  54. 'search',
  55. 'set',
  56. 'shrinkwrap',
  57. 'star',
  58. 'stars',
  59. 'start',
  60. 'stop',
  61. 'team',
  62. 'test',
  63. 'token',
  64. 'trust',
  65. 'undeprecate',
  66. 'uninstall',
  67. 'unpublish',
  68. 'unstar',
  69. 'update',
  70. 'version',
  71. 'view',
  72. 'whoami',
  73. ]
  74. // These must resolve to an entry in commands
  75. const aliases = {
  76. // aliases
  77. author: 'owner',
  78. home: 'docs',
  79. issues: 'bugs',
  80. info: 'view',
  81. show: 'view',
  82. find: 'search',
  83. add: 'install',
  84. unlink: 'uninstall',
  85. remove: 'uninstall',
  86. rm: 'uninstall',
  87. r: 'uninstall',
  88. // short names for common things
  89. un: 'uninstall',
  90. rb: 'rebuild',
  91. list: 'ls',
  92. ln: 'link',
  93. create: 'init',
  94. i: 'install',
  95. it: 'install-test',
  96. cit: 'install-ci-test',
  97. up: 'update',
  98. c: 'config',
  99. s: 'search',
  100. se: 'search',
  101. tst: 'test',
  102. t: 'test',
  103. ddp: 'dedupe',
  104. v: 'view',
  105. 'run-script': 'run',
  106. 'clean-install': 'ci',
  107. 'clean-install-test': 'install-ci-test',
  108. x: 'exec',
  109. why: 'explain',
  110. la: 'll',
  111. verison: 'version',
  112. ic: 'ci',
  113. // typos
  114. innit: 'init',
  115. // manually abbrev so that install-test doesn't make insta stop working
  116. in: 'install',
  117. ins: 'install',
  118. inst: 'install',
  119. insta: 'install',
  120. instal: 'install',
  121. isnt: 'install',
  122. isnta: 'install',
  123. isntal: 'install',
  124. isntall: 'install',
  125. 'install-clean': 'ci',
  126. 'isntall-clean': 'ci',
  127. hlep: 'help',
  128. 'dist-tags': 'dist-tag',
  129. upgrade: 'update',
  130. udpate: 'update',
  131. rum: 'run',
  132. sit: 'install-ci-test',
  133. urn: 'run',
  134. ogr: 'org',
  135. 'add-user': 'adduser',
  136. }
  137. const deref = (c) => {
  138. if (!c) {
  139. return
  140. }
  141. // Translate camelCase to snake-case (i.e. installTest to install-test)
  142. if (c.match(/[A-Z]/)) {
  143. c = c.replace(/([A-Z])/g, m => '-' + m.toLowerCase())
  144. }
  145. // if they asked for something exactly we are done
  146. if (commands.includes(c)) {
  147. return c
  148. }
  149. // if they asked for a direct alias
  150. if (aliases[c]) {
  151. return aliases[c]
  152. }
  153. const abbrevs = abbrev(commands.concat(Object.keys(aliases)))
  154. // first deref the abbrev,
  155. // if there is one then resolve any aliases so `npm install-cl` will resolve to `install-clean` then to `ci`
  156. let a = abbrevs[c]
  157. while (aliases[a]) {
  158. a = aliases[a]
  159. }
  160. return a
  161. }
  162. module.exports = {
  163. aliases,
  164. commands,
  165. deref,
  166. }