|
1 | 1 | #!/usr/bin/env node |
2 | 2 | const PROGRAM_NAME = process.argv[1].replace(/.*\//, "") |
3 | 3 |
|
4 | | -const tree = buildDependencyTree(".") |
5 | | -console.log(buildRemappings(deduplicate(mapHashes(tree), tree)).join("\n")) |
| 4 | +console.log(buildRemappings(deduplicate(buildDependencyTree("."))).join("\n")) |
6 | 5 |
|
7 | 6 | // builds a in memory representation of the projects dependency tree |
8 | 7 | // |
@@ -38,22 +37,20 @@ function buildRemappings(pkg) { |
38 | 37 | return pkg.deps.map(buildRemappings).concat(remappings).flat() |
39 | 38 | } |
40 | 39 |
|
| 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 | + |
41 | 47 | // walk tree and build a mapping from hash => path |
42 | 48 | function mapHashes(pkg) { |
43 | 49 | const go = (mapping, dep) => { |
44 | 50 | mapping[dep.hash] = dep.path |
45 | 51 | return dep.deps.reduce(go, mapping) |
46 | 52 | } |
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 }) |
57 | 54 | } |
58 | 55 |
|
59 | 56 | // strip the leading `.` or `./` from a path |
|
0 commit comments