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 up: 'update',
102 c: 'config',
103 s: 'search',
104 se: 'search',
105 tst: 'test',
106 t: 'test',
107 ddp: 'dedupe',
108 v: 'view',
109 'run-script': 'run',
110 'clean-install': 'ci',
111 'clean-install-test': 'install-ci-test',
112 x: 'exec',
113 why: 'explain',
114 la: 'll',
115 verison: 'version',
116 ic: 'ci',
117
118 // typos
119 innit: 'init',
120 // manually abbrev so that install-test doesn't make insta stop working
121 in: 'install',
122 ins: 'install',
123 inst: 'install',
124 insta: 'install',
125 instal: 'install',
126 isnt: 'install',
127 isnta: 'install',
128 isntal: 'install',
129 isntall: 'install',
130 'install-clean': 'ci',
131 'isntall-clean': 'ci',
132 hlep: 'help',
133 'dist-tags': 'dist-tag',
134 upgrade: 'update',
135 udpate: 'update',
136 rum: 'run',
137 sit: 'install-ci-test',
138 urn: 'run',
139 ogr: 'org',
140 'add-user': 'adduser',
141}
142
143const deref = (c) => {
144 if (!c) {
145 return
146 }
147
148 // Translate camelCase to snake-case (i.e. installTest to install-test)
149 if (c.match(/[A-Z]/)) {
150 c = c.replace(/([A-Z])/g, m => '-' + m.toLowerCase())
151 }
152
153 // if they asked for something exactly we are done
154 if (commands.includes(c)) {
155 return c
156 }
157
158 // if they asked for a direct alias
159 if (aliases[c]) {
160 return aliases[c]
161 }
162
163 const abbrevs = abbrev(commands.concat(Object.keys(aliases)))
164
165 // first deref the abbrev,
166 // if there is one then resolve any aliases 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 