-
-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathmaintainerships.js
103 lines (87 loc) · 2.28 KB
/
maintainerships.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
'use strict';
const fetch = require('../fetch');
const figgy = require('figgy-pudding');
module.exports = maintainerships;
// usage: ds maintainerships namespace
// list all packages maintained by the given namespace, if the logged-in user has permission
const filterOps = figgy({
argv: true,
registry: true,
token: true,
log: { default: require('npmlog') }
});
async function maintainerships(opts) {
opts = filterOps(opts);
let invitee = opts.argv[0];
if (!invitee.includes('@')) {
invitee += '@' + opts.registry.replace(/^https?:\/\//, '');
}
const uri = `${
opts.registry
}/v1/namespaces/namespace/${invitee}/maintainerships`;
const response = await fetch(uri, {
headers: {
authorization: `Bearer ${opts.token}`
}
});
const body = await response.json();
if (body.error) {
console.error(body.error);
return 1;
}
if (!Array.isArray(body.objects)) {
console.log(body);
return 0;
}
if (body.objects.length === 0) {
console.log(`${invitee} maintains no packages.`);
return 0;
}
console.log(
`${invitee} maintains ` +
(body.objects.length == 1
? 'one package'
: `${body.objects.length} packages`) +
':'
);
body.objects.forEach(p => {
console.log(` ${p.name}`);
});
return listNamespaceMemberships(opts);
}
async function listNamespaceMemberships(opts) {
// Note that this is ready to pop out into its own command if we want.
let invitee = opts.argv[0];
if (!invitee.includes('@')) {
invitee += '@' + opts.registry.replace(/^https?:\/\//, '');
}
const uri = `${opts.registry}/v1/users/user/${invitee}/memberships`;
const response = await fetch(uri, {
headers: {
authorization: `Bearer ${opts.token}`
}
});
const body = await response.json();
if (body.error) {
console.error(body.error);
return 1;
}
if (!Array.isArray(body.objects)) {
console.log(body);
return 0;
}
if (body.objects.length === 0) {
console.log(`${invitee} is not a member of any namespaces.`);
return 0;
}
console.log(
`${invitee} is a member of ` +
(body.objects.length == 1
? 'one namespace'
: `${body.objects.length} namespaces`) +
':'
);
body.objects.forEach(p => {
console.log(` ${p.name}`);
});
}