Skip to content

Commit

Permalink
Merge pull request #21461 from storybookjs/norbert/fix-finding-projec…
Browse files Browse the repository at this point in the history
…t-root

Core: Fix project root detection in yarn PnP without git
  • Loading branch information
shilman committed Mar 9, 2023
2 parents 990a42d + 4eef0a1 commit f1c17fc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
33 changes: 32 additions & 1 deletion code/lib/core-common/src/utils/__tests__/paths.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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');
});
});
10 changes: 9 additions & 1 deletion code/lib/core-common/src/utils/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
//
Expand All @@ -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) {
Expand Down

0 comments on commit f1c17fc

Please sign in to comment.