diff --git a/packages/core/src/utils/helper.ts b/packages/core/src/utils/helper.ts index 1393533df..b4d433527 100644 --- a/packages/core/src/utils/helper.ts +++ b/packages/core/src/utils/helper.ts @@ -78,7 +78,12 @@ async function calcLongestCommonPath( return null; } - const splitPaths = absPaths.map((p) => p.split(path.posix.sep)); + // we support two cases + // 1. /packages-a/src/index.ts + // 2. D:/packages-a/src/index.ts + const sep = path.posix.sep as '/'; + + const splitPaths = absPaths.map((p) => p.split(sep)); let lcaFragments = splitPaths[0]!; for (let i = 1; i < splitPaths.length; i++) { const currentPath = splitPaths[i]!; @@ -92,7 +97,7 @@ async function calcLongestCommonPath( lcaFragments = lcaFragments.slice(0, j); } - let lca = lcaFragments.length > 0 ? lcaFragments.join(path.sep) : '/'; + let lca = lcaFragments.length > 0 ? lcaFragments.join(sep) : sep; const stats = await fsP.stat(lca); if (stats?.isFile()) { diff --git a/packages/core/tests/lcp.test.ts b/packages/core/tests/lcp.test.ts index 065a0f793..8283538c1 100644 --- a/packages/core/tests/lcp.test.ts +++ b/packages/core/tests/lcp.test.ts @@ -29,11 +29,11 @@ describe('LCP calculate correctly', () => { expect(result).toEqual('/Users/Someone/project-a/src'); } else { const result = await calcLongestCommonPath([ - 'D:\\Users\\Someone\\project-a\\src\\helpers', - 'D:\\Users\\Someone\\project-a\\src', - 'D:\\Users\\Someone\\project-a\\src\\utils', + 'D:/Users/Someone/project-a/src/helpers', + 'D:/Users/Someone/project-a/src', + 'D:/Users/Someone/project-a/src/utils', ]); - expect(result).toEqual('D:\\Users\\Someone\\project-a\\src'); + expect(result).toEqual('D:/Users/Someone/project-a/src'); } }); @@ -49,11 +49,11 @@ describe('LCP calculate correctly', () => { expect(result).toEqual('/Users/Someone/project-monorepo'); } else { const result = await calcLongestCommonPath([ - 'D:\\Users\\Someone\\project-monorepo\\packages-a\\src\\index.ts', - 'D:\\Users\\Someone\\project-monorepo\\packages-util\\src\\index.js', - 'D:\\Users\\Someone\\project-monorepo\\script.js', + 'D:/Users/Someone/project-monorepo/packages-a/src/index.ts', + 'D:/Users/Someone/project-monorepo/packages-util/src/index.js', + 'D:/Users/Someone/project-monorepo/script.js', ]); - expect(result).toEqual('D:\\Users\\Someone\\project-monorepo'); + expect(result).toEqual('D:/Users/Someone/project-monorepo'); } }); @@ -68,9 +68,9 @@ describe('LCP calculate correctly', () => { expect(result).toEqual('/Users/Someone/project/src'); } else { const result = await calcLongestCommonPath([ - 'D:\\Users\\Someone\\project\\src\\index.js', + 'D:/Users/Someone/project/src/index.js', ]); - expect(result).toEqual('D:\\Users\\Someone\\project\\src'); + expect(result).toEqual('D:/Users/Someone/project/src'); } }); });