Skip to content

Commit 9107713

Browse files
krzkaczoralexdupre
andauthored
Fix the lowestCommonPath function, #772 (#858)
Co-authored-by: Alex Dupre <[email protected]>
1 parent bc3080b commit 9107713

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

.changeset/ten-pants-learn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'typechain': patch
3+
---
4+
5+
Fix the detection of inputs root in some specific scenarios
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
export function lowestCommonPath(paths: string[]) {
22
const pathParts = paths.map((path) => path.split(/[\\/]/))
3-
const commonParts = pathParts[0].filter((part, index) => pathParts.every((parts) => parts[index] === part))
3+
const commonParts = [] as string[]
4+
const maxParts = Math.min.apply(
5+
null,
6+
pathParts.map((p) => p.length),
7+
)
8+
for (let i = 0; i < maxParts; i++) {
9+
const part = pathParts[0][i]
10+
if (pathParts.slice(1).every((otherPath) => otherPath[i] === part)) {
11+
commonParts.push(part)
12+
} else {
13+
break
14+
}
15+
}
416
return commonParts.join('/')
517
}

packages/typechain/test/utils/files/lowestCommonPath.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ describe(lowestCommonPath.name, () => {
1818
expect(actual).toEqual('/TypeChain/contracts/compiled')
1919
})
2020

21+
it('stops at first different sub-path', () => {
22+
const paths = [
23+
'/TypeChain/contracts/v0.6.4/interfaces/Payable.abi',
24+
'/TypeChain/contracts/v0.8.9/interfaces/Rarity.abi',
25+
]
26+
27+
const actual = lowestCommonPath(paths)
28+
expect(actual).toEqual('/TypeChain/contracts')
29+
})
30+
2131
it('works for Windows paths', () => {
2232
const paths = [
2333
'D:/workspace/TypeChain/packages/hardhat/test/fixture-projects/hardhat-project/artifacts/contracts/EdgeCases.sol/EdgeCases.json',

0 commit comments

Comments
 (0)