Skip to content

Commit

Permalink
Rewrite [pypi]; affects [npm] (#1922)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmelnikow authored Aug 27, 2018
1 parent 78bb890 commit 302c860
Show file tree
Hide file tree
Showing 25 changed files with 691 additions and 273 deletions.
51 changes: 10 additions & 41 deletions lib/all-badge-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,21 +614,6 @@ const allBadgeExamples = [
title: 'PowerShell Gallery',
previewUri: '/powershellgallery/dt/ACMESharp.svg',
},
{
title: 'PyPI',
previewUri: '/pypi/dm/Django.svg',
keywords: ['python'],
},
{
title: 'PyPI',
previewUri: '/pypi/dw/Django.svg',
keywords: ['python'],
},
{
title: 'PyPI',
previewUri: '/pypi/dd/Django.svg',
keywords: ['python'],
},
{
title: 'Conda',
previewUri: '/conda/dn/conda-forge/python.svg',
Expand Down Expand Up @@ -1048,11 +1033,6 @@ const allBadgeExamples = [
title: 'Bower',
previewUri: '/bower/l/bootstrap.svg',
},
{
title: 'PyPI - License',
previewUri: '/pypi/l/Django.svg',
keywords: ['python', 'pypi'],
},
{
title: 'Hex.pm',
previewUri: '/hexpm/l/plug.svg',
Expand Down Expand Up @@ -1222,9 +1202,14 @@ const allBadgeExamples = [
},
examples: [
{
title: 'PyPI',
previewUri: '/pypi/v/nine.svg',
keywords: ['python'],
title: 'npm bundle size (minified)',
previewUri: '/bundlephobia/min/react.svg',
keywords: ['node'],
},
{
title: 'npm bundle size (minified + gzip)',
previewUri: '/bundlephobia/minzip/react.svg',
keywords: ['node'],
},
{
title: 'Conda',
Expand Down Expand Up @@ -1552,24 +1537,8 @@ const allBadgeExamples = [
},
examples: [
{
title: 'PyPI - Wheel',
previewUri: '/pypi/wheel/Django.svg',
keywords: ['python', 'pypi'],
},
{
title: 'PyPI - Format',
previewUri: '/pypi/format/Django.svg',
keywords: ['python', 'pypi'],
},
{
title: 'PyPI - Implementation',
previewUri: '/pypi/implementation/Django.svg',
keywords: ['python', 'pypi'],
},
{
title: 'PyPI - Status',
previewUri: '/pypi/status/Django.svg',
keywords: ['python', 'pypi'],
title: 'VersionEye',
previewUri: '/versioneye/d/ruby/rails.svg',
},
{
title: 'Wheelmap',
Expand Down
8 changes: 8 additions & 0 deletions lib/deprecation-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const { makeBadgeData, setBadgeColor } = require('./badge-data')
const { deprecatedServices } = require('./deprecated-services')
const { Deprecated } = require('../services/errors')

const isDeprecated = function(
service,
Expand All @@ -21,7 +22,14 @@ const getDeprecatedBadge = function(label, data) {
return badgeData
}

function enforceDeprecation(effectiveDate) {
if (Date.now() >= effectiveDate.getTime()) {
throw new Deprecated()
}
}

module.exports = {
isDeprecated,
getDeprecatedBadge,
enforceDeprecation,
}
22 changes: 21 additions & 1 deletion lib/licenses.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
'use strict'

const { toArray } = require('./badge-data')

const licenseTypes = {
// permissive licenses - not public domain and not copyleft
permissive: {
Expand Down Expand Up @@ -59,4 +62,21 @@ const defaultLicenseColor = 'lightgrey'
const licenseToColor = spdxId =>
licenseToColorMap[spdxId] || defaultLicenseColor

module.exports = { licenseToColor }
function renderLicenseBadge({ license, licenses }) {
if (licenses === undefined) {
licenses = toArray(license)
}

if (licenses.length === 0) {
return { message: 'missing', color: 'red' }
}

return {
message: licenses.join(', '),
// TODO This does not provide a color when more than one license is
// present. Probably that should be fixed.
color: licenseToColor(licenses),
}
}

module.exports = { licenseToColor, renderLicenseBadge }
26 changes: 24 additions & 2 deletions lib/licenses.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { test, given } = require('sazerac')
const { licenseToColor } = require('./licenses')
const { test, given, forCases } = require('sazerac')
const { licenseToColor, renderLicenseBadge } = require('./licenses')

describe('license helpers', function() {
test(licenseToColor, () => {
Expand All @@ -11,4 +11,26 @@ describe('license helpers', function() {
given('unknown-license').expect('lightgrey')
given(null).expect('lightgrey')
})

test(renderLicenseBadge, () => {
forCases([
given({ license: undefined }),
given({ licenses: [] }),
given({}),
]).expect({
message: 'missing',
color: 'red',
})
forCases([
given({ license: 'WTFPL' }),
given({ licenses: ['WTFPL'] }),
]).expect({
message: 'WTFPL',
color: '7cd958',
})
given({ licenses: ['MPL-2.0', 'MIT'] }).expect({
message: 'MPL-2.0, MIT',
color: 'lightgrey',
})
})
})
11 changes: 11 additions & 0 deletions lib/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
'use strict'

const semver = require('semver')
const { addv } = require('./text-formatters')
const { version: versionColor } = require('./color-formatters')

// Given a list of versions (as strings), return the latest version.
// Return undefined if no version could be found.
Expand Down Expand Up @@ -139,10 +141,19 @@ function rangeStart(v) {
return range.set[0][0].semver.version
}

function renderVersionBadge({ version, tag, defaultLabel }) {
return {
label: tag ? `${defaultLabel}@${tag}` : undefined,
message: addv(version),
color: versionColor(version),
}
}

module.exports = {
latest,
listCompare,
slice,
minor,
rangeStart,
renderVersionBadge,
}
15 changes: 14 additions & 1 deletion lib/version.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { test, given } = require('sazerac')
const { latest, slice, rangeStart } = require('./version')
const { latest, slice, rangeStart, renderVersionBadge } = require('./version')
const includePre = true

describe('Version helpers', function() {
Expand Down Expand Up @@ -119,4 +119,17 @@ describe('Version helpers', function() {
test(rangeStart, () => {
given('^2.4.7').expect('2.4.7')
})

test(renderVersionBadge, () => {
given({ version: '1.2.3' }).expect({
label: undefined,
message: 'v1.2.3',
color: 'blue',
})
given({ version: '1.2.3', tag: 'next', defaultLabel: 'npm' }).expect({
label: 'npm@next',
message: 'v1.2.3',
color: 'blue',
})
})
})
Loading

0 comments on commit 302c860

Please sign in to comment.