-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.js
More file actions
288 lines (265 loc) · 8.41 KB
/
Copy pathversion.js
File metadata and controls
288 lines (265 loc) · 8.41 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
const EventEmitter = require('microevent');
const BASE_URL = (window && window.version_base_url) || 'https://api.releasepage.co';
const HELP_URL = (window && window.help_base_url) || 'https://help.releasepage.co/api/getting-started';
const GITHUB_BASE_URL = 'https://api.github.com/repos/:owner/:repo/releases/latest';
const Version = function (opts = {}) {
this.opts = opts;
this.bind('load', () => this.render());
};
Version.prototype = {
options(opts) {
this.opts = opts;
return this;
},
load() {
if (this.opts.github) {
this.useGitHub = true;
} else {
if (!this.opts.apiKey) {
console.error('version.js: no key provided');
}
if (!this.opts.pageId) {
console.error('version.js: no pageId provided');
}
}
const onLoad = this.onLoad.bind(this);
const xhr = new XMLHttpRequest();
xhr.addEventListener('load', function () {
onLoad({ status: this.status, response: this.response });
});
const url = this.getUrl();
xhr.open('GET', url);
xhr.send();
},
getUrl() {
if (this.useGitHub) {
const { repo } = this.opts.github;
return GITHUB_BASE_URL.replace(':owner/:repo', repo);
}
return `${BASE_URL}/v1/pages/${this.opts.pageId}/version?apiKey=${this.opts.apiKey}`;
},
onLoad(resp) {
return this.useGitHub ? this.parseGitHubResponse(resp) : this.parseReleasePageResponse(resp);
},
parseGitHubResponse({ status, response }) {
switch (status) {
case 401:
return console.error('version.js: Bad GitHub credentials');
case 403:
return console.error('version.js: Access is denied to that GitHub repository');
case 404:
return console.error('version.js: GitHub repository not found');
case 301:
return console.error('version.js: GitHub repository has moved permanently');
case 302:
case 307:
return console.error('version.js: GitHub repository has moved');
case 200: {
const data = JSON.parse(response);
this.latest = [{
repo: this.opts.github.repo,
version: data.tag_name,
author: data.author.login,
published_at: data.published_at
}];
break;
}
default:
return console.error('version.js: GitHub API error', status, response);
}
return this.trigger('load');
},
parseReleasePageResponse({ status, response }) {
switch (status) {
case 404:
return console.error(
`version.js: A Release Page with id ${this.opts.pageId} does not exist`
);
case 401:
console.error(
`version.js: enable the version api for this Release Page: ${HELP_URL}`
);
break;
case 200: {
const data = JSON.parse(response);
this.latest = data.latest;
this.latestGrouped = data.latestGrouped;
this.permalink = data.permalink;
break;
}
default:
return console.error('version.js: ReleasePage API error', status, response);
}
return this.trigger('load');
},
render() {
const badgeEls = document.querySelectorAll('[data-version-badge]');
if (badgeEls.length) {
this.renderBadges({ elements: badgeEls });
}
const repoNameEls = document.querySelectorAll('[data-repo-name]');
if (repoNameEls.length) {
this.renderRepoNames({ elements: repoNameEls });
}
const authorEls = document.querySelectorAll('[data-version-author]');
if (authorEls.length) {
this.renderAuthors({ elements: authorEls });
}
const renderPublishedAt = document.querySelectorAll('[data-version-published]');
if (renderPublishedAt.length) {
this.renderPublishedAt({ elements: renderPublishedAt });
}
return this;
},
renderAuthors({ elements }) {
return elements.forEach((el) => {
const repo = el.parentElement.getAttribute('data-version-for');
let authors;
if (repo) {
authors = [this.latest.find(l => new RegExp(l.repo, 'i').test(repo)).author];
} else if (this.latestGrouped) {
authors = this.latestGrouped.authors;
} else {
authors = this.latest.reduce((all, l) => {
if (all.includes(l.author)) return all;
return [...all, l.author];
}, []);
}
// eslint-disable-next-line no-param-reassign
el.textContent = formatArray(authors);
});
},
renderPublishedAt({ elements }) {
return elements.forEach((el) => {
const repo = el.parentElement.getAttribute('data-version-for');
let publishedAt;
if (repo) {
publishedAt = this.latest.find(l => new RegExp(l.repo, 'i').test(repo)).published_at;
} else if (this.isGrouped()) {
publishedAt = this.latestGrouped.published_at;
} else {
publishedAt = this.latest[0].published_at;
}
// eslint-disable-next-line no-param-reassign
el.textContent = new Date(publishedAt).toDateString();
});
},
renderRepoNames({ elements }) {
return elements.forEach((el) => {
const repo = el.parentElement.getAttribute('data-version-for');
let names;
if (repo) {
// use the repo provided
const repoDetails = this.latest.find(l => new RegExp(l.repo, 'i').test(repo));
names = [repoDetails.name || repoDetails.repo];
} else {
// else use all
names = this.latest.map(l => l.name || l.repo);
}
// eslint-disable-next-line no-param-reassign
el.textContent = formatArray(names);
});
},
renderBadges({ elements }) {
elements.forEach((el) => {
const repo = el.parentElement.getAttribute('data-version-for');
let version;
if (repo) {
// use the repo provided
version = this.latest.find(l => new RegExp(l.repo, 'i').test(repo)).version;
} else if (this.isGrouped()) {
// if no repo provided then use the grouped version number
version = this.latestGrouped.version;
} else {
// if no version yet then pick the latest one to use
version = this.latest[0].version || this.latest[0].version;
}
// eslint-disable-next-line no-param-reassign
el.textContent = version;
});
},
publishedAt({ repo } = {}) {
if (this.isGrouped()) {
return this.latestGrouped.published_at;
}
if (repo) {
const repoDetails = this.latest.find(l => new RegExp(l.repo, 'i').test(repo));
return repoDetails ? repoDetails.published_at : null;
}
return this.latest[0].published_at;
},
author({ repo } = {}) {
if (this.isGrouped()) {
return this.latestGrouped.authors;
}
if (repo) {
const repoDetails = this.latest.find(l => new RegExp(l.repo, 'i').test(repo));
return repoDetails ? repoDetails.author : null;
}
return this.latest[0].author;
},
tag({ repo } = {}) {
if (localStorage && localStorage.getItem('versionjs.debug_version')) {
return localStorage.getItem('versionjs.debug_version');
}
if (this.isGrouped()) {
return this.latestGrouped.version;
}
if (repo) {
const repoDetails = this.latest.find(l => new RegExp(l.repo, 'i').test(repo));
return repoDetails ? repoDetails.version : null;
}
return this.latest[0].version;
},
isGrouped() {
return !!this.latestGrouped;
}
};
EventEmitter.mixin(Version);
function formatArray(arr) {
let outStr = '';
if (arr.length === 1) {
outStr = arr[0];
} else if (arr.length === 2) {
// joins all with "and" but no commas
// example: "bob and sam"
outStr = arr.join(' and ');
} else if (arr.length > 2) {
// joins all with commas, but last one gets ", and" (oxford comma!)
// example: "bob, joe, and sam"
outStr = `${arr.slice(0, -1).join(', ')}, and ${arr.slice(-1)}`;
}
return outStr;
}
let version;
const el = document.querySelector('script[data-page-id]');
const githubEl = document.querySelector('script[data-repo]');
if (el) {
const pageId = el.getAttribute('data-page-id');
const apiKey = el.getAttribute('data-api-key');
if (pageId) {
version = new Version({
pageId,
apiKey
});
}
} else if (githubEl) {
const repo = githubEl.getAttribute('data-repo');
version = new Version({
github: {
repo
}
});
} else {
version = new Version();
}
if (module) {
module.exports = version;
}
if (typeof window !== 'undefined') {
window.version = version;
// set up automatically
document.addEventListener('DOMContentLoaded', () => {
version.load();
});
}