-
-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathmembers.js
84 lines (68 loc) · 1.88 KB
/
members.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
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
'use strict';
const fetch = require('../fetch');
const figgy = require('figgy-pudding');
const parsePackageSpec = require('../canonicalize-spec');
module.exports = members;
// usage: ds members name@host[/pkg]
const membersOpts = figgy({
argv: true,
registry: true,
log: { default: require('npmlog') }
});
async function members(opts) {
opts = membersOpts(opts);
if (opts.argv.length !== 1) {
console.error('Usage: ds members <namespace|package>');
return 1;
}
if (opts.argv[0].includes('/')) {
return listPackageMaintainers(opts);
}
// list namespace members
let ns = opts.argv[0];
if (!ns.includes('@')) {
ns += '@' + opts.registry.replace(/^https?:\/\//, '');
}
const uri = `${opts.registry}/v1/namespaces/namespace/${ns}/members`;
const response = await fetch(uri);
const body = await response.json();
if (!Array.isArray(body.objects) || body.objects.length === 0) {
console.log(`${ns} has no members.`);
return 0;
}
console.log(
`${ns} has ` +
(body.objects.length == 1
? 'one member'
: `${body.objects.length} members`) +
':'
);
body.objects.forEach(n => {
console.log(` ${n}`);
});
}
async function listPackageMaintainers(opts) {
const { _, ...parsed } = parsePackageSpec(
opts.argv[0],
opts.registry.replace(/^https?:\/\//, '')
);
const uri = `${opts.registry}/v1/packages/package/${
parsed.canonical
}/maintainers`;
const response = await fetch(uri);
const body = await response.json();
if (!Array.isArray(body.objects) || body.objects.length === 0) {
console.log(`${parsed.canonical} has no maintainers.`);
return 0;
}
console.log(
`${parsed.canonical} has ` +
(body.objects.length == 1
? 'one maintainer'
: `${body.objects.length} maintainer`) +
':'
);
body.objects.forEach(n => {
console.log(` ${n}`);
});
}