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

Wyświetl plik

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