Skip to content

Commit

Permalink
Fix the lowestCommonPath function, #772 (#858)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Dupre <ale@FreeBSD.org>
  • Loading branch information
krzkaczor and alexdupre authored Jul 24, 2023
1 parent bc3080b commit 9107713
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/ten-pants-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'typechain': patch
---

Fix the detection of inputs root in some specific scenarios
14 changes: 13 additions & 1 deletion packages/typechain/src/utils/files/lowestCommonPath.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
export function lowestCommonPath(paths: string[]) {
const pathParts = paths.map((path) => path.split(/[\\/]/))
const commonParts = pathParts[0].filter((part, index) => pathParts.every((parts) => parts[index] === part))
const commonParts = [] as string[]
const maxParts = Math.min.apply(
null,
pathParts.map((p) => p.length),
)
for (let i = 0; i < maxParts; i++) {
const part = pathParts[0][i]
if (pathParts.slice(1).every((otherPath) => otherPath[i] === part)) {
commonParts.push(part)
} else {
break
}
}
return commonParts.join('/')
}
10 changes: 10 additions & 0 deletions packages/typechain/test/utils/files/lowestCommonPath.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ describe(lowestCommonPath.name, () => {
expect(actual).toEqual('/TypeChain/contracts/compiled')
})

it('stops at first different sub-path', () => {
const paths = [
'/TypeChain/contracts/v0.6.4/interfaces/Payable.abi',
'/TypeChain/contracts/v0.8.9/interfaces/Rarity.abi',
]

const actual = lowestCommonPath(paths)
expect(actual).toEqual('/TypeChain/contracts')
})

it('works for Windows paths', () => {
const paths = [
'D:/workspace/TypeChain/packages/hardhat/test/fixture-projects/hardhat-project/artifacts/contracts/EdgeCases.sol/EdgeCases.json',
Expand Down

0 comments on commit 9107713

Please sign in to comment.