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);
}

相关内容

热门资讯

据相关数据显示!边锋老友修改器... 据相关数据显示!边锋老友修改器,切实是真的辅助平台(有挂细节)-哔哩哔哩1、操作简单,无需边锋老友修...
长期以来!悠闲卡五星辅助器,真... 长期以来!悠闲卡五星辅助器,真是存在有辅助插件(真实有挂)-哔哩哔哩1)悠闲卡五星辅助器有没有挂:进...
更值得关注的是!围棋辅助器乐乐... 更值得关注的是!围棋辅助器乐乐,果然是有辅助器(真的有挂)-哔哩哔哩1、完成围棋辅助器乐乐辅助器v3...
迎来新发展!情怀游戏字牌辅助,... 迎来新发展!情怀游戏字牌辅助,都是有挂辅助插件(有挂详细)-哔哩哔哩1、情怀游戏字牌辅助透视辅助软件...
现场直击!哥哥打大a有没有辅助... 现场直击!哥哥打大a有没有辅助,确实有挂辅助软件(有挂猫腻)-哔哩哔哩1、让任何用户在无需哥哥打大a...
最终!好友赣南新版本辅助,都是... 最终!好友赣南新版本辅助,都是有挂辅助插件(新版有挂)-哔哩哔哩1、好友赣南新版本辅助免费脚本咨询教...
据报道!奇迹陕西游戏辅助器,确... 您好,奇迹陕西游戏辅助器这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多...
做出回应!雀姬辅助器,一贯真的... 做出回应!雀姬辅助器,一贯真的是有辅助工具(有挂实锤)-哔哩哔哩1、让任何用户在无需雀姬辅助器安装教...
最终!潮友会鱼虾蟹塞子概率计算... 最终!潮友会鱼虾蟹塞子概率计算方式,好像真的是有辅助脚本(有挂透视)-哔哩哔哩潮友会鱼虾蟹塞子概率计...
事发当天!德州扑克游戏辅助器,... 事发当天!德州扑克游戏辅助器,总是存在有辅助软件(有挂猫腻)-哔哩哔哩1、很好的工具软件,可以解锁游...