Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions packages/cli-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@
"chalk": "5.4.1",
"change-case": "4.1.2",
"color-json": "3.0.5",
"commondir": "1.0.1",
"conf": "11.0.2",
"deepmerge": "4.3.1",
"dotenv": "16.4.7",
Expand Down Expand Up @@ -165,7 +164,6 @@
"zod": "3.24.4"
},
"devDependencies": {
"@types/commondir": "^1.0.0",
"@types/diff": "^5.2.3",
"@types/fs-extra": "9.0.13",
"@types/gradient-string": "^1.1.2",
Expand Down
36 changes: 35 additions & 1 deletion packages/cli-kit/src/public/node/path.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {relativizePath, normalizePath, cwd, sniffForPath} from './path.js'
import {relativizePath, normalizePath, cwd, sniffForPath, commonParentDirectory} from './path.js'
import {describe, test, expect} from 'vitest'

describe('relativize', () => {
Expand All @@ -25,6 +25,40 @@ describe('cwd', () => {
})
})

describe('commonParentDirectory', () => {
// Parity tests with the original 'commondir' npm package (v1.0.1)
test('finds common parent for paths sharing a prefix', () => {
expect(commonParentDirectory('/foo', '/foo/bar')).toBe('/foo')
expect(commonParentDirectory('/foo/bar', '/foo//bar/baz')).toBe('/foo/bar')
})

test('finds deepest common ancestor', () => {
expect(commonParentDirectory('/a/b/c', '/a/b')).toBe('/a/b')
expect(commonParentDirectory('/a/b', '/a/b/c/d/e')).toBe('/a/b')
})

test('returns root when paths diverge at top level', () => {
expect(commonParentDirectory('/x/y/z/w', '/xy/z')).toBe('/')
})

test('handles Windows-style paths', () => {
expect(commonParentDirectory('X:\\foo', 'X:\\\\foo\\bar')).toBe('X:/foo')
expect(commonParentDirectory('X:\\a\\b\\c', 'X:\\a\\b')).toBe('X:/a/b')
})

test('returns root for completely divergent Windows paths', () => {
expect(commonParentDirectory('X:\\x\\y\\z\\w', '\\\\xy\\z')).toBe('/')
})

test('returns root for single-component paths', () => {
expect(commonParentDirectory('/', '/')).toBe('/')
})

test('handles identical paths', () => {
expect(commonParentDirectory('/a/b/c', '/a/b/c')).toBe('/a/b/c')
})
})

describe('sniffForPath', () => {
test('returns the path if provided', () => {
// Given
Expand Down
20 changes: 18 additions & 2 deletions packages/cli-kit/src/public/node/path.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import commondir from 'commondir'
import {
relative,
dirname as patheDirname,
Expand Down Expand Up @@ -106,6 +105,23 @@ export function parsePath(path: string): {root: string; dir: string; base: strin
return parse(path)
}

/**
* Returns the longest common parent directory of two absolute paths.
*
* @param first - First absolute path.
* @param second - Second absolute path.
* @returns The common parent directory, or '/' if they share only the root.
*/
export function commonParentDirectory(first: string, second: string): string {
const firstParts = first.split(/\/+|\\+/)
const secondParts = second.split(/\/+|\\+/)
let i = 0
while (i < firstParts.length && i < secondParts.length && firstParts[i] === secondParts[i]) {
i++
}
return i > 1 ? firstParts.slice(0, i).join('/') : '/'
}

/**
* Given an absolute filesystem path, it makes it relative to
* the current working directory. This is useful when logging paths
Expand All @@ -117,7 +133,7 @@ export function parsePath(path: string): {root: string; dir: string; base: strin
* @returns Relativized path.
*/
export function relativizePath(path: string, dir: string = cwd()): string {
const result = commondir([path, dir])
const result = commonParentDirectory(path, dir)
const relativePath = relative(dir, path)
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Expand Down
16 changes: 0 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading