forked from entropic-dev/entropic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
invite.js
55 lines (47 loc) · 1.21 KB
/
invite.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
'use strict';
const fetch = require('../fetch');
const figgy = require('figgy-pudding');
const parsePackageSpec = require('../canonicalize-spec');
module.exports = invite;
const inviteOpts = figgy({
argv: true,
registry: true,
token: true,
namespace: true,
package: true,
log: { default: require('npmlog') }
});
async function invite(opts) {
opts = inviteOpts(opts);
// I do note that it would be nice to invite a list of people at once.
const invitee = opts.argv[0];
let uri;
if (opts.package) {
const parsed = parsePackageSpec(
opts.package,
opts.registry.replace(/^https?:\/\//, '')
);
uri = `${opts.registry}/v1/packages/package/${
parsed.canonical
}/maintainers/${invitee}`;
} else {
let ns = opts.namespace;
if (!ns.includes('@')) {
ns += '@' + opts.registry.replace(/^https?:\/\//, '');
}
uri = `${opts.registry}/v1/namespaces/namespace/${ns}/members/${invitee}`;
}
const response = await fetch(uri, {
method: 'POST',
headers: {
authorization: `Bearer ${opts.token}`
}
});
const body = await response.json();
if (body.message) {
console.log(body.message);
return 0;
}
console.log(body);
return 1;
}