fetchYarnDeps: support more url types

This commit is contained in:
Yureka 2021-12-10 11:36:00 +01:00 committed by Yuka
parent 1aec248ccf
commit c420487e0b

View File

@ -9,6 +9,7 @@ const child_process = require('child_process')
const path = require('path') const path = require('path')
const lockfile = require('./yarnpkg-lockfile.js') const lockfile = require('./yarnpkg-lockfile.js')
const { promisify } = require('util') const { promisify } = require('util')
const url = require('url')
const execFile = promisify(child_process.execFile) const execFile = promisify(child_process.execFile)
@ -72,6 +73,23 @@ const downloadGit = async (fileName, url, rev) => {
await exec('rm', [ '-rf', fileName + '.tmp', ]) await exec('rm', [ '-rf', fileName + '.tmp', ])
} }
const isGitUrl = pattern => {
// https://github.com/yarnpkg/yarn/blob/3119382885ea373d3c13d6a846de743eca8c914b/src/resolvers/exotics/git-resolver.js#L15-L47
const GIT_HOSTS = ['github.com', 'gitlab.com', 'bitbucket.com', 'bitbucket.org']
const GIT_PATTERN_MATCHERS = [/^git:/, /^git\+.+:/, /^ssh:/, /^https?:.+\.git$/, /^https?:.+\.git#.+/]
for (const matcher of GIT_PATTERN_MATCHERS) if (matcher.test(pattern)) return true
const {hostname, path} = url.parse(pattern)
if (hostname && path && GIT_HOSTS.indexOf(hostname) >= 0
// only if dependency is pointing to a git repo,
// e.g. facebook/flow and not file in a git repo facebook/flow/archive/v1.0.0.tar.gz
&& path.split('/').filter(p => !!p).length === 2
) return true
return false
}
const downloadPkg = (pkg, verbose) => { const downloadPkg = (pkg, verbose) => {
const [ url, hash ] = pkg.resolved.split('#') const [ url, hash ] = pkg.resolved.split('#')
if (verbose) console.log('downloading ' + url) if (verbose) console.log('downloading ' + url)
@ -79,12 +97,10 @@ const downloadPkg = (pkg, verbose) => {
if (url.startsWith('https://codeload.github.com/') && url.includes('/tar.gz/')) { if (url.startsWith('https://codeload.github.com/') && url.includes('/tar.gz/')) {
const s = url.split('/') const s = url.split('/')
downloadGit(fileName, `https://github.com/${s[3]}/${s[4]}.git`, s[6]) downloadGit(fileName, `https://github.com/${s[3]}/${s[4]}.git`, s[6])
} else if (isGitUrl(url)) {
return downloadGit(fileName, url.replace(/^git\+/, ''), hash)
} else if (url.startsWith('https://')) { } else if (url.startsWith('https://')) {
return downloadFileHttps(fileName, url, hash) return downloadFileHttps(fileName, url, hash)
} else if (url.startsWith('git:')) {
return downloadGit(fileName, url.replace(/^git\+/, ''), hash)
} else if (url.startsWith('git+')) {
return downloadGit(fileName, url.replace(/^git\+/, ''), hash)
} else if (url.startsWith('file:')) { } else if (url.startsWith('file:')) {
console.warn(`ignoring unsupported file:path url "${url}"`) console.warn(`ignoring unsupported file:path url "${url}"`)
} else { } else {