Files
pnpm/lib/resolve/github.js
Zoltan Kochan b19369407a fix: don't use ! as a delimiter in the store
! can be part of a valid npm package name. Use + as a
delimiter instead.

close #276, PR #320

BREAKING CHANGE:

Stores created with the ! delimiter are not compatible
with the new version that uses +.

Any store created by older versions of pnpm has to be
removed and reinstalled.
2016-08-28 15:33:07 +03:00

71 lines
1.6 KiB
JavaScript

'use strict'
const pkgFullName = require('../pkg_full_name')
/**
* Resolves a 'hosted' package hosted on 'github'.
*/
const PARSE_GITHUB_RE = /^github:([^\/]+)\/([^#]+)(#(.+))?$/
module.exports = function resolveGithub (pkg, opts) {
const getJSON = opts.got.getJSON
const spec = parseGithubSpec(pkg)
return resolveRef(spec).then(ref => {
spec.ref = ref
return resolvePackageName(spec).then(name => {
return {
name,
fullname: pkgFullName({
name,
version: ['github', spec.owner, spec.repo, spec.ref].join(pkgFullName.delimiter)
}),
dist: {
tarball: [
'https://api.github.com/repos',
spec.owner,
spec.repo,
'tarball',
spec.ref
].join('/')
}
}
})
})
function resolvePackageName (spec) {
const url = [
'https://api.github.com/repos',
spec.owner,
spec.repo,
'contents/package.json?ref=' + spec.ref
].join('/')
return getJSON(url).then(body => {
const content = new Buffer(body.content, 'base64').toString('utf8')
const pkg = JSON.parse(content)
return pkg.name
})
}
function resolveRef (spec) {
const url = [
'https://api.github.com/repos',
spec.owner,
spec.repo,
'commits',
spec.ref
].join('/')
return getJSON(url).then(body => body.sha)
}
}
function parseGithubSpec (pkg) {
const m = PARSE_GITHUB_RE.exec(pkg.hosted.shortcut)
if (!m) {
throw new Error('cannot parse: ' + pkg.hosted.shortcut)
}
const owner = m[1]
const repo = m[2]
const ref = m[4] || 'HEAD'
return {owner, repo, ref}
}