From c825bd6560cd717455784ac8c479a2f111ff7e75 Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Mon, 20 Nov 2023 13:09:08 +0100 Subject: [PATCH 1/6] rename property & add tests --- .../src/utils/normalize-stories.ts | 8 +- .../src/presets/common-override-preset.ts | 57 +--- .../__tests__/remove-mdx-stories.test.ts | 274 ++++++++++++++++++ .../src/utils/remove-mdx-entries.ts | 54 ++++ 4 files changed, 335 insertions(+), 58 deletions(-) create mode 100644 code/lib/core-server/src/utils/__tests__/remove-mdx-stories.test.ts create mode 100644 code/lib/core-server/src/utils/remove-mdx-entries.ts diff --git a/code/lib/core-common/src/utils/normalize-stories.ts b/code/lib/core-common/src/utils/normalize-stories.ts index 3beabeae4c8c..1a69fb1ce689 100644 --- a/code/lib/core-common/src/utils/normalize-stories.ts +++ b/code/lib/core-common/src/utils/normalize-stories.ts @@ -34,7 +34,7 @@ export const getDirectoryFromWorkingDir = ({ export const normalizeStoriesEntry = ( entry: StoriesEntry, - { configDir, workingDir, default_files_pattern = DEFAULT_FILES_PATTERN }: NormalizeOptions + { configDir, workingDir, defaultFilesPattern = DEFAULT_FILES_PATTERN }: NormalizeOptions ): NormalizedStoriesSpecifier => { let specifierWithoutMatcher: Omit; @@ -53,7 +53,7 @@ export const normalizeStoriesEntry = ( specifierWithoutMatcher = { titlePrefix: DEFAULT_TITLE_PREFIX, directory: entry, - files: default_files_pattern, + files: defaultFilesPattern, }; } else { specifierWithoutMatcher = { @@ -65,7 +65,7 @@ export const normalizeStoriesEntry = ( } else { specifierWithoutMatcher = { titlePrefix: DEFAULT_TITLE_PREFIX, - files: default_files_pattern, + files: defaultFilesPattern, ...entry, }; } @@ -99,7 +99,7 @@ export const normalizeStoriesEntry = ( interface NormalizeOptions { configDir: string; workingDir: string; - default_files_pattern?: string; + defaultFilesPattern?: string; } export const normalizeStories = (entries: StoriesEntry[], options: NormalizeOptions) => { diff --git a/code/lib/core-server/src/presets/common-override-preset.ts b/code/lib/core-server/src/presets/common-override-preset.ts index d92dead5d8f4..8cc740aea7d7 100644 --- a/code/lib/core-server/src/presets/common-override-preset.ts +++ b/code/lib/core-server/src/presets/common-override-preset.ts @@ -1,14 +1,5 @@ -import type { - Options, - PresetProperty, - StoriesEntry, - StorybookConfig, - TestBuildFlags, -} from '@storybook/types'; -import { normalizeStories, commonGlobOptions } from '@storybook/core-common'; -import { isAbsolute, join, relative } from 'path'; -import slash from 'slash'; -import { glob } from 'glob'; +import type { Options, PresetProperty, StorybookConfig, TestBuildFlags } from '@storybook/types'; +import { removeMDXEntries } from '../utils/remove-mdx-entries'; export const framework: PresetProperty<'framework', StorybookConfig> = async (config) => { // This will get called with the values from the user's main config, but before @@ -25,49 +16,7 @@ export const framework: PresetProperty<'framework', StorybookConfig> = async (co export const stories: PresetProperty<'stories', StorybookConfig> = async (entries, options) => { if (options?.build?.test?.disableMDXEntries) { - const list = normalizeStories(entries, { - configDir: options.configDir, - workingDir: options.configDir, - default_files_pattern: '**/*.@(stories.@(js|jsx|mjs|ts|tsx))', - }); - const result = ( - await Promise.all( - list.map(async ({ directory, files, titlePrefix }) => { - const pattern = join(directory, files); - const absolutePattern = isAbsolute(pattern) ? pattern : join(options.configDir, pattern); - const absoluteDirectory = isAbsolute(directory) - ? directory - : join(options.configDir, directory); - - return { - files: ( - await glob(slash(absolutePattern), { - ...commonGlobOptions(absolutePattern), - follow: true, - }) - ).map((f) => relative(absoluteDirectory, f)), - directory, - titlePrefix, - }; - }) - ) - ).flatMap((expanded, i) => { - const filteredEntries = expanded.files.filter((s) => !s.endsWith('.mdx')); - // only return the filtered entries when there is something to filter - // as webpack is faster with unexpanded globs - let items = []; - if (filteredEntries.length < expanded.files.length) { - items = filteredEntries.map((k) => ({ - ...expanded, - files: `**/${k}`, - })); - } else { - items = [list[i]]; - } - - return items; - }); - return result; + return removeMDXEntries(entries, options); } return entries; }; diff --git a/code/lib/core-server/src/utils/__tests__/remove-mdx-stories.test.ts b/code/lib/core-server/src/utils/__tests__/remove-mdx-stories.test.ts new file mode 100644 index 000000000000..9c40041c0dc1 --- /dev/null +++ b/code/lib/core-server/src/utils/__tests__/remove-mdx-stories.test.ts @@ -0,0 +1,274 @@ +import { glob as globlOriginal } from 'glob'; +import { type StoriesEntry } from '@storybook/types'; +import { normalizeStoriesEntry } from '@storybook/core-common'; +import { join } from 'path'; +import { removeMDXEntries } from '../remove-mdx-entries'; + +const glob = globlOriginal as jest.MockedFunction; + +jest.mock('glob', () => ({ glob: jest.fn() })); + +const createList = (list: { entry: StoriesEntry; result: string[] }[], configDir: string) => { + return list.reduce>( + (acc, { entry, result }) => { + const { directory, files } = normalizeStoriesEntry(entry, { + configDir, + workingDir: '/', + }); + acc[join('/', directory, files)] = { result, entry }; + return acc; + }, + {} + ); +}; + +const createGlobMock = (input: ReturnType) => { + return async (k: string | string[]) => { + if (Array.isArray(k)) { + throw new Error('do not pass an array to glob during tests'); + } + const result = input[k].result || []; + return result; + }; +}; + +test('empty', async () => { + const configDir = '/configDir/'; + const list = createList([], configDir); + glob.mockImplementation(createGlobMock(list)); + + await expect(() => removeMDXEntries(Object.keys(list), { configDir })).rejects + .toThrowErrorMatchingInlineSnapshot(` + "Storybook could not index your stories. + Your main configuration somehow does not contain a 'stories' field, or it resolved to an empty array. + + Please check your main configuration file and make sure it exports a 'stories' field that is not an empty array. + + More info: https://storybook.js.org/docs/react/faq#can-i-have-a-storybook-with-no-local-stories + " + `); +}); + +test('minimal', async () => { + const configDir = '/configDir/'; + const list = createList([{ entry: '*.js', result: [] }], configDir); + glob.mockImplementation(createGlobMock(list)); + + await expect( + removeMDXEntries( + Object.values(list).map((e) => e.entry), + { configDir } + ) + ).resolves.toMatchInlineSnapshot(` + Array [ + Object { + "directory": ".", + "files": "*.js", + "importPathMatcher": /\\^\\\\\\.\\[\\\\\\\\/\\]\\(\\?:\\(\\?!\\\\\\.\\)\\(\\?=\\.\\)\\[\\^/\\]\\*\\?\\\\\\.js\\)\\$/, + "titlePrefix": "", + }, + ] + `); +}); + +test('multiple', async () => { + const configDir = '/configDir/'; + const list = createList( + [ + { entry: '*.ts', result: [] }, + { entry: '*.js', result: [] }, + ], + configDir + ); + glob.mockImplementation(createGlobMock(list)); + + await expect( + removeMDXEntries( + Object.values(list).map((e) => e.entry), + { configDir } + ) + ).resolves.toMatchInlineSnapshot(` + Array [ + Object { + "directory": ".", + "files": "*.ts", + "importPathMatcher": /\\^\\\\\\.\\[\\\\\\\\/\\]\\(\\?:\\(\\?!\\\\\\.\\)\\(\\?=\\.\\)\\[\\^/\\]\\*\\?\\\\\\.ts\\)\\$/, + "titlePrefix": "", + }, + Object { + "directory": ".", + "files": "*.js", + "importPathMatcher": /\\^\\\\\\.\\[\\\\\\\\/\\]\\(\\?:\\(\\?!\\\\\\.\\)\\(\\?=\\.\\)\\[\\^/\\]\\*\\?\\\\\\.js\\)\\$/, + "titlePrefix": "", + }, + ] + `); +}); + +test('mdx but not matching any files', async () => { + const configDir = '/configDir/'; + const list = createList( + [ + { entry: '*.mdx', result: [] }, + { entry: '*.js', result: [] }, + ], + configDir + ); + glob.mockImplementation(createGlobMock(list)); + + await expect( + removeMDXEntries( + Object.values(list).map((e) => e.entry), + { configDir } + ) + ).resolves.toMatchInlineSnapshot(` + Array [ + Object { + "directory": ".", + "files": "*.mdx", + "importPathMatcher": /\\^\\\\\\.\\[\\\\\\\\/\\]\\(\\?:\\(\\?!\\\\\\.\\)\\(\\?=\\.\\)\\[\\^/\\]\\*\\?\\\\\\.mdx\\)\\$/, + "titlePrefix": "", + }, + Object { + "directory": ".", + "files": "*.js", + "importPathMatcher": /\\^\\\\\\.\\[\\\\\\\\/\\]\\(\\?:\\(\\?!\\\\\\.\\)\\(\\?=\\.\\)\\[\\^/\\]\\*\\?\\\\\\.js\\)\\$/, + "titlePrefix": "", + }, + ] + `); +}); + +test('removes entries that only yield mdx files', async () => { + const configDir = '/configDir/'; + const list = createList( + [ + { entry: '*.mdx', result: ['/configDir/my-file.mdx'] }, + { entry: '*.js', result: [] }, + ], + configDir + ); + glob.mockImplementation(createGlobMock(list)); + + await expect( + removeMDXEntries( + Object.values(list).map((e) => e.entry), + { configDir } + ) + ).resolves.toMatchInlineSnapshot(` + Array [ + Object { + "directory": ".", + "files": "*.js", + "importPathMatcher": /\\^\\\\\\.\\[\\\\\\\\/\\]\\(\\?:\\(\\?!\\\\\\.\\)\\(\\?=\\.\\)\\[\\^/\\]\\*\\?\\\\\\.js\\)\\$/, + "titlePrefix": "", + }, + ] + `); +}); + +test('expands entries that only yield mixed files', async () => { + const configDir = '/configDir/'; + const list = createList( + [ + { entry: '*.@(mdx|ts)', result: ['/configDir/my-file.mdx', '/configDir/my-file.ts'] }, + { entry: '*.js', result: [] }, + ], + configDir + ); + glob.mockImplementation(createGlobMock(list)); + + await expect( + removeMDXEntries( + Object.values(list).map((e) => e.entry), + { configDir } + ) + ).resolves.toMatchInlineSnapshot(` + Array [ + Object { + "directory": ".", + "files": "**/my-file.ts", + "titlePrefix": "", + }, + Object { + "directory": ".", + "files": "*.js", + "importPathMatcher": /\\^\\\\\\.\\[\\\\\\\\/\\]\\(\\?:\\(\\?!\\\\\\.\\)\\(\\?=\\.\\)\\[\\^/\\]\\*\\?\\\\\\.js\\)\\$/, + "titlePrefix": "", + }, + ] + `); +}); + +test('passes titlePrefix', async () => { + const configDir = '/configDir/'; + const list = createList( + [ + { + entry: { files: '*.@(mdx|ts)', directory: '.', titlePrefix: 'foo' }, + result: ['/configDir/my-file.mdx', '/configDir/my-file.ts'], + }, + ], + configDir + ); + glob.mockImplementation(createGlobMock(list)); + + await expect( + removeMDXEntries( + Object.values(list).map((e) => e.entry), + { configDir } + ) + ).resolves.toMatchInlineSnapshot(` + Array [ + Object { + "directory": ".", + "files": "**/my-file.ts", + "titlePrefix": "foo", + }, + ] + `); +}); + +test('expands to multiple entries', async () => { + const configDir = '/configDir/'; + const list = createList( + [ + { + entry: { files: '*.@(mdx|ts)', directory: '.', titlePrefix: 'foo' }, + result: [ + '/configDir/my-file.mdx', + '/configDir/my-file1.ts', + '/configDir/my-file2.ts', + '/configDir/my-file3.ts', + ], + }, + ], + configDir + ); + glob.mockImplementation(createGlobMock(list)); + + await expect( + removeMDXEntries( + Object.values(list).map((e) => e.entry), + { configDir } + ) + ).resolves.toMatchInlineSnapshot(` + Array [ + Object { + "directory": ".", + "files": "**/my-file1.ts", + "titlePrefix": "foo", + }, + Object { + "directory": ".", + "files": "**/my-file2.ts", + "titlePrefix": "foo", + }, + Object { + "directory": ".", + "files": "**/my-file3.ts", + "titlePrefix": "foo", + }, + ] + `); +}); diff --git a/code/lib/core-server/src/utils/remove-mdx-entries.ts b/code/lib/core-server/src/utils/remove-mdx-entries.ts new file mode 100644 index 000000000000..0efc7923baad --- /dev/null +++ b/code/lib/core-server/src/utils/remove-mdx-entries.ts @@ -0,0 +1,54 @@ +import type { Options, StoriesEntry } from '@storybook/types'; +import { normalizeStories, commonGlobOptions } from '@storybook/core-common'; +import { isAbsolute, join, relative } from 'path'; +import slash from 'slash'; +import { glob } from 'glob'; + +export async function removeMDXEntries( + entries: StoriesEntry[], + options: Pick +) { + const list = normalizeStories(entries, { + configDir: options.configDir, + workingDir: options.configDir, + defaultFilesPattern: '**/*.@(stories.@(js|jsx|mjs|ts|tsx))', + }); + const result = ( + await Promise.all( + list.map(async ({ directory, files, titlePrefix }) => { + const pattern = join(directory, files); + const absolutePattern = isAbsolute(pattern) ? pattern : join(options.configDir, pattern); + const absoluteDirectory = isAbsolute(directory) + ? directory + : join(options.configDir, directory); + + return { + files: ( + await glob(slash(absolutePattern), { + ...commonGlobOptions(absolutePattern), + follow: true, + }) + ).map((f) => relative(absoluteDirectory, f)), + directory, + titlePrefix, + }; + }) + ) + ).flatMap((expanded, i) => { + const filteredEntries = expanded.files.filter((s) => !s.endsWith('.mdx')); + // only return the filtered entries when there is something to filter + // as webpack is faster with unexpanded globs + let items = []; + if (filteredEntries.length < expanded.files.length) { + items = filteredEntries.map((k) => ({ + ...expanded, + files: `**/${k}`, + })); + } else { + items = [list[i]]; + } + + return items; + }); + return result; +} From b20a4830585a648a1e9ab622df90b9688507878d Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Mon, 20 Nov 2023 13:42:15 +0100 Subject: [PATCH 2/6] maybe fix for windows --- .../src/utils/__tests__/remove-mdx-stories.test.ts | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/code/lib/core-server/src/utils/__tests__/remove-mdx-stories.test.ts b/code/lib/core-server/src/utils/__tests__/remove-mdx-stories.test.ts index 9c40041c0dc1..5438e48edfe9 100644 --- a/code/lib/core-server/src/utils/__tests__/remove-mdx-stories.test.ts +++ b/code/lib/core-server/src/utils/__tests__/remove-mdx-stories.test.ts @@ -2,6 +2,7 @@ import { glob as globlOriginal } from 'glob'; import { type StoriesEntry } from '@storybook/types'; import { normalizeStoriesEntry } from '@storybook/core-common'; import { join } from 'path'; +import slash from 'slash'; import { removeMDXEntries } from '../remove-mdx-entries'; const glob = globlOriginal as jest.MockedFunction; @@ -27,13 +28,14 @@ const createGlobMock = (input: ReturnType) => { if (Array.isArray(k)) { throw new Error('do not pass an array to glob during tests'); } - const result = input[k].result || []; + const result = input[slash(k)].result || []; return result; }; }; +const configDir = '/configDir/'; + test('empty', async () => { - const configDir = '/configDir/'; const list = createList([], configDir); glob.mockImplementation(createGlobMock(list)); @@ -50,7 +52,6 @@ test('empty', async () => { }); test('minimal', async () => { - const configDir = '/configDir/'; const list = createList([{ entry: '*.js', result: [] }], configDir); glob.mockImplementation(createGlobMock(list)); @@ -72,7 +73,6 @@ test('minimal', async () => { }); test('multiple', async () => { - const configDir = '/configDir/'; const list = createList( [ { entry: '*.ts', result: [] }, @@ -106,7 +106,6 @@ test('multiple', async () => { }); test('mdx but not matching any files', async () => { - const configDir = '/configDir/'; const list = createList( [ { entry: '*.mdx', result: [] }, @@ -140,7 +139,6 @@ test('mdx but not matching any files', async () => { }); test('removes entries that only yield mdx files', async () => { - const configDir = '/configDir/'; const list = createList( [ { entry: '*.mdx', result: ['/configDir/my-file.mdx'] }, @@ -168,7 +166,6 @@ test('removes entries that only yield mdx files', async () => { }); test('expands entries that only yield mixed files', async () => { - const configDir = '/configDir/'; const list = createList( [ { entry: '*.@(mdx|ts)', result: ['/configDir/my-file.mdx', '/configDir/my-file.ts'] }, @@ -201,7 +198,6 @@ test('expands entries that only yield mixed files', async () => { }); test('passes titlePrefix', async () => { - const configDir = '/configDir/'; const list = createList( [ { @@ -230,7 +226,6 @@ test('passes titlePrefix', async () => { }); test('expands to multiple entries', async () => { - const configDir = '/configDir/'; const list = createList( [ { From f1d57e9a928eeb6afe29d32010148640227fcd74 Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Mon, 20 Nov 2023 14:09:44 +0100 Subject: [PATCH 3/6] arg windows --- .../__tests__/remove-mdx-stories.test.ts | 107 ++++++++---------- 1 file changed, 47 insertions(+), 60 deletions(-) diff --git a/code/lib/core-server/src/utils/__tests__/remove-mdx-stories.test.ts b/code/lib/core-server/src/utils/__tests__/remove-mdx-stories.test.ts index 5438e48edfe9..18223c63c38a 100644 --- a/code/lib/core-server/src/utils/__tests__/remove-mdx-stories.test.ts +++ b/code/lib/core-server/src/utils/__tests__/remove-mdx-stories.test.ts @@ -7,16 +7,19 @@ import { removeMDXEntries } from '../remove-mdx-entries'; const glob = globlOriginal as jest.MockedFunction; +const configDir = '/configDir/'; +const workingDir = '/'; + jest.mock('glob', () => ({ glob: jest.fn() })); -const createList = (list: { entry: StoriesEntry; result: string[] }[], configDir: string) => { +const createList = (list: { entry: StoriesEntry; result: string[] }[]) => { return list.reduce>( (acc, { entry, result }) => { const { directory, files } = normalizeStoriesEntry(entry, { configDir, - workingDir: '/', + workingDir, }); - acc[join('/', directory, files)] = { result, entry }; + acc[slash(join('/', directory, files))] = { result, entry }; return acc; }, {} @@ -28,15 +31,17 @@ const createGlobMock = (input: ReturnType) => { if (Array.isArray(k)) { throw new Error('do not pass an array to glob during tests'); } - const result = input[slash(k)].result || []; - return result; + if (input[slash(k)]) { + return input[slash(k)]?.result; + } + + console.log({ k, input }); + throw new Error('can not find key in input'); }; }; -const configDir = '/configDir/'; - test('empty', async () => { - const list = createList([], configDir); + const list = createList([]); glob.mockImplementation(createGlobMock(list)); await expect(() => removeMDXEntries(Object.keys(list), { configDir })).rejects @@ -52,7 +57,7 @@ test('empty', async () => { }); test('minimal', async () => { - const list = createList([{ entry: '*.js', result: [] }], configDir); + const list = createList([{ entry: '*.js', result: [] }]); glob.mockImplementation(createGlobMock(list)); await expect( @@ -73,13 +78,10 @@ test('minimal', async () => { }); test('multiple', async () => { - const list = createList( - [ - { entry: '*.ts', result: [] }, - { entry: '*.js', result: [] }, - ], - configDir - ); + const list = createList([ + { entry: '*.ts', result: [] }, + { entry: '*.js', result: [] }, + ]); glob.mockImplementation(createGlobMock(list)); await expect( @@ -106,13 +108,10 @@ test('multiple', async () => { }); test('mdx but not matching any files', async () => { - const list = createList( - [ - { entry: '*.mdx', result: [] }, - { entry: '*.js', result: [] }, - ], - configDir - ); + const list = createList([ + { entry: '*.mdx', result: [] }, + { entry: '*.js', result: [] }, + ]); glob.mockImplementation(createGlobMock(list)); await expect( @@ -139,13 +138,10 @@ test('mdx but not matching any files', async () => { }); test('removes entries that only yield mdx files', async () => { - const list = createList( - [ - { entry: '*.mdx', result: ['/configDir/my-file.mdx'] }, - { entry: '*.js', result: [] }, - ], - configDir - ); + const list = createList([ + { entry: '*.mdx', result: ['/configDir/my-file.mdx'] }, + { entry: '*.js', result: [] }, + ]); glob.mockImplementation(createGlobMock(list)); await expect( @@ -166,13 +162,10 @@ test('removes entries that only yield mdx files', async () => { }); test('expands entries that only yield mixed files', async () => { - const list = createList( - [ - { entry: '*.@(mdx|ts)', result: ['/configDir/my-file.mdx', '/configDir/my-file.ts'] }, - { entry: '*.js', result: [] }, - ], - configDir - ); + const list = createList([ + { entry: '*.@(mdx|ts)', result: ['/configDir/my-file.mdx', '/configDir/my-file.ts'] }, + { entry: '*.js', result: [] }, + ]); glob.mockImplementation(createGlobMock(list)); await expect( @@ -198,15 +191,12 @@ test('expands entries that only yield mixed files', async () => { }); test('passes titlePrefix', async () => { - const list = createList( - [ - { - entry: { files: '*.@(mdx|ts)', directory: '.', titlePrefix: 'foo' }, - result: ['/configDir/my-file.mdx', '/configDir/my-file.ts'], - }, - ], - configDir - ); + const list = createList([ + { + entry: { files: '*.@(mdx|ts)', directory: '.', titlePrefix: 'foo' }, + result: ['/configDir/my-file.mdx', '/configDir/my-file.ts'], + }, + ]); glob.mockImplementation(createGlobMock(list)); await expect( @@ -226,20 +216,17 @@ test('passes titlePrefix', async () => { }); test('expands to multiple entries', async () => { - const list = createList( - [ - { - entry: { files: '*.@(mdx|ts)', directory: '.', titlePrefix: 'foo' }, - result: [ - '/configDir/my-file.mdx', - '/configDir/my-file1.ts', - '/configDir/my-file2.ts', - '/configDir/my-file3.ts', - ], - }, - ], - configDir - ); + const list = createList([ + { + entry: { files: '*.@(mdx|ts)', directory: '.', titlePrefix: 'foo' }, + result: [ + '/configDir/my-file.mdx', + '/configDir/my-file1.ts', + '/configDir/my-file2.ts', + '/configDir/my-file3.ts', + ], + }, + ]); glob.mockImplementation(createGlobMock(list)); await expect( From 27fd45d13b7ac7b3dd6effdf97e6965b9f4e6e64 Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Mon, 20 Nov 2023 14:30:59 +0100 Subject: [PATCH 4/6] imprvoe, fix tests for windows --- .../__tests__/remove-mdx-stories.test.ts | 92 +++++++++---------- .../src/utils/remove-mdx-entries.ts | 2 +- 2 files changed, 43 insertions(+), 51 deletions(-) diff --git a/code/lib/core-server/src/utils/__tests__/remove-mdx-stories.test.ts b/code/lib/core-server/src/utils/__tests__/remove-mdx-stories.test.ts index 18223c63c38a..3ad458b287ab 100644 --- a/code/lib/core-server/src/utils/__tests__/remove-mdx-stories.test.ts +++ b/code/lib/core-server/src/utils/__tests__/remove-mdx-stories.test.ts @@ -35,7 +35,6 @@ const createGlobMock = (input: ReturnType) => { return input[slash(k)]?.result; } - console.log({ k, input }); throw new Error('can not find key in input'); }; }; @@ -60,17 +59,16 @@ test('minimal', async () => { const list = createList([{ entry: '*.js', result: [] }]); glob.mockImplementation(createGlobMock(list)); - await expect( - removeMDXEntries( - Object.values(list).map((e) => e.entry), - { configDir } - ) - ).resolves.toMatchInlineSnapshot(` + const result = await removeMDXEntries( + Object.values(list).map((e) => e.entry), + { configDir } + ); + + expect(result.map(({ importPathMatcher, ...v }) => v)).toMatchInlineSnapshot(` Array [ Object { "directory": ".", "files": "*.js", - "importPathMatcher": /\\^\\\\\\.\\[\\\\\\\\/\\]\\(\\?:\\(\\?!\\\\\\.\\)\\(\\?=\\.\\)\\[\\^/\\]\\*\\?\\\\\\.js\\)\\$/, "titlePrefix": "", }, ] @@ -84,23 +82,21 @@ test('multiple', async () => { ]); glob.mockImplementation(createGlobMock(list)); - await expect( - removeMDXEntries( - Object.values(list).map((e) => e.entry), - { configDir } - ) - ).resolves.toMatchInlineSnapshot(` + const result = await removeMDXEntries( + Object.values(list).map((e) => e.entry), + { configDir } + ); + + expect(result.map(({ importPathMatcher, ...v }) => v)).toMatchInlineSnapshot(` Array [ Object { "directory": ".", "files": "*.ts", - "importPathMatcher": /\\^\\\\\\.\\[\\\\\\\\/\\]\\(\\?:\\(\\?!\\\\\\.\\)\\(\\?=\\.\\)\\[\\^/\\]\\*\\?\\\\\\.ts\\)\\$/, "titlePrefix": "", }, Object { "directory": ".", "files": "*.js", - "importPathMatcher": /\\^\\\\\\.\\[\\\\\\\\/\\]\\(\\?:\\(\\?!\\\\\\.\\)\\(\\?=\\.\\)\\[\\^/\\]\\*\\?\\\\\\.js\\)\\$/, "titlePrefix": "", }, ] @@ -114,23 +110,21 @@ test('mdx but not matching any files', async () => { ]); glob.mockImplementation(createGlobMock(list)); - await expect( - removeMDXEntries( - Object.values(list).map((e) => e.entry), - { configDir } - ) - ).resolves.toMatchInlineSnapshot(` + const result = await removeMDXEntries( + Object.values(list).map((e) => e.entry), + { configDir } + ); + + expect(result.map(({ importPathMatcher, ...v }) => v)).toMatchInlineSnapshot(` Array [ Object { "directory": ".", "files": "*.mdx", - "importPathMatcher": /\\^\\\\\\.\\[\\\\\\\\/\\]\\(\\?:\\(\\?!\\\\\\.\\)\\(\\?=\\.\\)\\[\\^/\\]\\*\\?\\\\\\.mdx\\)\\$/, "titlePrefix": "", }, Object { "directory": ".", "files": "*.js", - "importPathMatcher": /\\^\\\\\\.\\[\\\\\\\\/\\]\\(\\?:\\(\\?!\\\\\\.\\)\\(\\?=\\.\\)\\[\\^/\\]\\*\\?\\\\\\.js\\)\\$/, "titlePrefix": "", }, ] @@ -144,17 +138,16 @@ test('removes entries that only yield mdx files', async () => { ]); glob.mockImplementation(createGlobMock(list)); - await expect( - removeMDXEntries( - Object.values(list).map((e) => e.entry), - { configDir } - ) - ).resolves.toMatchInlineSnapshot(` + const result = await removeMDXEntries( + Object.values(list).map((e) => e.entry), + { configDir } + ); + + expect(result.map(({ importPathMatcher, ...v }) => v)).toMatchInlineSnapshot(` Array [ Object { "directory": ".", "files": "*.js", - "importPathMatcher": /\\^\\\\\\.\\[\\\\\\\\/\\]\\(\\?:\\(\\?!\\\\\\.\\)\\(\\?=\\.\\)\\[\\^/\\]\\*\\?\\\\\\.js\\)\\$/, "titlePrefix": "", }, ] @@ -168,12 +161,12 @@ test('expands entries that only yield mixed files', async () => { ]); glob.mockImplementation(createGlobMock(list)); - await expect( - removeMDXEntries( - Object.values(list).map((e) => e.entry), - { configDir } - ) - ).resolves.toMatchInlineSnapshot(` + const result = await removeMDXEntries( + Object.values(list).map((e) => e.entry), + { configDir } + ); + + expect(result.map(({ importPathMatcher, ...v }) => v)).toMatchInlineSnapshot(` Array [ Object { "directory": ".", @@ -183,7 +176,6 @@ test('expands entries that only yield mixed files', async () => { Object { "directory": ".", "files": "*.js", - "importPathMatcher": /\\^\\\\\\.\\[\\\\\\\\/\\]\\(\\?:\\(\\?!\\\\\\.\\)\\(\\?=\\.\\)\\[\\^/\\]\\*\\?\\\\\\.js\\)\\$/, "titlePrefix": "", }, ] @@ -199,12 +191,12 @@ test('passes titlePrefix', async () => { ]); glob.mockImplementation(createGlobMock(list)); - await expect( - removeMDXEntries( - Object.values(list).map((e) => e.entry), - { configDir } - ) - ).resolves.toMatchInlineSnapshot(` + const result = await removeMDXEntries( + Object.values(list).map((e) => e.entry), + { configDir } + ); + + expect(result.map(({ importPathMatcher, ...v }) => v)).toMatchInlineSnapshot(` Array [ Object { "directory": ".", @@ -229,12 +221,12 @@ test('expands to multiple entries', async () => { ]); glob.mockImplementation(createGlobMock(list)); - await expect( - removeMDXEntries( - Object.values(list).map((e) => e.entry), - { configDir } - ) - ).resolves.toMatchInlineSnapshot(` + const result = await removeMDXEntries( + Object.values(list).map((e) => e.entry), + { configDir } + ); + + expect(result.map(({ importPathMatcher, ...v }) => v)).toMatchInlineSnapshot(` Array [ Object { "directory": ".", diff --git a/code/lib/core-server/src/utils/remove-mdx-entries.ts b/code/lib/core-server/src/utils/remove-mdx-entries.ts index 0efc7923baad..e53c4420e203 100644 --- a/code/lib/core-server/src/utils/remove-mdx-entries.ts +++ b/code/lib/core-server/src/utils/remove-mdx-entries.ts @@ -7,7 +7,7 @@ import { glob } from 'glob'; export async function removeMDXEntries( entries: StoriesEntry[], options: Pick -) { +): Promise[]> { const list = normalizeStories(entries, { configDir: options.configDir, workingDir: options.configDir, From 5cdee70c1a20edef996ff02161e824ef53d36783 Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Mon, 20 Nov 2023 14:35:04 +0100 Subject: [PATCH 5/6] oops --- code/lib/core-server/src/utils/remove-mdx-entries.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/lib/core-server/src/utils/remove-mdx-entries.ts b/code/lib/core-server/src/utils/remove-mdx-entries.ts index e53c4420e203..e1e763ca6043 100644 --- a/code/lib/core-server/src/utils/remove-mdx-entries.ts +++ b/code/lib/core-server/src/utils/remove-mdx-entries.ts @@ -7,7 +7,7 @@ import { glob } from 'glob'; export async function removeMDXEntries( entries: StoriesEntry[], options: Pick -): Promise[]> { +): Promise> { const list = normalizeStories(entries, { configDir: options.configDir, workingDir: options.configDir, From 1e82d194d050647e7a6d43460503fbb137fffd39 Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Mon, 20 Nov 2023 14:57:01 +0100 Subject: [PATCH 6/6] aaaa --- .../utils/__tests__/remove-mdx-stories.test.ts | 14 +++++++------- .../core-server/src/utils/remove-mdx-entries.ts | 15 +++++++++------ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/code/lib/core-server/src/utils/__tests__/remove-mdx-stories.test.ts b/code/lib/core-server/src/utils/__tests__/remove-mdx-stories.test.ts index 3ad458b287ab..93280240e74e 100644 --- a/code/lib/core-server/src/utils/__tests__/remove-mdx-stories.test.ts +++ b/code/lib/core-server/src/utils/__tests__/remove-mdx-stories.test.ts @@ -64,7 +64,7 @@ test('minimal', async () => { { configDir } ); - expect(result.map(({ importPathMatcher, ...v }) => v)).toMatchInlineSnapshot(` + expect(result).toMatchInlineSnapshot(` Array [ Object { "directory": ".", @@ -87,7 +87,7 @@ test('multiple', async () => { { configDir } ); - expect(result.map(({ importPathMatcher, ...v }) => v)).toMatchInlineSnapshot(` + expect(result).toMatchInlineSnapshot(` Array [ Object { "directory": ".", @@ -115,7 +115,7 @@ test('mdx but not matching any files', async () => { { configDir } ); - expect(result.map(({ importPathMatcher, ...v }) => v)).toMatchInlineSnapshot(` + expect(result).toMatchInlineSnapshot(` Array [ Object { "directory": ".", @@ -143,7 +143,7 @@ test('removes entries that only yield mdx files', async () => { { configDir } ); - expect(result.map(({ importPathMatcher, ...v }) => v)).toMatchInlineSnapshot(` + expect(result).toMatchInlineSnapshot(` Array [ Object { "directory": ".", @@ -166,7 +166,7 @@ test('expands entries that only yield mixed files', async () => { { configDir } ); - expect(result.map(({ importPathMatcher, ...v }) => v)).toMatchInlineSnapshot(` + expect(result).toMatchInlineSnapshot(` Array [ Object { "directory": ".", @@ -196,7 +196,7 @@ test('passes titlePrefix', async () => { { configDir } ); - expect(result.map(({ importPathMatcher, ...v }) => v)).toMatchInlineSnapshot(` + expect(result).toMatchInlineSnapshot(` Array [ Object { "directory": ".", @@ -226,7 +226,7 @@ test('expands to multiple entries', async () => { { configDir } ); - expect(result.map(({ importPathMatcher, ...v }) => v)).toMatchInlineSnapshot(` + expect(result).toMatchInlineSnapshot(` Array [ Object { "directory": ".", diff --git a/code/lib/core-server/src/utils/remove-mdx-entries.ts b/code/lib/core-server/src/utils/remove-mdx-entries.ts index e1e763ca6043..ed93c1bc8d64 100644 --- a/code/lib/core-server/src/utils/remove-mdx-entries.ts +++ b/code/lib/core-server/src/utils/remove-mdx-entries.ts @@ -7,7 +7,7 @@ import { glob } from 'glob'; export async function removeMDXEntries( entries: StoriesEntry[], options: Pick -): Promise> { +): Promise { const list = normalizeStories(entries, { configDir: options.configDir, workingDir: options.configDir, @@ -34,18 +34,21 @@ export async function removeMDXEntries( }; }) ) - ).flatMap((expanded, i) => { - const filteredEntries = expanded.files.filter((s) => !s.endsWith('.mdx')); + ).flatMap(({ directory, files, titlePrefix }, i) => { + const filteredEntries = files.filter((s) => !s.endsWith('.mdx')); // only return the filtered entries when there is something to filter // as webpack is faster with unexpanded globs let items = []; - if (filteredEntries.length < expanded.files.length) { + if (filteredEntries.length < files.length) { items = filteredEntries.map((k) => ({ - ...expanded, + directory, + titlePrefix, files: `**/${k}`, })); } else { - items = [list[i]]; + items = [ + { directory: list[i].directory, titlePrefix: list[i].titlePrefix, files: list[i].files }, + ]; } return items;