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: Support community builders in sb init #14651

Merged
merged 1 commit into from
Apr 19, 2021
Merged
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
2 changes: 1 addition & 1 deletion lib/cli/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ program
.option('-t --type <type>', 'Add Storybook for a specific project type')
.option('--story-format <csf | csf-ts | mdx >', 'Generate stories in a specified format')
.option('-y --yes', 'Answer yes to all prompts')
.option('-b --builder <webpack4 | webpack5>', 'Builder library')
.option('-b --builder <builder>', 'Builder library')
.action((options) => initiate(options, pkg));

program
Expand Down
27 changes: 20 additions & 7 deletions lib/cli/src/generators/baseGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { NpmOptions } from '../NpmOptions';
import { StoryFormat, SupportedLanguage, SupportedFrameworks, Builder } from '../project_types';
import {
StoryFormat,
SupportedLanguage,
SupportedFrameworks,
Builder,
CoreBuilder,
} from '../project_types';
import { getBabelDependencies, copyComponents } from '../helpers';
import { configure } from './configure';
import { getPackageDetails, JsPackageManager } from '../js-package-manager';
Expand Down Expand Up @@ -42,6 +48,17 @@ const defaultOptions: FrameworkOptions = {
commonJs: false,
};

const builderDependencies = (builder: Builder) => {
switch (builder) {
case CoreBuilder.Webpack4:
return [];
case CoreBuilder.Webpack5:
return ['@storybook/builder-webpack5'];
default:
return [builder];
}
};

export async function baseGenerator(
packageManager: JsPackageManager,
npmOptions: NpmOptions,
Expand Down Expand Up @@ -73,10 +90,6 @@ export async function baseGenerator(
const yarn2Dependencies =
packageManager.type === 'yarn2' ? ['@storybook/addon-docs', '@mdx-js/react'] : [];

const builderDependencies: Partial<Record<Builder, string>> = {
[Builder.Webpack5]: '@storybook/builder-webpack5',
};

const packageJson = packageManager.retrievePackageJson();
const installedDependencies = new Set(Object.keys(packageJson.dependencies));

Expand All @@ -86,7 +99,7 @@ export async function baseGenerator(
...extraPackages,
...extraAddons,
...yarn2Dependencies,
builderDependencies[builder],
...builderDependencies(builder),
]
.filter(Boolean)
.filter(
Expand All @@ -96,7 +109,7 @@ export async function baseGenerator(
const versionedPackages = await packageManager.getVersionedPackages(...packages);

const mainOptions =
builder !== Builder.Webpack4
builder !== CoreBuilder.Webpack4
? {
core: {
builder,
Expand Down
3 changes: 2 additions & 1 deletion lib/cli/src/initiate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
StoryFormat,
SupportedLanguage,
Builder,
CoreBuilder,
} from './project_types';
import { commandLog, codeLog, paddedLog } from './helpers';
import angularGenerator from './generators/ANGULAR';
Expand Down Expand Up @@ -67,7 +68,7 @@ const installStorybook = (projectType: ProjectType, options: CommandOptions): Pr
const generatorOptions = {
storyFormat: options.storyFormat || defaultStoryFormat,
language,
builder: options.builder || Builder.Webpack4,
builder: options.builder || CoreBuilder.Webpack4,
};

const end = () => {
Expand Down
4 changes: 3 additions & 1 deletion lib/cli/src/project_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ export enum StoryFormat {
MDX = 'mdx',
}

export enum Builder {
export enum CoreBuilder {
Webpack4 = 'webpack4',
Webpack5 = 'webpack5',
}

export type Builder = CoreBuilder | string;

export enum SupportedLanguage {
JAVASCRIPT = 'javascript',
TYPESCRIPT = 'typescript',
Expand Down