Skip to content

Commit 93a1112

Browse files
safinnDimitris
authored andcommitted
control over caching pre-release, stable release or latest
1 parent a06e6c5 commit 93a1112

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

lib/cache.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ module.exports = class Cache {
7575
}
7676

7777
async refreshCache() {
78-
const { account, repository, pre, token } = this.config
78+
const { account, repository, pre, only_pre, token } = this.config
7979
const repo = account + '/' + repository
8080
const url = `https://api.github.com/repos/${repo}/releases?per_page=100`
8181
const headers = { Accept: 'application/vnd.github.preview' }
@@ -106,8 +106,14 @@ module.exports = class Cache {
106106
}
107107

108108
const release = data.find(item => {
109-
const isPre = Boolean(pre) === Boolean(item.prerelease)
110-
return !item.draft && isPre
109+
if (item.draft) return false
110+
if (only_pre) {
111+
return item.prerelease
112+
}
113+
if (!pre) {
114+
return !item.prerelease
115+
}
116+
return true
111117
})
112118

113119
if (!release || !release.assets || !Array.isArray(release.assets)) {
@@ -127,6 +133,7 @@ module.exports = class Cache {
127133
this.latest.version = tag_name
128134
this.latest.notes = release.body
129135
this.latest.pub_date = release.published_at
136+
this.latest.prerelease = release.prerelease
130137

131138
// Clear list of download links
132139
this.latest.platforms = {}

lib/server.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const {
55
ACCOUNT: account,
66
REPOSITORY: repository,
77
PRE: pre,
8+
ONLY_PRE: only_pre,
89
TOKEN: token,
910
URL: PRIVATE_BASE_URL,
1011
VERCEL_URL
@@ -17,6 +18,7 @@ module.exports = hazel({
1718
account,
1819
repository,
1920
pre,
21+
only_pre,
2022
token,
2123
url
2224
})

test/cache.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,42 @@ describe('Cache', () => {
5050
expect(typeof storage.platforms).toBe('object')
5151
})
5252

53+
it('a pre-release is cached', async () => {
54+
const config = {
55+
account: 'zeit',
56+
repository: 'hyper',
57+
token: process.env.TOKEN,
58+
url: process.env.URL,
59+
pre: false,
60+
only_pre: true
61+
}
62+
63+
const cache = new Cache(config)
64+
await cache.refreshCache()
65+
const storage = cache.loadCache()
66+
67+
expect(typeof storage.version).toBe('string')
68+
expect(typeof storage.platforms).toBe('object')
69+
expect(storage.prerelease).toBe(true)
70+
})
71+
72+
it('a stable release is cached', async () => {
73+
const config = {
74+
account: 'zeit',
75+
repository: 'hyper',
76+
token: process.env.TOKEN,
77+
url: process.env.URL
78+
}
79+
80+
const cache = new Cache(config)
81+
await cache.refreshCache()
82+
const storage = cache.loadCache()
83+
84+
expect(typeof storage.version).toBe('string')
85+
expect(typeof storage.platforms).toBe('object')
86+
expect(storage.prerelease).toBe(false)
87+
})
88+
5389
it('should set platforms correctly', async () => {
5490
const config = {
5591
account: 'zeit',

0 commit comments

Comments
 (0)