Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Control over caching pre-release, stable release or latest #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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