Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Fix negated glob support #17328

Merged
merged 2 commits into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/core-common/src/utils/__tests__/normalize-stories.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,27 @@ describe('normalizeStoriesEntry', () => {
}
`);
});

it('globs with negation', () => {
const specifier = normalizeStoriesEntry('../!(negation)/*.stories.mdx', options);
expect(specifier).toMatchInlineSnapshot(`
{
"titlePrefix": "",
"directory": ".",
"files": "!(negation)/*.stories.mdx",
"importPathMatcher": {}
}
`);

expect(specifier.importPathMatcher).toMatchPaths([
'./path/file.stories.mdx',
'./second-path/file.stories.mdx',
]);
expect(specifier.importPathMatcher).not.toMatchPaths([
'./path/file.stories.js',
'./path/to/file.stories.mdx',
'./file.stories.mdx',
'../file.stories.mdx',
]);
});
});
40 changes: 17 additions & 23 deletions lib/core-common/src/utils/normalize-stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,35 +59,29 @@ export const normalizeStoriesEntry = (
let specifierWithoutMatcher: Omit<NormalizedStoriesSpecifier, 'importPathMatcher'>;

if (typeof entry === 'string') {
if (!entry.includes('*')) {
if (isDirectory(configDir, entry)) {
specifierWithoutMatcher = {
titlePrefix: DEFAULT_TITLE_PREFIX,
directory: entry,
files: DEFAULT_FILES,
};
} else {
specifierWithoutMatcher = {
titlePrefix: DEFAULT_TITLE_PREFIX,
directory: path.dirname(entry),
files: path.basename(entry),
};
}
} else {
const fixedEntry = detectBadGlob(entry);
const globResult = scan(fixedEntry);
const directory = globResult.isGlob
? globResult.prefix + globResult.base
: path.dirname(fixedEntry);
const filesFallback =
directory !== '.' ? fixedEntry.substr(directory.length + 1) : fixedEntry;
const files = globResult.isGlob ? globResult.glob : filesFallback;
const fixedEntry = detectBadGlob(entry);
const globResult = scan(fixedEntry);
if (globResult.isGlob) {
const directory = globResult.prefix + globResult.base;
const files = globResult.glob;

specifierWithoutMatcher = {
titlePrefix: DEFAULT_TITLE_PREFIX,
directory,
files,
};
} else if (isDirectory(configDir, entry)) {
specifierWithoutMatcher = {
titlePrefix: DEFAULT_TITLE_PREFIX,
directory: entry,
files: DEFAULT_FILES,
};
} else {
specifierWithoutMatcher = {
titlePrefix: DEFAULT_TITLE_PREFIX,
directory: path.dirname(entry),
files: path.basename(entry),
};
}
} else {
specifierWithoutMatcher = {
Expand Down