forked from bchr02/node-pre-gyp-github
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
222 lines (166 loc) · 6.12 KB
/
index.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
"use strict";
var path = require("path");
var fs = require('fs');
var mime = require("mime-types");
var cwd = process.cwd();
var verbose = false;
var consoleLog = function(x) {
return (verbose) ? console.log(x) : false;
};
const { Octokit } = require("@octokit/rest");
class NodePreGypGithub {
constructor() {
// super();
this.stage_dir = path.join(cwd, "build", "stage");
}
init() {
console.log("init()");
var ownerRepo, hostPrefix;
this.package_json = JSON.parse(fs.readFileSync(path.join(cwd, 'package.json')));
if (!this.package_json.repository || !this.package_json.repository.url) {
throw new Error('Missing repository.url in package.json');
}
else {
ownerRepo = this.package_json.repository.url.match(/https?:\/\/([^\/]+)\/(.*)(?=\.git)/i);
if (ownerRepo) {
this.host = 'api.' + ownerRepo[1];
ownerRepo = ownerRepo[2].split('/');
this.owner = ownerRepo[0];
this.repo = ownerRepo[1];
}
else throw new Error('A correctly formatted GitHub repository.url was not found within package.json');
}
hostPrefix = 'https://' + this.host + '/' + this.owner + '/' + this.repo + '/releases/download/';
if (!this.package_json.binary || 'object' !== typeof this.package_json.binary || 'string' !== typeof this.package_json.binary.host) {
throw new Error('Missing binary.host in package.json');
}
else if (this.package_json.binary.host.replace('https://', 'https://api.').substr(0, hostPrefix.length) !== hostPrefix) {
throw new Error('binary.host in package.json should begin with: "' + hostPrefix + '"');
}
this.octokit = this.octokit || new Octokit({
auth: process.env.NODE_PRE_GYP_GITHUB_TOKEN,
baseUrl: 'https://' + this.host,
headers: {
"user-agent": (this.package_json.name) ? this.package_json.name : "node-pre-gyp-github"
}
});
}
authenticate_settings() {
var token = process.env.NODE_PRE_GYP_GITHUB_TOKEN;
if (!token) throw new Error(
//'Please specify NODE_PRE_GYP_GITHUB_TOKEN');
'NODE_PRE_GYP_GITHUB_TOKEN environment variable not found');
return {
"type": "oauth",
"token": token
};
}
async createRelease(args) {
var options = {
'host': this.host,
'owner': this.owner,
'repo': this.repo,
'tag_name': this.package_json.version,
'target_commitish': 'master',
'name': 'v' + this.package_json.version,
'body': this.package_json.name + ' ' + this.package_json.version,
'draft': true,
'prerelease': false
};
Object.keys(args).forEach(function(key) {
if (args.hasOwnProperty(key) && options.hasOwnProperty(key)) {
options[key] = args[key];
}
});
//xx this.octokit.authenticate(this.authenticate_settings());
return this.octokit.repos.createRelease(options);
}
async uploadReleaseAsset(cfg) {
// this.octokit.authenticate(this.authenticate_settings());
const contentType = mime.contentType(cfg.fileName) || 'application/octet-stream';
const data = fs.readFileSync(cfg.filePath);
await this.octokit.repos.uploadReleaseAsset({
url: this.release.upload_url,
owner: this.owner,
id: this.release.id,
repo: this.repo,
name: cfg.fileName,
data,
contentType
});
consoleLog('Staged file ' + cfg.fileName + ' saved to ' + this.owner + '/' + this.repo + ' release ' + this.release.tag_name + ' successfully.');
}
async uploadReleaseAssets() {
var asset;
consoleLog("Stage directory path: " + path.join(this.stage_dir));
const pThis = this;
return new Promise((resolve, reject) => {
fs.readdir(path.join(this.stage_dir), async (err, files) => {
if (err) { return reject(err); }
if (!files.length) {
return reject(new Error('No files found within the stage directory: ' + this.stage_dir));
}
for (const file of files) {
if (pThis.release && pThis.release.assets) {
asset = pThis.release.assets.filter((element) => element.name === file);
if (asset.length) {
return reject(
new Error("Staged file " + file + " found but it already exists in release " + this.release.tag_name +
". If you would like to replace it, you must first manually delete it within GitHub."));
}
}
consoleLog("Staged file " + file + " found. Proceeding to upload it.");
await pThis.uploadReleaseAsset({
fileName: file,
filePath: path.join(pThis.stage_dir, file)
});
}
resolve();
});
});
}
async publish(options) {
try {
options = (typeof options === 'undefined') ? {} : options;
verbose = (typeof options.verbose === 'undefined' || options.verbose) ? true : false;
if (!process.env.NODE_PRE_GYP_GITHUB_TOKEN) {
throw new Error("NODE_PRE_GYP_GITHUB_TOKEN environment variable not found");
}
this.init();
// this.octokit.authenticate(this.authenticate_settings());
const pThis = this;
const data = await pThis.octokit.repos.listReleases({
'owner': this.owner,
'repo': this.repo
});
var release;
// when remote_path is set expect files to be in stage_dir / remote_path after substitution
if (pThis.package_json.binary.remote_path) {
options.tag_name = pThis.package_json.binary.remote_path.replace(/\{version\}/g, this.package_json.version);
pThis.stage_dir = path.join(pThis.stage_dir, options.tag_name);
} else {
// This is here for backwards compatibility for before binary.remote_path support was added in version 1.2.0.
options.tag_name = pThis.package_json.version;
}
release = data.data.filter((element, index, array) => element.tag_name === options.tag_name);
if (release.length === 0) {
const release = await pThis.createRelease(options);
pThis.release = release.data;
if (pThis.release.draft) {
consoleLog('Release ' + pThis.release.tag_name + " not found, so a draft release was created. YOU MUST MANUALLY PUBLISH THIS DRAFT WITHIN GITHUB FOR IT TO BE ACCESSIBLE.");
}
else {
consoleLog('Release ' + release.tag_name + " not found, so a new release was created and published.");
}
await pThis.uploadReleaseAssets(pThis.release.upload_url);
}
else {
pThis.release = release[0];
await pThis.uploadReleaseAssets();
}
} catch (err) {
throw err;
}
}
}
module.exports = NodePreGypGithub;