Skip to content

Commit

Permalink
control over caching pre-release, stable release or latest
Browse files Browse the repository at this point in the history
  • Loading branch information
safinn authored and Dimitris committed Dec 23, 2021
1 parent a06e6c5 commit 93a1112
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ module.exports = class Cache {
}

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

const release = data.find(item => {
const isPre = Boolean(pre) === Boolean(item.prerelease)
return !item.draft && isPre
if (item.draft) return false
if (only_pre) {
return item.prerelease
}
if (!pre) {
return !item.prerelease
}
return true
})

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

// Clear list of download links
this.latest.platforms = {}
Expand Down
2 changes: 2 additions & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
ACCOUNT: account,
REPOSITORY: repository,
PRE: pre,
ONLY_PRE: only_pre,
TOKEN: token,
URL: PRIVATE_BASE_URL,
VERCEL_URL
Expand All @@ -17,6 +18,7 @@ module.exports = hazel({
account,
repository,
pre,
only_pre,
token,
url
})
36 changes: 36 additions & 0 deletions test/cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,42 @@ describe('Cache', () => {
expect(typeof storage.platforms).toBe('object')
})

it('a pre-release is cached', async () => {
const config = {
account: 'zeit',
repository: 'hyper',
token: process.env.TOKEN,
url: process.env.URL,
pre: false,
only_pre: true
}

const cache = new Cache(config)
await cache.refreshCache()
const storage = cache.loadCache()

expect(typeof storage.version).toBe('string')
expect(typeof storage.platforms).toBe('object')
expect(storage.prerelease).toBe(true)
})

it('a stable release is cached', async () => {
const config = {
account: 'zeit',
repository: 'hyper',
token: process.env.TOKEN,
url: process.env.URL
}

const cache = new Cache(config)
await cache.refreshCache()
const storage = cache.loadCache()

expect(typeof storage.version).toBe('string')
expect(typeof storage.platforms).toBe('object')
expect(storage.prerelease).toBe(false)
})

it('should set platforms correctly', async () => {
const config = {
account: 'zeit',
Expand Down

0 comments on commit 93a1112

Please sign in to comment.