Skip to content

Commit 573ad45

Browse files
authored
Merge pull request #7176 from Shopify/04-02-remove-commondir-dep
Remove commondir dependency, inline as commonParentDirectory()
2 parents d108925 + 8e7cc63 commit 573ad45

4 files changed

Lines changed: 53 additions & 21 deletions

File tree

packages/cli-kit/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@
121121
"chalk": "5.4.1",
122122
"change-case": "4.1.2",
123123
"color-json": "3.0.5",
124-
"commondir": "1.0.1",
125124
"conf": "11.0.2",
126125
"deepmerge": "4.3.1",
127126
"dotenv": "16.4.7",
@@ -165,7 +164,6 @@
165164
"zod": "3.24.4"
166165
},
167166
"devDependencies": {
168-
"@types/commondir": "^1.0.0",
169167
"@types/diff": "^5.2.3",
170168
"@types/fs-extra": "9.0.13",
171169
"@types/gradient-string": "^1.1.2",

packages/cli-kit/src/public/node/path.test.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {relativizePath, normalizePath, cwd, sniffForPath} from './path.js'
1+
import {relativizePath, normalizePath, cwd, sniffForPath, commonParentDirectory} from './path.js'
22
import {describe, test, expect} from 'vitest'
33

44
describe('relativize', () => {
@@ -25,6 +25,40 @@ describe('cwd', () => {
2525
})
2626
})
2727

28+
describe('commonParentDirectory', () => {
29+
// Parity tests with the original 'commondir' npm package (v1.0.1)
30+
test('finds common parent for paths sharing a prefix', () => {
31+
expect(commonParentDirectory('/foo', '/foo/bar')).toBe('/foo')
32+
expect(commonParentDirectory('/foo/bar', '/foo//bar/baz')).toBe('/foo/bar')
33+
})
34+
35+
test('finds deepest common ancestor', () => {
36+
expect(commonParentDirectory('/a/b/c', '/a/b')).toBe('/a/b')
37+
expect(commonParentDirectory('/a/b', '/a/b/c/d/e')).toBe('/a/b')
38+
})
39+
40+
test('returns root when paths diverge at top level', () => {
41+
expect(commonParentDirectory('/x/y/z/w', '/xy/z')).toBe('/')
42+
})
43+
44+
test('handles Windows-style paths', () => {
45+
expect(commonParentDirectory('X:\\foo', 'X:\\\\foo\\bar')).toBe('X:/foo')
46+
expect(commonParentDirectory('X:\\a\\b\\c', 'X:\\a\\b')).toBe('X:/a/b')
47+
})
48+
49+
test('returns root for completely divergent Windows paths', () => {
50+
expect(commonParentDirectory('X:\\x\\y\\z\\w', '\\\\xy\\z')).toBe('/')
51+
})
52+
53+
test('returns root for single-component paths', () => {
54+
expect(commonParentDirectory('/', '/')).toBe('/')
55+
})
56+
57+
test('handles identical paths', () => {
58+
expect(commonParentDirectory('/a/b/c', '/a/b/c')).toBe('/a/b/c')
59+
})
60+
})
61+
2862
describe('sniffForPath', () => {
2963
test('returns the path if provided', () => {
3064
// Given

packages/cli-kit/src/public/node/path.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import commondir from 'commondir'
21
import {
32
relative,
43
dirname as patheDirname,
@@ -106,6 +105,23 @@ export function parsePath(path: string): {root: string; dir: string; base: strin
106105
return parse(path)
107106
}
108107

108+
/**
109+
* Returns the longest common parent directory of two absolute paths.
110+
*
111+
* @param first - First absolute path.
112+
* @param second - Second absolute path.
113+
* @returns The common parent directory, or '/' if they share only the root.
114+
*/
115+
export function commonParentDirectory(first: string, second: string): string {
116+
const firstParts = first.split(/\/+|\\+/)
117+
const secondParts = second.split(/\/+|\\+/)
118+
let i = 0
119+
while (i < firstParts.length && i < secondParts.length && firstParts[i] === secondParts[i]) {
120+
i++
121+
}
122+
return i > 1 ? firstParts.slice(0, i).join('/') : '/'
123+
}
124+
109125
/**
110126
* Given an absolute filesystem path, it makes it relative to
111127
* the current working directory. This is useful when logging paths
@@ -117,7 +133,7 @@ export function parsePath(path: string): {root: string; dir: string; base: strin
117133
* @returns Relativized path.
118134
*/
119135
export function relativizePath(path: string, dir: string = cwd()): string {
120-
const result = commondir([path, dir])
136+
const result = commonParentDirectory(path, dir)
121137
const relativePath = relative(dir, path)
122138
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
123139
// @ts-ignore

pnpm-lock.yaml

Lines changed: 0 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)