I recently published https://github.com/metabolize/rq-dashboard-on-heroku and want to add badges to show the locked version of Python and rq-dashboard, the main dependency it’s wrapping. This is along the lines of #2259, which was for package.json-based applications, and also included some discussion of a Python application that used `requirements.txt`. It’s useful for showing the pinned version of any dependency in a Python application that uses a lockfile. In the future, as an alternative to reading Pipfile.lock, I could see expanding this to read Pipfile. However for my purposes I prefer to show the locked dependency, since that’s the version that a user of my package would actually get if they ran it on Heroku.
59 lines
1.3 KiB
JavaScript
59 lines
1.3 KiB
JavaScript
'use strict'
|
|
|
|
const Joi = require('@hapi/joi')
|
|
const { InvalidParameter } = require('.')
|
|
|
|
const isDependency = Joi.alternatives(
|
|
Joi.object({
|
|
version: Joi.string().required(),
|
|
}).required(),
|
|
Joi.object({
|
|
ref: Joi.string().required(),
|
|
}).required()
|
|
)
|
|
|
|
const isLockfile = Joi.object({
|
|
_meta: Joi.object({
|
|
requires: Joi.object({
|
|
python_version: Joi.string(),
|
|
}).required(),
|
|
}).required(),
|
|
default: Joi.object().pattern(Joi.string().required(), isDependency),
|
|
develop: Joi.object().pattern(Joi.string().required(), isDependency),
|
|
}).required()
|
|
|
|
function getDependencyVersion({
|
|
kind = 'default',
|
|
wantedDependency,
|
|
lockfileData,
|
|
}) {
|
|
let dependenciesOfKind
|
|
if (kind === 'dev') {
|
|
dependenciesOfKind = lockfileData.develop
|
|
} else if (kind === 'default') {
|
|
dependenciesOfKind = lockfileData.default
|
|
} else {
|
|
throw Error(`Not very kind: ${kind}`)
|
|
}
|
|
|
|
if (!(wantedDependency in dependenciesOfKind)) {
|
|
throw new InvalidParameter({
|
|
prettyMessage: `${kind} dependency not found`,
|
|
})
|
|
}
|
|
|
|
const { version, ref } = dependenciesOfKind[wantedDependency]
|
|
|
|
if (version) {
|
|
// Strip the `==` which is always present.
|
|
return { version: version.replace('==', '') }
|
|
} else {
|
|
return { ref: ref.substring(1, 8) }
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
isLockfile,
|
|
getDependencyVersion,
|
|
}
|