Skip to content

Commit

Permalink
dapp: remappings: minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
d-xo committed Aug 4, 2021
1 parent 64652e9 commit d80f3c1
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions src/dapp/libexec/dapp/dapp-remappings
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env node
const PROGRAM_NAME = process.argv[1].replace(/.*\//, "")

const tree = buildDependencyTree(".")
console.log(buildRemappings(deduplicate(mapHashes(tree), tree)).join("\n"))
console.log(buildRemappings(deduplicate(buildDependencyTree("."))).join("\n"))

// builds a in memory representation of the projects dependency tree
//
Expand Down Expand Up @@ -38,22 +37,20 @@ function buildRemappings(pkg) {
return pkg.deps.map(buildRemappings).concat(remappings).flat()
}

// walk tree and rewrite paths so that all packages with the same hash have the same path
function deduplicate(pkg) {
const mapping = mapHashes(pkg)
const go = p => ({ ...p, path: mapping[p.hash], deps: p.deps.map(go) })
return go(pkg)
}

// walk tree and build a mapping from hash => path
function mapHashes(pkg) {
const go = (mapping, dep) => {
mapping[dep.hash] = dep.path
return dep.deps.reduce(go, mapping)
}
return tree.deps.reduce(go, { [pkg.hash]: pkg.path })
}

// walk tree and rewrite paths so that all packages with the same hash have the same path
function deduplicate(mapping, pkg) {
return {
...pkg,
path: mapping[pkg.hash],
deps: pkg.deps.map(dep => deduplicate(mapping, dep))
}
return pkg.deps.reduce(go, { [pkg.hash]: pkg.path })
}

// strip the leading `.` or `./` from a path
Expand Down

0 comments on commit d80f3c1

Please sign in to comment.