refactor: cleaner nuget implementation

pull/67/head
Amio 2018-08-04 00:25:51 +08:00
rodzic 6dae7effa8
commit 95712a4c73
2 zmienionych plików z 20 dodań i 31 usunięć

Wyświetl plik

@ -1,14 +1,13 @@
const axios = require('../axios.js')
const semColor = require('../utils/sem-color.js')
const pre = versions => versions.filter(v => v.includes('-'))
const stable = versions => versions.filter(v => !v.includes('-'))
const latest = versions => versions.length > 0 && versions.slice(-1)[0]
module.exports = async function (method, project, channel) {
const endpoint = `https://api.nuget.org/v3-flatcontainer/${project}/index.json`
const { versions } = await axios.get(endpoint).then(res => res.data)
const unknownBadgeData = {
subject: 'nuget',
status: 'unknown',
color: 'grey'
}
switch (method) {
case 'v':
@ -16,38 +15,28 @@ module.exports = async function (method, project, channel) {
switch (channel) {
case 'latest':
if (versions.length > 0) {
version = versions.slice(-1)[0]
}
version = latest(versions)
break
case 'pre':
const preReleaseVersions = versions.filter(v => v.indexOf('-') !== -1)
if (preReleaseVersions.length > 0) {
version = preReleaseVersions.slice(-1)[0]
}
version = latest(pre(versions))
break
default: // by default get stable version
const stableVersions = versions.filter(v => v.indexOf('-') === -1)
if (stableVersions.length > 0) {
version = stableVersions.slice(-1)[0]
}
default: // get stable version
version = latest(stable(versions))
}
// if in case version is still empty, try to get the latest
if (!version && versions.length > 0) {
version = versions.slice(-1)[0]
}
// in case version is still empty, try to get the latest
version = version || latest(versions)
if (version) {
return {
subject: 'nuget',
status: 'v' + version,
color: semColor(version)
}
} else {
return unknownBadgeData
return {
subject: 'nuget',
status: version ? `v${version}` : 'unknown',
color: semColor(version)
}
default:
return unknownBadgeData
return {
subject: 'nuget',
status: 'unknown',
color: 'grey'
}
}
}

Wyświetl plik

@ -11,7 +11,7 @@
*/
module.exports = function vc (version) {
if (version.match(/\b(alpha|beta|canary)\b/)) {
if (version.match(/\b(alpha|beta|canary)/)) {
return 'cyan'
}