-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ea2268
commit 74fbb39
Showing
5 changed files
with
102 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import path from 'path'; | ||
import { describe, expect, it } from 'vitest'; | ||
import { getStoryId } from './get-story-id'; | ||
|
||
describe('getStoryId', () => { | ||
it('should return the storyId', async () => { | ||
const cwd = process.cwd(); | ||
const options = { | ||
configDir: path.join(cwd, '.storybook'), | ||
presets: { | ||
apply: (val: string) => { | ||
if (val === 'stories') { | ||
return Promise.resolve(['../src/**/*.stories.@(js|jsx|mjs|ts|tsx)']); | ||
} | ||
}, | ||
}, | ||
} as any; | ||
const storyFilePath = path.join(cwd, 'src', 'components', 'stories', 'Page1.stories.ts'); | ||
const exportedStoryName = 'Default'; | ||
|
||
const storyId = await getStoryId({ storyFilePath, exportedStoryName }, options); | ||
|
||
expect(storyId).toBe('components-stories-page1--default'); | ||
}); | ||
|
||
it('should throw an error if the storyId cannot be calculated', async () => { | ||
const cwd = process.cwd(); | ||
const options = { | ||
configDir: path.join(cwd, '.storybook'), | ||
presets: { | ||
apply: (val: string) => { | ||
if (val === 'stories') { | ||
return Promise.resolve(['../src/**/*.stories.@(js|jsx|mjs|ts|tsx)']); | ||
} | ||
}, | ||
}, | ||
} as any; | ||
const storyFilePath = path.join(cwd, 'not-covered-path', 'stories', 'Page1.stories.ts'); | ||
const exportedStoryName = 'Default'; | ||
|
||
await expect(() => | ||
getStoryId({ storyFilePath, exportedStoryName }, options) | ||
).rejects.toThrowError(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import type { Options } from '@storybook/types'; | ||
import dedent from 'ts-dedent'; | ||
import { normalizeStories, normalizeStoryPath } from '@storybook/core-common'; | ||
import path from 'path'; | ||
import { storyNameFromExport, toId } from '@storybook/csf'; | ||
import slash from 'slash'; | ||
import { userOrAutoTitleFromSpecifier } from '@storybook/preview-api'; | ||
|
||
interface Data { | ||
storyFilePath: string; | ||
exportedStoryName: string; | ||
} | ||
|
||
export async function getStoryId(data: Data, options: Options) { | ||
const stories = await options.presets.apply('stories', [], options); | ||
|
||
const workingDir = process.cwd(); | ||
|
||
const normalizedStories = normalizeStories(stories, { | ||
configDir: options.configDir, | ||
workingDir, | ||
}); | ||
|
||
const relativePath = path.relative(workingDir, data.storyFilePath); | ||
const importPath = slash(normalizeStoryPath(relativePath)); | ||
|
||
const autoTitle = normalizedStories | ||
.map((normalizeStory) => userOrAutoTitleFromSpecifier(importPath, normalizeStory)) | ||
.filter(Boolean)[0]; | ||
|
||
if (autoTitle === undefined) { | ||
// eslint-disable-next-line local-rules/no-uncategorized-errors | ||
throw new Error(dedent` | ||
The generation of your new Story file was successful! But it seems that we are unable to index it. | ||
Please make sure that the new Story file is matched by the 'stories' glob pattern in your Storybook configuration. | ||
The location of the new Story file is: ${relativePath} | ||
`); | ||
} | ||
|
||
const storyName = storyNameFromExport(data.exportedStoryName); | ||
const storyId = toId(autoTitle as string, storyName); | ||
|
||
return storyId; | ||
} |