Merge pull request #21461 from storybookjs/norbert/fix-finding-project-root

Core: Fix project root detection in yarn PnP without git
This commit is contained in:
Michael Shilman 2023-03-09 17:56:03 +08:00 committed by GitHub
commit f1c17fc368
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 2 deletions

View File

@ -1,5 +1,8 @@
import path from 'path';
import { normalizeStoryPath } from '../paths';
import findUp from 'find-up';
import { normalizeStoryPath, getProjectRoot } from '../paths';
jest.mock('find-up');
describe('paths - normalizeStoryPath()', () => {
it('returns a path starting with "./" unchanged', () => {
@ -37,3 +40,31 @@ describe('paths - normalizeStoryPath()', () => {
expect(normalizeStoryPath(filename)).toEqual(`.${path.sep}${filename}`);
});
});
describe('getProjectRoot', () => {
const mockedFindUp = findUp as jest.Mocked<typeof findUp>;
it('should return the root directory containing a .git directory', () => {
mockedFindUp.sync.mockImplementation((name) =>
name === ('.git' as any) ? '/path/to/root' : undefined
);
expect(getProjectRoot()).toBe('/path/to');
});
it('should return the root directory containing a .svn directory if there is no .git directory', () => {
mockedFindUp.sync.mockImplementation((name) =>
name === ('.svn' as any) ? '/path/to/root' : undefined
);
expect(getProjectRoot()).toBe('/path/to');
});
it('should return the root directory containing a .yarn directory if there is no .git or .svn directory', () => {
mockedFindUp.sync.mockImplementation((name) =>
name === ('.yarn' as any) ? '/path/to/root' : undefined
);
expect(getProjectRoot()).toBe('/path/to');
});
});

View File

@ -6,7 +6,7 @@ export const getProjectRoot = () => {
try {
const found = findUp.sync('.git', { type: 'directory' });
if (found) {
result = result || path.join(found, '..');
result = path.join(found, '..');
}
} catch (e) {
//
@ -19,6 +19,14 @@ export const getProjectRoot = () => {
} catch (e) {
//
}
try {
const found = findUp.sync('.yarn', { type: 'directory' });
if (found) {
result = result || path.join(found, '..');
}
} catch (e) {
//
}
try {
result = result || __dirname.split('node_modules')[0];
} catch (e) {