AngularStorybook-有没有一种自动生成.stories.ts文件的方法?
创始人
2024-10-29 16:30:38
0

使用Angular Schematics可以自动生成Angular组件和storybook配置,从而自动生成.story.ts文件。下面是一些示例代码:

首先,安装Angular Schematics:

npm install -g @angular-devkit/schematics-cli

接着,使用以下命令生成一个新的Schematic项目:

schematics blank ../my-schematic

然后,在你的项目中添加Angular Schematics依赖:

npm install @storybook/angular @angular/cdk @ng-packagr/core

现在我们需要编写我们的schematic,用来生成以下文件:

  • src/app/components/my-component/my-component.component.ts
  • src/app/components/my-component/my-component.component.html
  • src/app/components/my-component/my-component.component.scss
  • src/app/components/my-component/my-component.component.spec.ts
  • src/app/components/my-component/my-component.stories.ts

以下是示例代码:

import { Rule, SchematicContext, Tree, chain, url, apply, move, mergeWith, applyTemplates } from '@angular-devkit/schematics';
import { strings } from '@angular-devkit/core';
import { normalize } from 'path';

// You don't have to export the function as default. You can also have more than one rule factory
// per file.
export function mySchematic(_options: any): Rule {
  return (tree: Tree, _context: SchematicContext) => {
    return chain([
      generateComponent(_options),
      generateStorybook(_options)
    ])(tree, _context);
  };
}

function generateComponent(options: any): Rule {
  const templateSource = apply(url('./files'), [
    applyTemplates({
      ...strings,
      ...options,
    }),
    move(
      normalize(`src/app/components/${options.name}`)
    )
  ]);

  return mergeWith(templateSource);
}

function generateStorybook(options: any): Rule {
  const templateSource = apply(url('./files'), [
    applyTemplates({
      ...strings,
      ...options,
    }),
    move(
      normalize(`src/app/components/${options.name}`)
    )
  ]);

  return mergeWith(templateSource);
}

相关内容

热门资讯

WePoKe透视挂!红龙扑克模... WePoKe透视挂!红龙扑克模拟器,(aa poker)原来存在有挂(详细透视力荐教程),支持语音通...
透视免费(德州免费辅助神器ap... 透视免费(德州免费辅助神器app)wepokeai代打(详细辅助微扑克教程)本来真的有挂1、在德州免...
透视好友房(wepOkE)外挂... 透视好友房(wepOkE)外挂透明挂辅助代打(wepoke模拟器)一直存在有挂(详细透视攻略方法);...
wepokeai代打!德扑之星... wepokeai代打!德扑之星记分牌有什么用,(aa poker)真是真的有挂(详细辅助科技教程);...
透视数据(德州ai机器人)nz... 透视数据(德州ai机器人)nzt德州辅助(详细辅助切实教程)都是真的是有挂1、许多玩家不知道德州ai...
透视苹果版(wePoke)透视... 1、透视苹果版(wePoke)透视辅助安装(wepok软件透明挂)确实是有挂(详细透视高科技教程)(...
wpk辅助挂!德州辅助软件开发... wpk辅助挂!德州辅助软件开发定制,(wePOke)一向真的是有挂(详细辅助插件教程);wpk辅助挂...
透视辅助(微扑克ai辅助)微扑... 透视辅助(微扑克ai辅助)微扑克ai辅助器苹果版(详细辅助黑科技教程)一直存在有挂是一款可以让一直输...
透视中牌率(WePoke)外挂... 1、透视中牌率(WePoke)外挂透明挂辅助器(wepoke一定有挂)原生真的是有挂(详细透视202...
wepoke黑科技!轰趴大菠萝... wepoke黑科技!轰趴大菠萝辅助器,(wEpOke)起初存在有挂(详细透视透视教程);人气非常高,...