Skip to content
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
wants to merge 1 commit into
base: next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions code/core/src/cli/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import picocolors from 'picocolors';
import invariant from 'tiny-invariant';

import { build } from '../build';
import { buildIndex as index } from '../buildIndex';
import { dev } from '../dev';

addToGlobalContext('cliVersion', versions.storybook);
Expand Down Expand Up @@ -127,6 +128,31 @@ command('build')
}).catch(() => process.exit(1));
});

command('index')
.option('-o, --output-file <file-name>', 'JSON file to output index')
.option('-c, --config-dir <dir-name>', 'Directory where to load Storybook configurations from')
.option('--quiet', 'Suppress verbose build output')
.option('--loglevel <level>', 'Control level of logging during build')
.action(async (options) => {
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
logger.setLevel(options.loglevel);
consoleLogger.log(picocolors.bold(`${pkg.name} v${pkg.version}\n`));

// The key is the field created in `options` variable for
// each command line argument. Value is the env variable.
getEnvConfig(options, {
staticDir: 'SBCONFIG_STATIC_DIR',
outputDir: 'SBCONFIG_OUTPUT_DIR',
configDir: 'SBCONFIG_CONFIG_DIR',
});

await index({
...options,
packageJson: pkg,
test: !!options.test || process.env.SB_TESTBUILD === 'true',
}).catch(() => process.exit(1));
});

program.on('command:*', ([invalidCmd]) => {
consoleLogger.error(
' Invalid command: %s.\n See --help for a list of available commands.',
Expand Down
23 changes: 23 additions & 0 deletions code/core/src/cli/buildIndex.ts
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)
);
};
71 changes: 71 additions & 0 deletions code/core/src/core-server/build-index.ts
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));
};
Comment on lines +67 to +71
Copy link
Member

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.

3 changes: 1 addition & 2 deletions code/core/src/core-server/build-static.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { existsSync } from 'node:fs';
import { cp, mkdir, readdir } from 'node:fs/promises';
import { cp, mkdir } from 'node:fs/promises';
import { rm } from 'node:fs/promises';
import { dirname, join, relative, resolve } from 'node:path';

Expand Down
1 change: 1 addition & 0 deletions code/core/src/core-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export { getPreviewHeadTemplate, getPreviewBodyTemplate } from '@storybook/core/

export * from './build-static';
export * from './build-dev';
export * from './build-index';
export * from './withTelemetry';
export { default as build } from './standalone';
export { mapStaticDir } from './utils/server-statics';
Expand Down
1 change: 1 addition & 0 deletions code/core/src/telemetry/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type EventType =
| 'boot'
| 'dev'
| 'build'
| 'index'
| 'upgrade'
| 'init'
| 'scaffolded-empty'
Expand Down
2 changes: 1 addition & 1 deletion code/lib/cli/src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { spawn } from 'child_process';

const args = process.argv.slice(2);

if (['dev', 'build'].includes(args[0])) {
if (['dev', 'build', 'index'].includes(args[0])) {
require('@storybook/core/cli/bin');
} else {
const proxiedArgs =
Expand Down
Loading