Merge pull request #32987 from backstage/rugvip/cli-common-path-cleanup

cli-common: clean up path utils follow-up
This commit is contained in:
Patrik Oldsberg
2026-02-25 14:27:29 +01:00
committed by GitHub
3 changed files with 27 additions and 11 deletions
+5 -5
View File
@@ -16,18 +16,18 @@
/* eslint-disable no-restricted-syntax */
import { resolve as resolvePath } from 'node:path';
import { findPaths, findRootPath, findOwnDir, findOwnRootDir } from './paths';
import { findPaths, findRootPath, findOwnRootDir, findOwnPaths } from './paths';
describe('paths', () => {
afterEach(() => {
jest.restoreAllMocks();
});
it('findOwnDir and findOwnRootDir should find owns paths', () => {
const dir = findOwnDir(__dirname);
const root = findOwnRootDir(dir);
it('findOwnPaths and findOwnRootDir should find own paths', () => {
const own = findOwnPaths(__dirname);
const root = findOwnRootDir(own.dir);
expect(dir).toBe(resolvePath(__dirname, '..'));
expect(own.dir).toBe(resolvePath(__dirname, '..'));
expect(root).toBe(resolvePath(__dirname, '../../..'));
});
+17 -6
View File
@@ -119,7 +119,23 @@ export function findOwnRootDir(ownDir: string) {
);
}
return resolvePath(ownDir, '../..');
const rootDir = findRootPath(ownDir, pkgJsonPath => {
try {
const content = fs.readFileSync(pkgJsonPath, 'utf8');
const data = JSON.parse(content);
return Boolean(data.workspaces);
} catch (error) {
throw new Error(
`Failed to read package.json at '${pkgJsonPath}', ${error}`,
);
}
});
if (!rootDir) {
throw new Error(`No monorepo root found when searching from '${ownDir}'`);
}
return rootDir;
}
// Hierarchical directory cache shared across all OwnPathsImpl instances.
@@ -199,11 +215,6 @@ class OwnPathsImpl implements OwnPaths {
};
}
// Finds the root of a given package
export function findOwnDir(searchDir: string) {
return OwnPathsImpl.findDir(searchDir);
}
// Used by the test utility in testUtils.ts to override targetPaths
export let targetPathsOverride: TargetPaths | undefined;