mirror of
https://github.com/storybookjs/storybook.git
synced 2025-03-16 05:03:11 +08:00
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:
commit
f1c17fc368
@ -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');
|
||||
});
|
||||
});
|
||||
|
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user