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 committed Jul 29, 2018
1 parent 77abdbd commit 3312a63
Show file tree
Hide file tree
Showing 5 changed files with 831 additions and 862 deletions.
13 changes: 10 additions & 3 deletions lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,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 All @@ -80,8 +80,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 @@ -100,6 +106,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,
NOW_URL
Expand All @@ -17,6 +18,7 @@ module.exports = hazel({
account,
repository,
pre,
only_pre,
token,
url
})
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
"no-await-in-loop": 0
}
},
"jest": {
"verbose": true,
"testURL": "http://localhost/"
},
"now": {
"env": [
"ACCOUNT",
Expand Down Expand Up @@ -59,7 +63,7 @@
"devDependencies": {
"eslint-config-prettier": "2.9.0",
"husky": "0.14.3",
"lint-staged": "7.0.0",
"lint-staged": "7.2.0",
"micro-dev": "2.2.0",
"prettier": "1.10.2",
"xo": "0.20.3"
Expand Down
36 changes: 36 additions & 0 deletions test/cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,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
Loading

0 comments on commit 3312a63

Please sign in to comment.