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