-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.cjs
121 lines (107 loc) · 3.07 KB
/
index.cjs
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
#!/usr/bin/env node
// @ts-check
const {exec} = require('child_process');
const semver = require('semver');
const yargs = require('yargs/yargs');
const {hideBin} = require('yargs/helpers');
const fetch = require('node-fetch');
const {promisify} = require('util');
const argv = yargs(hideBin(process.argv))
.command(
'$0 <pkg>',
'Queries the provided registry and returns all the dependencies for each version.',
(yargs) => {
yargs.positional('pkg', {
describe: 'package identifier',
type: 'string',
});
},
)
.options({
range: {
alias: 'r',
type: 'string',
default: '',
description: 'Semver range to retrieve.',
},
major: {
alias: 'm',
type: 'boolean',
default: false,
description: 'Inspect only the latest version for each major.',
},
stable: {
alias: 's',
type: 'boolean',
default: false,
description:
'Inspect only stable versions (no alpha, beta, prereleases).',
},
registry: {
type: 'string',
default: '',
description:
'Registry to query. Leave empty to use current registry set in NPM config.',
},
})
.strict()
.demandCommand(1)
.help()
.parseSync();
(async () => {
let {registry, major, stable, range, pkg} = argv;
if (!registry) {
registry = (
await promisify(exec)('npm config get registry', {
encoding: 'utf-8',
})
).stdout.trim();
}
console.log(`Using registry ${registry}...`);
const {versions, time} = await fetch(`${registry}/${pkg}`).then((v) =>
v.json(),
);
let allVersions = [];
for (const v in versions) {
allVersions.push({
version: v,
time: new Date(time[v]),
});
}
// sort desc
allVersions.sort((v1, v2) => v2['time'].valueOf() - v1['time'].valueOf());
if (range) {
allVersions = allVersions.filter(({version}) =>
semver.satisfies(version, range),
);
}
if (stable) {
allVersions = allVersions.filter(
({version}) => !semver.prerelease(version),
);
}
if (major) {
const majors = new Set();
const latestVersions = [];
for (const versionObj of allVersions) {
const major = semver.major(versionObj.version);
if (!majors.has(major)) {
majors.add(major);
latestVersions.push(versionObj);
}
}
allVersions = latestVersions;
}
const out = {};
const keys = ['dependencies', 'devDependencies', 'peerDependencies'];
for (const {version} of allVersions) {
out[version] = {};
for (const k of keys) {
const src = versions[version][k];
if (src) {
out[version][k] = src;
}
}
}
console.log(out);
})();