Skip to content

Commit d80f3c1

Browse files
committed
dapp: remappings: minor refactoring
1 parent 64652e9 commit d80f3c1

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

src/dapp/libexec/dapp/dapp-remappings

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#!/usr/bin/env node
22
const PROGRAM_NAME = process.argv[1].replace(/.*\//, "")
33

4-
const tree = buildDependencyTree(".")
5-
console.log(buildRemappings(deduplicate(mapHashes(tree), tree)).join("\n"))
4+
console.log(buildRemappings(deduplicate(buildDependencyTree("."))).join("\n"))
65

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

40+
// walk tree and rewrite paths so that all packages with the same hash have the same path
41+
function deduplicate(pkg) {
42+
const mapping = mapHashes(pkg)
43+
const go = p => ({ ...p, path: mapping[p.hash], deps: p.deps.map(go) })
44+
return go(pkg)
45+
}
46+
4147
// walk tree and build a mapping from hash => path
4248
function mapHashes(pkg) {
4349
const go = (mapping, dep) => {
4450
mapping[dep.hash] = dep.path
4551
return dep.deps.reduce(go, mapping)
4652
}
47-
return tree.deps.reduce(go, { [pkg.hash]: pkg.path })
48-
}
49-
50-
// walk tree and rewrite paths so that all packages with the same hash have the same path
51-
function deduplicate(mapping, pkg) {
52-
return {
53-
...pkg,
54-
path: mapping[pkg.hash],
55-
deps: pkg.deps.map(dep => deduplicate(mapping, dep))
56-
}
53+
return pkg.deps.reduce(go, { [pkg.hash]: pkg.path })
5754
}
5855

5956
// strip the leading `.` or `./` from a path

0 commit comments

Comments
 (0)