CLI: Support community builders in sb init

This commit is contained in:
Michael Shilman 2021-04-19 18:53:06 +08:00
parent 3afa7f41c4
commit 59e56e846d
4 changed files with 26 additions and 10 deletions

View File

@ -24,7 +24,7 @@ program
.option('-t --type <type>', 'Add Storybook for a specific project type') .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('--story-format <csf | csf-ts | mdx >', 'Generate stories in a specified format')
.option('-y --yes', 'Answer yes to all prompts') .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)); .action((options) => initiate(options, pkg));
program program

View File

@ -1,5 +1,11 @@
import { NpmOptions } from '../NpmOptions'; 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 { getBabelDependencies, copyComponents } from '../helpers';
import { configure } from './configure'; import { configure } from './configure';
import { getPackageDetails, JsPackageManager } from '../js-package-manager'; import { getPackageDetails, JsPackageManager } from '../js-package-manager';
@ -42,6 +48,17 @@ const defaultOptions: FrameworkOptions = {
commonJs: false, 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( export async function baseGenerator(
packageManager: JsPackageManager, packageManager: JsPackageManager,
npmOptions: NpmOptions, npmOptions: NpmOptions,
@ -73,10 +90,6 @@ export async function baseGenerator(
const yarn2Dependencies = const yarn2Dependencies =
packageManager.type === 'yarn2' ? ['@storybook/addon-docs', '@mdx-js/react'] : []; 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 packageJson = packageManager.retrievePackageJson();
const installedDependencies = new Set(Object.keys(packageJson.dependencies)); const installedDependencies = new Set(Object.keys(packageJson.dependencies));
@ -86,7 +99,7 @@ export async function baseGenerator(
...extraPackages, ...extraPackages,
...extraAddons, ...extraAddons,
...yarn2Dependencies, ...yarn2Dependencies,
builderDependencies[builder], ...builderDependencies(builder),
] ]
.filter(Boolean) .filter(Boolean)
.filter( .filter(
@ -96,7 +109,7 @@ export async function baseGenerator(
const versionedPackages = await packageManager.getVersionedPackages(...packages); const versionedPackages = await packageManager.getVersionedPackages(...packages);
const mainOptions = const mainOptions =
builder !== Builder.Webpack4 builder !== CoreBuilder.Webpack4
? { ? {
core: { core: {
builder, builder,

View File

@ -8,6 +8,7 @@ import {
StoryFormat, StoryFormat,
SupportedLanguage, SupportedLanguage,
Builder, Builder,
CoreBuilder,
} from './project_types'; } from './project_types';
import { commandLog, codeLog, paddedLog } from './helpers'; import { commandLog, codeLog, paddedLog } from './helpers';
import angularGenerator from './generators/ANGULAR'; import angularGenerator from './generators/ANGULAR';
@ -67,7 +68,7 @@ const installStorybook = (projectType: ProjectType, options: CommandOptions): Pr
const generatorOptions = { const generatorOptions = {
storyFormat: options.storyFormat || defaultStoryFormat, storyFormat: options.storyFormat || defaultStoryFormat,
language, language,
builder: options.builder || Builder.Webpack4, builder: options.builder || CoreBuilder.Webpack4,
}; };
const end = () => { const end = () => {

View File

@ -84,11 +84,13 @@ export enum StoryFormat {
MDX = 'mdx', MDX = 'mdx',
} }
export enum Builder { export enum CoreBuilder {
Webpack4 = 'webpack4', Webpack4 = 'webpack4',
Webpack5 = 'webpack5', Webpack5 = 'webpack5',
} }
export type Builder = CoreBuilder | string;
export enum SupportedLanguage { export enum SupportedLanguage {
JAVASCRIPT = 'javascript', JAVASCRIPT = 'javascript',
TYPESCRIPT = 'typescript', TYPESCRIPT = 'typescript',