Skip to content

Commit

Permalink
Add /download/dmg support (#69)
Browse files Browse the repository at this point in the history
* Add `/download/dmg` support

* Continue exit code

* Add test
  • Loading branch information
AndyBitz committed Sep 23, 2019
1 parent 6c13ee7 commit 2c970a0
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 36 deletions.
5 changes: 3 additions & 2 deletions lib/aliases.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ const aliases = {
exe: ['win32', 'windows', 'win'],
deb: ['debian'],
rpm: ['fedora'],
AppImage: ['appimage']
AppImage: ['appimage'],
dmg: ['dmg']
}

module.exports = platform => {
if(typeof aliases[platform] !== 'undefined') {
if (typeof aliases[platform] !== 'undefined') {
return platform
}

Expand Down
52 changes: 30 additions & 22 deletions lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ module.exports = class Cache {
}

if (token && !url) {
const error = new Error('Neither NOW_URL, nor URL are defined, which are mandatory for private repo mode')
const error = new Error(
'Neither NOW_URL, nor URL are defined, which are mandatory for private repo mode'
)
error.code = 'missing_configuration_properties'
throw error
}

this.latest = {}
this.lastUpdate = null;
this.lastUpdate = null

this.cacheReleaseList = this.cacheReleaseList.bind(this)
this.refreshCache = this.refreshCache.bind(this)
Expand All @@ -41,17 +43,20 @@ module.exports = class Cache {
headers.Authorization = `token ${token}`
}

const { status, body } = await retry(async () => {
const response = await fetch(url, { headers })
const { status, body } = await retry(
async () => {
const response = await fetch(url, { headers })

if (response.status !== 200) {
throw new Error(
`Tried to cache RELEASES, but failed fetching ${url}, status ${status}`
)
}
if (response.status !== 200) {
throw new Error(
`Tried to cache RELEASES, but failed fetching ${url}, status ${status}`
)
}

return response;
}, { retries: 3 });
return response
},
{ retries: 3 }
)

const content = await convertStream(body)
const matches = content.match(/[^ ]*\.nupkg/gim)
Expand All @@ -76,17 +81,20 @@ module.exports = class Cache {
headers.Authorization = `token ${token}`
}

const response = await retry(async () => {
const response = await fetch(url, { headers })
const response = await retry(
async () => {
const response = await fetch(url, { headers })

if (response.status !== 200) {
throw new Error(
`GitHub API responded with ${response.status} for url ${url}`
)
}
if (response.status !== 200) {
throw new Error(
`GitHub API responded with ${response.status} for url ${url}`
)
}

return response;
}, { retries: 3 });
return response
},
{ retries: 3 }
)

const data = await response.json()

Expand Down Expand Up @@ -160,7 +168,7 @@ module.exports = class Cache {
const { lastUpdate, config } = this
const { interval = 15 } = config

if (lastUpdate && (Date.now() - lastUpdate) > ms(`${interval}m`)) {
if (lastUpdate && Date.now() - lastUpdate > ms(`${interval}m`)) {
return true
}

Expand All @@ -177,6 +185,6 @@ module.exports = class Cache {
await refreshCache()
}

return latest
return Object.assign({}, latest)
}
}
17 changes: 7 additions & 10 deletions lib/platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@
const { extname } = require('path')

module.exports = fileName => {
const extension = extname(fileName).split('.')[1]
const extension = extname(fileName).slice(1)

if ((fileName.includes('mac') || fileName.includes('darwin')) && extension === 'zip') {
if (
(fileName.includes('mac') || fileName.includes('darwin')) &&
extension === 'zip'
) {
return 'darwin'
}

const directCache = ['exe', 'rpm', 'deb', 'AppImage']
const index = directCache.indexOf(extension)

if (index > -1) {
return directCache[index]
}

return false
const directCache = ['exe', 'dmg', 'rpm', 'deb', 'AppImage']
return directCache.find(ext => ext === extension) || false
}
8 changes: 8 additions & 0 deletions lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ module.exports = ({ cache, config }) => {
exports.overview = async (req, res) => {
const latest = await loadCache()

// Filter out special platforms that should not
// be shown on the overview page
Object.keys(latest.platforms).forEach(platform => {
if (platform === 'dmg') {
delete latest.platforms[platform]
}
})

try {
const render = await prepareView()

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
},
"lint-staged": {
"*.js": [
"yarn test",
"prettier --single-quote --no-semi --write",
"yarn test && :",
"prettier --single-quote --no-semi --write --no-editorconfig",
"git add"
]
},
Expand Down
5 changes: 5 additions & 0 deletions test/platform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ describe('Platform', () => {
expect(result).toBe('deb')
})

it('Should parse dmg', () => {
const result = platform('hyper-2.1.1.dmg')
expect(result).toBe('dmg')
})

it('Should return false for unknown files', () => {
const result = platform('hi.txt')
expect(result).toBe(false)
Expand Down

1 comment on commit 2c970a0

@vercel
Copy link

@vercel vercel bot commented on 2c970a0 Sep 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.