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 'search', 56 'set', 57 'shrinkwrap', 58 'star', 59 'stars', 60 'start', 61 'stop', 62 'team', 63 'test', 64 'token', 65 'uninstall', 66 'unpublish', 67 'unstar', 68 'update', 69 'version', 70 'view', 71 'whoami', 72] 73 74// These must resolve to an entry in commands 75const aliases = { 76 77 // aliases 78 author: 'owner', 79 home: 'docs', 80 issues: 'bugs', 81 info: 'view', 82 show: 'view', 83 find: 'search', 84 add: 'install', 85 unlink: 'uninstall', 86 remove: 'uninstall', 87 rm: 'uninstall', 88 r: 'uninstall', 89 90 // short names for common things 91 un: 'uninstall', 92 rb: 'rebuild', 93 list: 'ls', 94 ln: 'link', 95 create: 'init', 96 i: 'install', 97 it: 'install-test', 98 cit: 'install-ci-test', 99 up: 'update', 100 c: 'config', 101 s: 'search', 102 se: 'search', 103 tst: 'test', 104 t: 'test', 105 ddp: 'dedupe', 106 v: 'view', 107 run: 'run-script', 108 'clean-install': 'ci', 109 'clean-install-test': 'install-ci-test', 110 x: 'exec', 111 why: 'explain', 112 la: 'll', 113 verison: 'version', 114 ic: 'ci', 115 116 // typos 117 innit: 'init', 118 // manually abbrev so that install-test doesn't make insta stop working 119 in: 'install', 120 ins: 'install', 121 inst: 'install', 122 insta: 'install', 123 instal: 'install', 124 isnt: 'install', 125 isnta: 'install', 126 isntal: 'install', 127 isntall: 'install', 128 'install-clean': 'ci', 129 'isntall-clean': 'ci', 130 hlep: 'help', 131 'dist-tags': 'dist-tag', 132 upgrade: 'update', 133 udpate: 'update', 134 rum: 'run-script', 135 sit: 'install-ci-test', 136 urn: 'run-script', 137 ogr: 'org', 138 'add-user': 'adduser', 139} 140 141const deref = (c) => { 142 if (!c) { 143 return 144 } 145 146 // Translate camelCase to snake-case (i.e. installTest to install-test) 147 if (c.match(/[A-Z]/)) { 148 c = c.replace(/([A-Z])/g, m => '-' + m.toLowerCase()) 149 } 150 151 // if they asked for something exactly we are done 152 if (commands.includes(c)) { 153 return c 154 } 155 156 // if they asked for a direct alias 157 if (aliases[c]) { 158 return aliases[c] 159 } 160 161 const abbrevs = abbrev(commands.concat(Object.keys(aliases))) 162 163 // first deref the abbrev, if there is one 164 // then resolve any aliases 165 // so `npm install-cl` will resolve to `install-clean` then to `ci` 166 let a = abbrevs[c] 167 while (aliases[a]) { 168 a = aliases[a] 169 } 170 return a 171} 172 173module.exports = { 174 aliases, 175 commands, 176 deref, 177} 178