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