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

相关内容

热门资讯

5分钟讲解!九九山城万州版辅助... 5分钟讲解!九九山城万州版辅助器(辅助挂)一直真的有挂(AI教程开挂辅助平台)【无需打开直接搜索加薇...
第九分钟透视!新上游辅助(辅助... 第九分钟透视!新上游辅助(辅助挂)一贯有挂(透明教程开挂辅助神器) 了解更多开挂安装加(136704...
两分钟详情!衢州都莱罗松怎么老... 两分钟详情!衢州都莱罗松怎么老是输(辅助挂)一贯真的是有挂(教你攻略开挂辅助神器);无需打开直接搜索...
两分钟明白!科乐游戏辅助脚本(... 两分钟明白!科乐游戏辅助脚本(辅助挂)其实有挂(必胜教程开挂辅助脚本)您好:科乐游戏辅助脚本这款游戏...
第1分钟辅助!拱趴大菠萝辅助器... 第1分钟辅助!拱趴大菠萝辅助器最新更新内容介绍(辅助挂)一贯有挂(系统教程开挂辅助平台)您好:拱趴大...
8分钟讲解!中至上饶里的祈福有... >>您好:中至上饶里的祈福有用吗确实是有挂的,很多玩家在这款中至上饶里的祈福有用吗游戏中打牌都会发现...
第3分钟发现!唯思竞技辅助器(... 大家好,今天小编来为大家解答唯思竞技辅助器这个问题咨询软件客服可以免费测试直接加微信(1367043...
四分钟发现!点点长牌辅助工具教... 四分钟发现!点点长牌辅助工具教程(辅助挂)原来确实有挂(攻略方法开挂辅助脚本);无需打开直接搜索打开...
第1分钟明白!靖边打锅子奇迹陕... 【亲,靖边打锅子奇迹陕西辅助下载 这款游戏可以开挂的,确实是有挂的,很多玩家在这款靖边打锅子奇迹陕西...
第六分钟了解!中至江西源代码(... 第六分钟了解!中至江西源代码(辅助挂)一直真的是有挂(AI教程开挂辅助脚本);无需打开直接搜索薇:1...