-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtinsalver.mjs
executable file
·88 lines (78 loc) · 2.82 KB
/
tinsalver.mjs
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
#!/usr/bin/env node
import { parseStringPromise } from 'xml2js';
import fetch from 'node-fetch';
import { exec } from 'child_process';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
async function* crawl(url) {
let resp = await fetch(url);
const xml = await parseStringPromise(await resp.text());
for (let item of xml?.content?.data[0]['content-item']) {
if (item['leaf'][0] == 'true') {
yield item;
} else {
yield* crawl(item['resourceURI'][0]);
}
}
}
await yargs(hideBin(process.argv))
.option('user', {
alias: 'u',
type: 'string',
required: true,
description: 'Sonatype user or token id',
})
.option('password', {
alias: 'p',
type: 'string',
required: true,
description: 'Sonatype password or token id',
})
.command({
command: 'sign <stagedRepoId>',
handler: async ({ stagedRepoId, user, password }) => {
const stagingURL = `https://oss.sonatype.org/service/local/repositories/${stagedRepoId}/content/`;
for await (let {
resourceURI: [url],
text: [text],
} of crawl(stagingURL)) {
if (text.startsWith('maven-metadata.xml')) {
continue;
}
const ext = text.split('.').slice(-1)[0];
if (['asc', 'sha1', 'sha256', 'sha512'].includes(ext)) {
continue;
}
console.log(url);
const fileBody = await (await fetch(url)).buffer();
const signature = await new Promise((resolve, reject) => {
try {
const proc = exec(`keybase pgp sign --detached -i -`, (err, stdout, stderr) => {
console.log(stderr);
if (err) {
reject(err);
} else {
resolve(stdout);
}
});
proc.stdin.write(fileBody);
proc.stdin.end();
} catch (err) {
reject(err);
}
});
console.log(signature);
const resp = await fetch(`${url}.asc`, {
method: 'PUT',
body: signature,
headers: {
Authorization: 'Basic ' + Buffer.from(`${user}:${password}`).toString('base64'),
},
});
if (resp.status != 201) {
throw `${resp.statusText} ${resp.status}`;
}
console.log(await resp.text());
}
},
}).argv;