CLI: Automatically generate babel config file in empty projects

This commit is contained in:
Michael Shilman 2021-08-28 00:12:52 +08:00
parent 5a988b02b4
commit 863a561c19
3 changed files with 13 additions and 1 deletions

View File

@ -11,7 +11,7 @@ import { extract } from './extract';
import { upgrade } from './upgrade';
import { repro } from './repro';
import { link } from './link';
import { generateStorybookBabelConfigInCWD } from './babal-config';
import { generateStorybookBabelConfigInCWD } from './babel-config';
const pkg = sync({ cwd: __dirname }).packageJson;

View File

@ -1,3 +1,5 @@
import fse from 'fs-extra';
import { getStorybookBabelDependencies } from '@storybook/core-common';
import { NpmOptions } from '../NpmOptions';
import {
StoryFormat,
@ -9,6 +11,7 @@ import {
import { getBabelDependencies, copyComponents } from '../helpers';
import { configure } from './configure';
import { getPackageDetails, JsPackageManager } from '../js-package-manager';
import { generateStorybookBabelConfigInCWD } from '../babel-config';
export type GeneratorOptions = {
language: SupportedLanguage;
@ -91,6 +94,11 @@ export async function baseGenerator(
const yarn2Dependencies =
packageManager.type === 'yarn2' ? ['@storybook/addon-docs', '@mdx-js/react'] : [];
const files = await fse.readdir(process.cwd());
const isNewFolder = !files.some(
(fname) => fname.startsWith('.babel') || fname.startsWith('babel') || fname === 'package.json'
);
const packageJson = packageManager.retrievePackageJson();
const installedDependencies = new Set(Object.keys(packageJson.dependencies));
@ -129,6 +137,10 @@ export async function baseGenerator(
}
const babelDependencies = addBabel ? await getBabelDependencies(packageManager, packageJson) : [];
if (isNewFolder) {
babelDependencies.push(...getStorybookBabelDependencies());
await generateStorybookBabelConfigInCWD();
}
packageManager.addDependencies({ ...npmOptions, packageJson }, [
...versionedPackages,
...babelDependencies,