codekingpro/portable-devtools
114k
1// file dependencies need their dependencies resolved based on the location
2// where the tarball was found, not the location where they end up getting
3// installed. directory (ie, symlink) deps also need to be resolved based on
4// their targets, but that's what realpath is
5
6const { dirname } = require('node:path')
7const npa = require('npm-package-arg')
8
9const fromPath = (node, edge) => {
10 if (edge && edge.overrides && edge.overrides.name === edge.name && edge.overrides.value) {
11 // fromPath could be called with a node that has a virtual root, if that
12 // happens we want to make sure we get the real root node when overrides
13 // are in use. this is to allow things like overriding a dependency with a
14 // tarball file that's a relative path from the project root
15 if (node.sourceReference) {
16 return node.sourceReference.root.realpath
17 }
18 return node.root.realpath
19 }
20
21 if (node.resolved) {
22 const spec = npa(node.resolved)
23 if (spec?.type === 'file') {
24 return dirname(spec.fetchSpec)
25 }
26 }
27 return node.realpath
28}
29
30module.exports = fromPath
31 