This repository was archived by the owner on Jun 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathupload-github-release.js
172 lines (160 loc) · 4.79 KB
/
upload-github-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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const fs = require('fs');
const path = require('path');
const AdmZip = require('adm-zip');
const TarGz = require('tar.gz');
const publishRelease = require('publish-release');
const rimraf = require('rimraf');
// helper
const mkdirSync = function (path) {
try {
fs.mkdirSync(path);
} catch (e) {
if (e.code != 'EEXIST') {
throw e;
}
}
};
// Find changelog for a specific version
const getChangelog = (changelogFilePath, forVersion) => {
const prefix = '<a name="';
const skipLines = 1;
return new Promise((resolve) => {
fs.readFile(changelogFilePath, (err, bytes) => {
if (err) {
console.log('Changelog file "' + changelogFilePath + '" not found!');
resolve({header : forVersion, body : ''});
} else {
let content = bytes.toString();
let lines = content.split('\n');
let bodyStartIndex = lines.findIndex((str) => str.substr(0, prefix.length + forVersion.length) === `${prefix}${forVersion}`);
if (bodyStartIndex > -1) {
let bodyEndIndex = lines.findIndex((str, idx) => idx > bodyStartIndex && str.substr(0, prefix.length) === prefix);
let header = forVersion;
let body = lines.slice(bodyStartIndex + 1 + skipLines, bodyEndIndex).join('\n');
resolve({header, body});
} else {
resolve({header : forVersion, body : ''});
}
}
});
});
};
// Build archive type .zip
// FIXME: fix handling of sub directory
const buildZipArchive = ({lookupDir, archiveFilePath}) => {
return new Promise((resolve, reject) => {
fs.readdir(lookupDir, (err, files) => {
if (err) {
reject();
} else {
let archive = new AdmZip();
files.forEach(file => {
let localFilename = path.join(lookupDir, file);
archive.addLocalFile(localFilename);
});
archive.writeZip(archiveFilePath);
resolve(archiveFilePath);
}
});
});
};
// Build archive type .tar.gz
const buildTarGzArchive = ({lookupDir, archiveFilePath}) => {
let gzipOptions = {
level : 9,
memLevel : 9,
};
let tarOptions = {
fromBase : true
};
return new TarGz(gzipOptions, tarOptions)
.compress(lookupDir, archiveFilePath)
.then(() => archiveFilePath);
};
// Upload and apply release
const publish = ({ghApiToken, projectVersion, projectVersionName, projectOwner, projectRepo, changelog, assets}) => {
return new Promise((resolve, reject) => {
publishRelease({
token : ghApiToken,
owner : projectOwner,
repo : projectRepo,
tag : projectVersion,
name : projectVersionName,
notes : changelog.body,
draft : false,
prerelease : false,
reuseRelease : true,
reuseDraftOnly : true,
assets : assets,
}, function (err, release) {
if (err) {
reject(err);
} else {
resolve(release);
}
})
});
};
// Main
// Load repo details
const pkg = require('./../package.json');
if (!process.env.GH_TOKEN) {
console.log('Missing env key GH_TOKEN');
process.exit(1);
}
//if (!process.env.PROJECT_OWNER) {
// console.log('Missing env key PROJECT_OWNER');
// process.exit(1);
//}
console.log(`Get changelog for version ${pkg.version}...`);
const changelogFilePath = path.normalize(path.join(__dirname, '../CHANGELOG.md'));
const distDir = path.normalize(path.join(__dirname, '..', 'dist'));
const tempDir = path.normalize(path.join(__dirname, '..', 'temp'));
const tempAssetsDir = path.normalize(path.join(__dirname, '..', 'temp', 'assets'));
// ensure temp structure
const resetTempResources = () => {
return new Promise((resolve) => {
if (fs.existsSync(tempAssetsDir)) {
// delete only
rimraf(tempAssetsDir, () => {
mkdirSync(tempAssetsDir);
resolve();
});
} else {
mkdirSync(tempDir);
mkdirSync(tempAssetsDir);
resolve();
}
});
};
// get changelog and get fresh archives
const bundle = () => {
return Promise.all([
getChangelog(changelogFilePath, pkg.version),
Promise.all([
buildZipArchive({lookupDir : distDir, archiveFilePath : `${tempAssetsDir}/${pkg.name}-${pkg.version}.zip`}),
buildTarGzArchive({lookupDir : distDir, archiveFilePath : `${tempAssetsDir}/${pkg.name}-${pkg.version}.tar.gz`}),
])
]);
};
resetTempResources()
.then(bundle)
.then(([changelog, assets]) => {
return publish({
ghApiToken : process.env.GH_TOKEN,
projectOwner : 'knalli',
projectRepo : process.env.PROJECT_REPO || pkg.name,
projectVersion : pkg.version,
projectVersionName : `${pkg.name} ${changelog.header || pkg.version}`,
changelog : changelog,
assets : assets,
});
})
.then(() => {
console.log('Release published successfully');
process.exit(0)
})
.catch((err) => {
console.log('Failed: ' + err);
process.exit(1);
});