-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
CLI: Add index command / API #30071
Draft
shilman
wants to merge
1
commit into
next
Choose a base branch
from
shilman/build-index
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
CLI: Add index command / API #30071
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { cache } from '@storybook/core/common'; | ||
|
||
import { buildIndexStandalone, withTelemetry } from '@storybook/core/core-server'; | ||
|
||
import { findPackage } from 'fd-package-json'; | ||
import invariant from 'tiny-invariant'; | ||
|
||
export const buildIndex = async (cliOptions: any) => { | ||
const packageJson = await findPackage(__dirname); | ||
invariant(packageJson, 'Failed to find the closest package.json file.'); | ||
const options = { | ||
...cliOptions, | ||
configDir: cliOptions.configDir || './.storybook', | ||
outputFile: cliOptions.outputFile || './index.json', | ||
ignorePreview: true, | ||
configType: 'PRODUCTION', | ||
cache, | ||
packageJson, | ||
}; | ||
await withTelemetry('index', { cliOptions, presetOptions: options }, () => | ||
buildIndexStandalone(options) | ||
); | ||
}; |
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,71 @@ | ||
import { writeFile } from 'node:fs/promises'; | ||
import { join } from 'node:path'; | ||
import { resolve } from 'node:path'; | ||
|
||
import { loadAllPresets, loadMainConfig, normalizeStories } from '@storybook/core/common'; | ||
import type { BuilderOptions, CLIOptions, LoadOptions } from '@storybook/core/types'; | ||
|
||
import { logger } from '@storybook/core/node-logger'; | ||
|
||
import { StoryIndexGenerator } from './utils/StoryIndexGenerator'; | ||
|
||
type BuildIndexOptions = CLIOptions & LoadOptions & BuilderOptions & { outputFile: string }; | ||
|
||
export const buildIndex = async (options: BuildIndexOptions) => { | ||
const configDir = resolve(options.configDir ?? '.storybook'); | ||
const config = await loadMainConfig({ | ||
configDir, | ||
noCache: true, | ||
}); | ||
|
||
const { framework } = config; | ||
const corePresets = []; | ||
|
||
const frameworkName = typeof framework === 'string' ? framework : framework?.name; | ||
if (frameworkName) { | ||
corePresets.push(join(frameworkName, 'preset')); | ||
} else if (!options.ignorePreview) { | ||
logger.warn(`you have not specified a framework in your ${options.configDir}/main.js`); | ||
} | ||
|
||
const presets = await loadAllPresets({ | ||
corePresets: [ | ||
require.resolve('@storybook/core/core-server/presets/common-preset'), | ||
...corePresets, | ||
], | ||
overridePresets: [ | ||
require.resolve('@storybook/core/core-server/presets/common-override-preset'), | ||
], | ||
isCritical: true, | ||
...options, | ||
}); | ||
const [indexers, stories, docsOptions] = await Promise.all([ | ||
// presets.apply('features'), | ||
presets.apply('experimental_indexers', []), | ||
presets.apply('stories', []), | ||
presets.apply('docs', {}), | ||
]); | ||
|
||
const workingDir = process.cwd(); | ||
const directories = { | ||
configDir, | ||
workingDir, | ||
}; | ||
const normalizedStories = normalizeStories(stories, directories); | ||
const generator = new StoryIndexGenerator(normalizedStories, { | ||
...directories, | ||
indexers, | ||
docs: docsOptions, | ||
build: {}, | ||
}); | ||
|
||
await generator.initialize(); | ||
const index = await generator.getIndex(); | ||
return index; | ||
}; | ||
|
||
export const buildIndexStandalone = async (options: BuildIndexOptions) => { | ||
const index = await buildIndex(options); | ||
console.log(`Writing index to ${options.outputFile}`); | ||
await writeFile(options.outputFile, JSON.stringify(index, null, 2)); | ||
}; | ||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this get added to
standalone.ts
also? I'm not quite sure what's up with that API.