Skip to content

Commit

Permalink
Merge pull request #17252 from storybookjs/17038-16964-16924-fix-comp…
Browse files Browse the repository at this point in the history
…lex-globs

Core: Fix issue with recursive glob with prior special chars
  • Loading branch information
shilman committed Jan 17, 2022
2 parents d6b88e4 + b1e0534 commit 2f83d1b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
10 changes: 10 additions & 0 deletions lib/core-common/src/utils/__tests__/paths.test.ts
Expand Up @@ -12,6 +12,16 @@ describe('paths - normalizeStoryPath()', () => {
expect(normalizeStoryPath(filename)).toEqual(filename);
});

it('returns a path equal to "." unchanged', () => {
const filename = '.';
expect(normalizeStoryPath(filename)).toEqual(filename);
});

it('returns a path equal to ".." unchanged', () => {
const filename = '..';
expect(normalizeStoryPath(filename)).toEqual(filename);
});

it('adds "./" to a normalized relative path', () => {
const filename = path.join('src', 'Comp.story.js');
expect(normalizeStoryPath(filename)).toEqual(`.${path.sep}${filename}`);
Expand Down
15 changes: 15 additions & 0 deletions lib/core-common/src/utils/__tests__/to-require-context.test.ts
Expand Up @@ -238,6 +238,21 @@ const testCases = [
'../src/stories/components/Icon/Icon.mdx',
],
},
// Patterns before the **, see:
// - https://github.com/storybookjs/storybook/issues/17038
// - https://github.com/storybookjs/storybook/issues/16964
// - https://github.com/storybookjs/storybook/issues/16924
{
glob: '../{dir1,dir2}/**/*.stories.js',
recursive: true,
validPaths: [
'../dir1/Icon.stories.js',
'../dir1/nested/Icon.stories.js',
'../dir2/Icon.stories.js',
'../dir2/nested/Icon.stories.js',
],
invalidPaths: ['../dir3/Icon.stories.js', '../dir3/nested/Icon.stories.js'],
},
];

describe('toRequireContext', () => {
Expand Down
4 changes: 2 additions & 2 deletions lib/core-common/src/utils/paths.ts
Expand Up @@ -29,9 +29,9 @@ export const nodePathsToArray = (nodePath: string) =>
.filter(Boolean)
.map((p) => path.resolve('./', p));

const relativePattern = /^\.{1,2}[/\\]/;
const relativePattern = /^\.{1,2}([/\\]|$)/;
/**
* Ensures that a path starts with `./` or `../`
* Ensures that a path starts with `./` or `../`, or is entirely `.` or `..`
*/
export function normalizeStoryPath(filename: string) {
if (relativePattern.test(filename)) return filename;
Expand Down
3 changes: 2 additions & 1 deletion lib/core-common/src/utils/to-require-context.ts
Expand Up @@ -7,9 +7,10 @@ export const toRequireContext = (specifier: NormalizedStoriesSpecifier) => {
// The importPathMatcher is a `./`-prefixed matcher that includes the directory
// For `require.context()` we want the same thing, relative to directory
const match = globToRegexp(`./${files}`);

return {
path: directory,
recursive: !!files.match(/^\*{1,2}\//),
recursive: files.includes('**') || files.split('/').length > 1,
match,
};
};
Expand Down

0 comments on commit 2f83d1b

Please sign in to comment.