forked from entropic-dev/entropic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoin.js
55 lines (46 loc) · 1.31 KB
/
join.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 = join;
// usage: ds join name@host/pkg --as namespace
const joinOpts = figgy({
argv: true,
as: true,
registry: true,
token: true,
log: { default: require('npmlog') }
});
async function join(opts) {
opts = joinOpts(opts);
if (opts.argv.length !== 1 || !opts.as) {
console.error('Usage: ds join <namespace|package> --as <namespace|user>');
return 1;
}
const host = opts.registry.replace(/^https?:\/\//, '');
const invitee = opts.as;
let uri;
if (opts.argv[0].includes('/')) {
const { _, ...parsed } = parsePackageSpec(
opts.argv[0],
opts.registry.replace(/^https?:\/\//, '')
);
uri = `${opts.registry}/v1/packages/package/${
parsed.canonical
}/maintainers/${invitee}/invitation`;
} else {
const ns = opts.argv[0] + (opts.argv[0].includes('@') ? '' : `@${host}`);
uri = `${
opts.registry
}/v1/namespaces/namespace/${ns}/members/${invitee}/invitation`;
}
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;
}