1// take a path and a resolved value, and turn it into a resolution from 2// the given new path. This is used with converting a package.json's 3// relative file: path into one suitable for a lockfile, or between 4// lockfiles, and for converting hosted git repos to a consistent url type. 5const npa = require('npm-package-arg') 6const relpath = require('./relpath.js') 7const consistentResolve = (resolved, fromPath, toPath, relPaths = false) => { 8 if (!resolved) { 9 return null 10 } 11 12 try { 13 const hostedOpt = { noCommittish: false } 14 const { 15 fetchSpec, 16 saveSpec, 17 type, 18 hosted, 19 rawSpec, 20 raw, 21 } = npa(resolved, fromPath) 22 if (type === 'file' || type === 'directory') { 23 const cleanFetchSpec = fetchSpec.replace(/#/g, '%23') 24 if (relPaths && toPath) { 25 return `file:${relpath(toPath, cleanFetchSpec)}` 26 } 27 return `file:${cleanFetchSpec}` 28 } 29 if (hosted) { 30 return `git+${hosted.auth ? hosted.https(hostedOpt) : hosted.sshurl(hostedOpt)}` 31 } 32 if (type === 'git') { 33 return saveSpec 34 } 35 if (rawSpec === '*') { 36 return raw 37 } 38 return rawSpec 39 } catch (_) { 40 // whatever we passed in was not acceptable to npa. 41 // leave it 100% untouched. 42 return resolved 43 } 44} 45module.exports = consistentResolve 46