首页 > 解决方案 > Angular Schematics:创建自定义树文件夹和文件

问题描述

我刚刚开始学习 Schematics,我的第一个任务是使用我的Angular应用程序中的自定义示意图生成自定义的文件夹树和空文件

例如 :

我想生成这棵树:

folderOne
|__ fileOne.ts
folderTwo
|__ fileTwo.ts
folderThree
|__ subfolderA
    |__ fileThree.ts

当然应该取决于运行原理图命令的源位置

(相对路径)

有什么快速(模板)的方法吗?

建议??

标签: angular-schematics

解决方案


在您的文件模板中

__fistVariable@dasherize__
|__firstVariable@dasherize__.ts
__secondVariable@dasherize__
|__secondVariable@dasherize__.ts

你的原理图功能:

const templateSource = apply(url("./files"), [
  template({
  ..._options,
  ...strings
  })
]);
return mergeWith(templateSource)(tree,_context);

在你的 schema.json

"properties": {
    "one": {
      "type": "string",
      "description": "Variable 1",
      "$default": {
        "$source": "argv",
        "index": 0
      },
      "minLength": 1,
      "x-prompt": "Enter the entity name (camelCase || dasherized)"
    },
    "two":{
      "type": "string",
      "description": "Variable two",
      "$default": {
        "$source": "argv",
        "index": 1
      },
      "minLength": 1,
      "x-prompt": "Enter the entity name (camelCase || dasherized)"
    }
  },
  "required": ["one", "two"]

推荐阅读