-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
38 lines (31 loc) · 1.07 KB
/
index.js
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
const got = require('got');
const sortFn = (a, b) => {
if (a > b) return -1;
if (a < b) return 1;
return 0;
};
module.exports = async (pkg, version, opts = {}, http = got) => {
const gotOpts = { json: true };
if (opts.username && !opts.password) {
throw new Error('A `password` is required when an npm username is passed');
} else if (opts.username && opts.password) {
gotOpts.auth = `${opts.username}:${opts.password}`;
}
const { body: results } = await http(`https://registry.npmjs.org/${pkg}`, gotOpts);
const versions = Object.keys(results.versions)
.filter((v) => v.includes(version))
.sort(sortFn);
if (!versions.length || !versions.some((v) => v.includes('rc'))) {
return {
version: `${version}-rc.1`,
publishedVersionExists: versions.some((v) => v === version),
};
}
const rcRegexpr = /rc\.(\d*)/g;
const [_, rcRaw] = rcRegexpr.exec(versions[0]);
const nextRc = (parseInt(rcRaw, 10) || 0) + 1;
return {
version: `${version}-rc.${nextRc}`,
publishedVersionExists: versions.some((v) => v === version),
};
};