From 56444ef6a652da5ad490f238a015234613dff81f Mon Sep 17 00:00:00 2001 From: Christian Lentfort <1284808+clentfort@users.noreply.github.com> Date: Tue, 25 Jan 2022 10:28:13 +0000 Subject: [PATCH] fix(core-common): support negated globs # Summary: Support negated globs like `../!(negation)/*.stories.mdx` in storybook config. **Related issues:** Closes #16964 --- .../utils/__tests__/normalize-stories.test.ts | 23 +++++++++++ .../src/utils/normalize-stories.ts | 40 ++++++++----------- 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/lib/core-common/src/utils/__tests__/normalize-stories.test.ts b/lib/core-common/src/utils/__tests__/normalize-stories.test.ts index 420e0a89ef39..ffa4b6a477a3 100644 --- a/lib/core-common/src/utils/__tests__/normalize-stories.test.ts +++ b/lib/core-common/src/utils/__tests__/normalize-stories.test.ts @@ -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', + ]); + }); }); diff --git a/lib/core-common/src/utils/normalize-stories.ts b/lib/core-common/src/utils/normalize-stories.ts index 69bfb552a44e..3f1e66b76862 100644 --- a/lib/core-common/src/utils/normalize-stories.ts +++ b/lib/core-common/src/utils/normalize-stories.ts @@ -59,35 +59,29 @@ export const normalizeStoriesEntry = ( let specifierWithoutMatcher: Omit; 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 = {