Skip to content

Commit

Permalink
Merge pull request #17328 from clentfort/fix/16964-negated-globs
Browse files Browse the repository at this point in the history
Core: Fix negated glob support
  • Loading branch information
shilman committed Jan 28, 2022
2 parents 208710f + 5d476f0 commit 7669653
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
23 changes: 23 additions & 0 deletions lib/core-common/src/utils/__tests__/normalize-stories.test.ts
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
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

0 comments on commit 7669653

Please sign in to comment.