-
-
Notifications
You must be signed in to change notification settings - Fork 506
/
github.js
72 lines (66 loc) · 2.63 KB
/
github.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
import fs from 'fs-extra';
import mime from 'mime-types';
import path from 'path';
import asyncOra from '../util/ora-handler';
import GitHub from '../util/github';
export default async ({ artifacts, packageJSON, forgeConfig, authToken, tag }) => {
if (!(forgeConfig.github_repository && typeof forgeConfig.github_repository === 'object' &&
forgeConfig.github_repository.owner && forgeConfig.github_repository.name)) {
throw 'In order to publish to github you must set the "github_repository.owner" and "github_repository.name" properties in your forge config. See the docs for more info'; // eslint-disable-line
}
const github = new GitHub(authToken, true);
let release;
await asyncOra('Searching for target release', async () => {
try {
release = (await github.getGitHub().repos.getReleases({
owner: forgeConfig.github_repository.owner,
repo: forgeConfig.github_repository.name,
per_page: 100,
})).data.find(testRelease => testRelease.tag_name === (tag || `v${packageJSON.version}`));
if (!release) {
throw { code: 404 };
}
} catch (err) {
if (err.code === 404) {
// Release does not exist, let's make it
release = (await github.getGitHub().repos.createRelease({
owner: forgeConfig.github_repository.owner,
repo: forgeConfig.github_repository.name,
tag_name: tag || `v${packageJSON.version}`,
name: tag || `v${packageJSON.version}`,
draft: forgeConfig.github_repository.draft !== false,
prerelease: forgeConfig.github_repository.prerelease === true,
})).data;
} else {
// Unknown error
throw err;
}
}
});
let uploaded = 0;
await asyncOra(`Uploading Artifacts ${uploaded}/${artifacts.length}`, async (uploadSpinner) => {
const updateSpinner = () => {
uploadSpinner.text = `Uploading Artifacts ${uploaded}/${artifacts.length}`; // eslint-disable-line
};
await Promise.all(artifacts.map(artifactPath =>
new Promise(async (resolve) => {
const done = () => {
uploaded += 1;
updateSpinner();
resolve();
};
if (release.assets.find(asset => asset.name === path.basename(artifactPath))) {
return done();
}
await github.getGitHub().repos.uploadAsset({
url: release.upload_url,
file: fs.createReadStream(artifactPath),
contentType: mime.lookup(artifactPath) || 'application/octet-stream',
contentLength: (await fs.stat(artifactPath)).size,
name: path.basename(artifactPath),
});
return done();
})
));
});
};