forked from entropic-dev/entropic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accept.js
60 lines (49 loc) · 1.4 KB
/
accept.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
'use strict';
const fetch = require('../fetch');
const figgy = require('figgy-pudding');
const parsePackageSpec = require('../canonicalize-spec');
module.exports = accept;
// usage: ds accept --package name@host/pkg --as namespace
// ds accept --namespace name@host
const acceptOpts = figgy({
as: true,
namespace: true,
package: true,
registry: true,
token: true,
log: { default: require('npmlog') }
});
async function accept(opts) {
opts = acceptOpts(opts);
if (!opts.as || (!opts.package && !opts.namespace)) {
console.error(
'Usage: ds accept --package <package> --as <namespace>\n' +
' ds accept --namespace <namespace>'
);
return 1;
}
let uri;
if (opts.package) {
const parsed = parsePackageSpec(
opts.package,
opts.registry.replace(/^https?:\/\//, '')
);
const invitee = opts.as;
uri = `${opts.registry}/v1/namespaces/namespace/${invitee}/maintainerships/${parsed.canonical}`;
} else {
let ns = opts.namespace;
if (!ns.includes('@')) {
ns += '@' + opts.registry.replace(/^https?:\/\//, '');
}
uri = `${opts.registry}/v1/users/user/memberships/invitations/${ns}`;
}
const response = await fetch(uri, {
method: 'POST',
headers: {
authorization: `Bearer ${opts.token}`
}
});
const body = await response.json();
console.log(body.message ? body.message : body);
return 0;
}