-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.ts
266 lines (240 loc) · 6.85 KB
/
cli.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env node
import type { FilesArray, VendorsOptions } from './lib/types.js';
import { Command } from '@commander-js/extra-typings';
import { install, sync, uninstall } from './lib/commands.js';
import { getConfig, setRunOptions } from './lib/config.js';
import { findRepoUrl, login } from './lib/github.js';
import {
assert,
getPackageJson,
isGitHubUrl,
ownerAndNameFromRepoUrl,
} from './lib/utils.js';
let vendorOptions: VendorsOptions;
const program = new Command();
const syncCmd = new Command('sync')
.alias('s')
.option('-f, --force', 'Force sync')
.action(({ force }) => syncAll(!!force))
.summary('Sync config file')
.description('Sync all dependencies in the config file')
.addHelpText(
'after',
`
Examples:
vendor sync
vendor sync -f
`,
);
const updateCmd = new Command('update')
.alias('upgrade')
.alias('bump')
.alias('up')
.alias('u')
.argument('[names...]')
.option('-pr|--pr', 'Output pull request text for gh action', false)
.action((names, { pr }) => {
if (names.length === 0) {
upgradeAll(pr);
} else {
for (const name of names) {
upgradeOne(name);
}
}
})
.summary('Update outdated dependencies')
.description(
'Update all/selected dependencies to their latest version (the tag of the latest release)',
)
.addHelpText(
'after',
`
Examples:
vendor update
vendor bump React
vendor update React Express
`,
);
const outdatedCmd = new Command('outdated')
.alias('o')
.action(() => showOutdated())
.summary('List outdated dependencies')
.description('List outdated dependencies')
.addHelpText(
'after',
`
Examples:
vendor outdated
vendor o
`,
);
const installCmd = new Command('install')
.alias('add')
.alias('i')
.alias('a')
.argument(
'<url/name>',
'GitHub repo URL or owner/repo format or name of repo to search for',
)
.argument('[version]', 'Version to install')
.option('-n, --name [name]', 'Name to write in dependencies')
.option('-f, --files <files...>', 'Files to install')
.action(async (source, version, { name, files }) => {
if (source) {
let url: string;
if (isGitHubUrl(source)) {
url = source;
} else if (source.match(/^[^/]+\/[^/]+$/)) {
// url is in format owner/repo
url = `https://www.github.com/${source}`;
} else {
url = await findRepoUrl(source);
}
assert(isGitHubUrl(url), `Invalid GitHub URL "${url}"`);
if (typeof name !== 'string' || name.length === 0) {
name = ownerAndNameFromRepoUrl(url).name;
}
const deps =
vendorOptions.dependencies[name] ||
Object.values(vendorOptions.dependencies).find(
(dep) => dep.repository === url,
) ||
{};
assert(
!!files || !!deps?.files,
'you must provide files to install with -f or --files <files...>',
);
installOne({ url, files: files || deps.files, version, name });
} else {
syncAll(true);
}
})
.summary('Install a dependency')
.description(
'Install a dependency. origin can be a GitHub repo URL or owner/repo format or name of repo to search for.\nFiles have to be provided with -f or --files <files...>',
)
.addHelpText(
'after',
`
Examples:
vendor install React -n MyReact -f README.md
vendor add Araxeus/vendorfiles v1.0.0 -f README.md LICENSE
vendor i https://github.com/th-ch/youtube-music -f "{release}/YouTube-Music-{version}.exe"
`,
);
const uninstallCmd = new Command('uninstall')
.alias('remove')
.alias('delete')
.alias('del')
.alias('rm')
.alias('un')
.alias('r')
.argument('[names...]', 'Package names to uninstall')
.action((names) => {
assert(names.length > 0, 'No package names provided');
for (const name of names) {
uninstallOne(name);
}
})
.summary('Uninstall dependencies')
.description('Uninstall all/selected dependencies')
.addHelpText(
'after',
`
Examples:
vendor uninstall React
vendor remove React youtube-music
`,
);
const loginCmd = new Command('login')
.alias('auth')
.argument('[token]', 'GitHub token (leave empty to login via browser)')
.action((token) => login(token))
.summary('Login to GitHub')
.description('Login to GitHub to increase rate limit')
.addHelpText(
'after',
`
Examples:
vendor login
vendor auth <token>
`,
);
program
.name('vendor')
.hook('preAction', async () => {
setRunOptions({
configFolder: program.getOptionValue('folder') as
| string
| undefined,
});
vendorOptions = await getConfig();
})
.usage('command [options]')
.addCommand(syncCmd)
.addCommand(updateCmd)
.addCommand(outdatedCmd)
.addCommand(installCmd)
.addCommand(uninstallCmd)
.addCommand(loginCmd)
.option('-dir, --folder [folder]', 'Folder containing the config file')
.version(
(await getPackageJson()).version || 'unknown',
'-v, --version',
'output the current version',
)
.parse();
function upgradeAll(prMode: boolean) {
if (prMode) {
setRunOptions({ prMode });
}
sync(vendorOptions, {
shouldUpdate: true,
});
}
function syncAll(force: boolean) {
sync(vendorOptions, {
shouldUpdate: false,
force,
});
}
function showOutdated() {
sync(vendorOptions, {
shouldUpdate: true,
showOutdatedOnly: true,
});
}
function installOne({
url,
name,
version,
files,
}: { url: string; files: FilesArray; version?: string; name?: string }) {
install({
dependency: (name && vendorOptions.dependencies[name]) || {
repository: url,
files,
version,
name,
},
config: vendorOptions.config,
configFile: vendorOptions.configFile,
configFileSettings: vendorOptions.configFileSettings,
shouldUpdate: !version,
newVersion: version,
});
}
function uninstallOne(name: string) {
uninstall(name, vendorOptions);
}
function upgradeOne(name: string) {
const dep = vendorOptions.configFile.vendorDependencies?.[name];
assert(!!dep, `No dependency found with name ${name}`);
assert(!!dep.repository, `No repository found for dependency ${name}`);
assert(!!dep.files, `No files found for dependency ${name}`);
installOne({
url: dep.repository,
files: dep.files,
name,
});
}