-
Notifications
You must be signed in to change notification settings - Fork 104
/
publish-release.js
78 lines (68 loc) · 1.98 KB
/
publish-release.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
/* eslint no-console: "off" */
/* eslint import/no-extraneous-dependencies: "off" */
const fs = require('fs');
const path = require('path');
const request = require('request-promise');
const USER_AGENT = 'publish-release-script-1';
const GH_API_ENDPOINT = 'https://api.github.com';
const GH_UPLOAD_URL = id => `https://uploads.github.com/repos/lmammino/norrisbot/releases/${id}/assets`;
const token = process.argv[2];
const repo = process.argv[3];
const release = process.argv[4];
const buildFolder = process.argv[5] || './build';
console.log(`Creating release "${release}" from "${buildFolder}"`);
const uploadFile = releaseId => file => new Promise((resolve, reject) => {
console.log('attaching', file.path);
return fs.readFile(file.path, (error, buffer) => {
if (error) {
return reject(error);
}
return resolve(request.post({
body: buffer,
url: `${GH_UPLOAD_URL(releaseId)}?name=${file.name}`,
headers: {
'User-Agent': USER_AGENT,
'Content-Type': 'application/zip',
Authorization: `token ${token}`,
},
}));
});
});
let releaseId;
request({
uri: `${GH_API_ENDPOINT}/repos/${repo}/releases`,
method: 'POST',
body: JSON.stringify({
tag_name: release,
name: `v${release}`,
}),
headers: {
'User-Agent': USER_AGENT,
'Content-Type': 'application/json',
Authorization: `token ${token}`,
},
})
.then((response) => {
const data = JSON.parse(response);
releaseId = data.id;
console.log(`Created release: ${data.url}`);
return new Promise((resolve, reject) => {
fs.readdir(buildFolder, (err, items) => {
if (err) {
return reject(err);
}
return resolve(items.map(item => ({
name: item,
path: path.resolve(path.join(buildFolder, item)),
})));
});
});
})
.then(files => Promise.all(files.map(uploadFile(releaseId))))
.then(() => {
console.log('✅ Done');
})
.catch((err) => {
console.error('Failed to create release', err);
process.exit(1);
});