diff --git a/lib/cache.js b/lib/cache.js index 65e9d30..9b66211 100644 --- a/lib/cache.js +++ b/lib/cache.js @@ -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' } @@ -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)) { @@ -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 = {} diff --git a/lib/server.js b/lib/server.js index f4b2e39..05df517 100644 --- a/lib/server.js +++ b/lib/server.js @@ -5,6 +5,7 @@ const { ACCOUNT: account, REPOSITORY: repository, PRE: pre, + ONLY_PRE: only_pre, TOKEN: token, URL: PRIVATE_BASE_URL, VERCEL_URL @@ -17,6 +18,7 @@ module.exports = hazel({ account, repository, pre, + only_pre, token, url }) diff --git a/test/cache.test.js b/test/cache.test.js index 2f76552..8c192ac 100644 --- a/test/cache.test.js +++ b/test/cache.test.js @@ -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',