Skip to content

Commit

Permalink
fix: fix generator
Browse files Browse the repository at this point in the history
  • Loading branch information
JSerFeng committed Oct 19, 2023
1 parent 704cf3d commit 2f6179f
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ If your current project is already a Modern.js project and you haven't used any
```bash
$ npx modern new
? Please select the operation you want: Enable features
? Please select the feature name: Enable Visual Testing (Storybook)
? Please select the feature name: EnableStorybook」V7
```
This command will create a template for Storybook, including:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Storybook 可以用非常多的工具进行构建,webpack,Vite 等。
```bash
$ npx modern new
? 请选择你想要的操作 启用可选功能
? 请选择功能名称 启用 StorybookV7 模式
? 请选择功能名称 启用「Storybook」V7
```

该命令会创建好 Storybook 常用的模版,包括
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ export const MWANewActionPluginName: Record<
[ActionFunction.MicroFrontend]: 'garfishPlugin',
[ActionFunction.Test]: 'testPlugin',
[ActionFunction.Storybook]: 'storybookPlugin',
[ActionFunction.StorybookV7]: 'storybookFramework',
[ActionFunction.SSG]: 'ssgPlugin',
[ActionFunction.Polyfill]: 'polyfillPlugin',
[ActionFunction.Proxy]: 'proxyPlugin',
Expand Down
79 changes: 23 additions & 56 deletions packages/generator/generators/storybook-generator-next/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { JsonAPI } from '@modern-js/codesmith-api-json';
import {
isTsProject,
getPackageManager,
getPackageManagerText,
fs,
isReact18,
getPackageVersion,
Expand All @@ -15,10 +14,11 @@ import {
i18n as commonI18n,
Language,
} from '@modern-js/generator-common';
import { i18n, localeKeys } from './locale';
import { i18n } from './locale';
import { getMajorVersion } from './utils';

const ADDON_ESSENTIAL = '@storybook/addon-essentials';
const MODERN_STORYBOOK = '@modern-js/storybook';

const getGeneratorPath = (generator: string, distTag: string) => {
if (process.env.CODESMITH_ENV === 'development') {
Expand All @@ -36,44 +36,23 @@ const handleTemplateFile = async (
) => {
const appDir = context.materials.default.basePath;
const language = isTsProject(appDir) ? Language.TS : Language.JS;
await appApi.forgeTemplate(
'../templates/stories/**/*',
undefined,
resourceKey =>
resourceKey
.replace('templates/', '')
.replace('.handlebars', `.${language}x`),
);

await appApi.forgeTemplate(
`../templates/storybook-${language}/**/*`,
`templates/storybook-${language}/**/*`,
undefined,
resourceKey =>
resourceKey
.replace('templates/', '')
.replace(`storybook-${language}`, '.storybook')
.replace('.handlebars', `.${language}`),
resourceKey => {
const key = resourceKey
.replace(`templates/storybook-${language}/`, '.storybook/')
.replace('.handlebars', `.${language}`);
return key;
},
);

if (language === Language.TS) {
await appApi.forgeTemplate(
'../templates/ts-template/**/*',
undefined,
resourceKey =>
resourceKey
.replace('templates/ts-template/', `stories/`)
.replace('.handlebars', ``),
);
} else {
appApi.forgeTemplate(
'../templates/js-template/**/*',
undefined,
resourceKey =>
resourceKey
.replace('templates/js-template/', `stories/`)
.replace('.handlebars', ``),
);
}
await appApi.forgeTemplate('templates/stories/**/*', undefined, resourceKey =>
resourceKey
.replace('templates/', '')
.replace('.handlebars', `.${language}x`),
);

// adjust react-dom dependence
const pkg = await fs.readJSON(
Expand All @@ -98,8 +77,12 @@ const handleTemplateFile = async (

const { modernVersion } = context.config;

const exitAddonsVersion = pkg.devDependencies?.[ADDON_ESSENTIAL];
const isExitStorybook = pkg.devDependencies?.['@modern-js/storybook'];
const exitAddonsVersion =
pkg.devDependencies?.[ADDON_ESSENTIAL] ||
pkg.dependencies?.[ADDON_ESSENTIAL];
const isExitStorybook =
pkg.devDependencies?.[MODERN_STORYBOOK] ||
pkg.dependencies?.[MODERN_STORYBOOK];

const latestVersion = await getPackageVersion(ADDON_ESSENTIAL);
let availableVersion = latestVersion;
Expand Down Expand Up @@ -127,7 +110,7 @@ const handleTemplateFile = async (
[ADDON_ESSENTIAL]: availableVersion,
...(!isExitStorybook
? {
'@modern-js/storybook': modernVersion,
[MODERN_STORYBOOK]: modernVersion,
}
: {}),
};
Expand All @@ -142,6 +125,7 @@ const handleTemplateFile = async (
scripts: {
'build-storybook': 'storybook build',
storybook: 'storybook dev -p 6006',
...(pkg.scripts || {}),
},
},
},
Expand Down Expand Up @@ -176,28 +160,11 @@ export default async (context: GeneratorContext, generator: GeneratorCore) => {
i18n.changeLanguage({ locale });
appApi.i18n.changeLanguage({ locale });

if (!(await appApi.checkEnvironment())) {
// eslint-disable-next-line no-process-exit
process.exit(1);
}

generator.logger.debug(`start run @modern-js/storybook-generator-next`);
generator.logger.debug(`context=${JSON.stringify(context)}`);
generator.logger.debug(`context.data=${JSON.stringify(context.data)}`);

const { packageManager } = await handleTemplateFile(
context,
appApi,
generator,
);

if (context.config.isSubGenerator) {
appApi.showSuccessInfo(
i18n.t(localeKeys.success, {
packageManager: getPackageManagerText(packageManager),
}),
);
}
await handleTemplateFile(context, appApi, generator);

generator.logger.debug(`forge @modern-js/storybook-generator-next succeed `);
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Component = ({ backgroundColor, color }) => (
<button style={{ backgroundColor, color }}>this is a Story Component</button>
<button style=\{{ backgroundColor, color }}>this is a Story Component</button>
);

export default {
Expand Down

This file was deleted.

This file was deleted.

1 change: 1 addition & 0 deletions tests/generator/utils/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ async function addNewActionDevDependence(repoCwd: string) {
'@modern-js/repo-generator': 'workspace:*',
'@modern-js/tailwindcss-generator': 'workspace:*',
'@modern-js/storybook-generator': 'workspace:*',
'@modern-js/storybook-generator-next': 'workspace:*',
'@modern-js/ssg-generator': 'workspace:*',
'@modern-js/module-test-generator': 'workspace:*',
'@modern-js/rspack-generator': 'workspace:*',
Expand Down

0 comments on commit 2f6179f

Please sign in to comment.