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分钟讲义!wepoker有... 第1分钟讲义!wepoker有辅助工具吗(透视)其实是有辅助插件(哔哩哔哩)1、下载好wepoker...
第7分钟安装!微信小程序十三张... 第7分钟安装!微信小程序十三张脚本,樱花之盛辅助软件(原来存在有辅助插件)-哔哩哔哩1、下载好微信小...
第4分钟资料!wepoker俱... 第4分钟资料!wepoker俱乐部辅助器(透视)原来有辅助工具(哔哩哔哩)暗藏猫腻,小编详细说明we...
十分钟插件!拱趴大菠萝辅助公户... 十分钟插件!拱趴大菠萝辅助公户,开心游戏攻略(都是是有辅助软件)-哔哩哔哩1、玩家可以在拱趴大菠萝辅...
三分钟烘培!pokemmo辅助... 三分钟烘培!pokemmo辅助器(透视)一贯是有辅助开挂(哔哩哔哩)1、实时pokemmo辅助器透视...
第六分钟安装!丽水茶苑苹果手机... 第六分钟安装!丽水茶苑苹果手机辅助,花花生活圈脚本(果然存在有辅助插件)-哔哩哔哩1、丽水茶苑苹果手...
第7分钟诀窍!德普之星透视软件... 第7分钟诀窍!德普之星透视软件免费入口官网(透视)一直是有辅助工具(哔哩哔哩)1、下载好德普之星透视...
3分钟脚本!新星游辅助真的假的... 3分钟脚本!新星游辅助真的假的,乐平包王讨赏辅助(总是真的是有辅助修改器)-哔哩哔哩1、新星游辅助真...
第5分钟讲义!hhpoker软... 第5分钟讲义!hhpoker软件可以玩吗(透视)本来有辅助插件(哔哩哔哩)1、下载好hhpoker软...
四分钟脚本!创思维激k辅助器,... 四分钟脚本!创思维激k辅助器,扣点点脚本辅助器(本来是真的辅助安装)-哔哩哔哩1、打开软件启动之后找...