如何用角度原理图覆盖文件? [英] How to overwrite file with angular schematics?

查看:20
本文介绍了如何用角度原理图覆盖文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个 Rule 每次都覆盖一个文件.在下面并将 MergeStrategy 设置为 Overwrite:

collection.json

<代码>{"$schema": "../node_modules/@angular-devkit/schematics/collection-schema.json",示意图":{功能": {别名":[fn"],"工厂": "./function","description": "创建一个函数.",架构":./function/schema.json"}}}

function/index.ts

导出默认函数(选项:FunctionOptions):规则{options.path = options.path ?规范化(选项.路径):选项.路径;const sourceDir = options.sourceDir;如果 (!sourceDir) {throw new SchematicsException(`sourceDir 选项是必需的.`);}const 模板源:源 = 应用(url('./文件'),[模板({...字符串,...选项,}),移动(源目录),])返回 mergeWith(templateSource, MergeStrategy.Overwrite);}

files/__path__/__name@dasherize__.ts

export function <%= camelize(name) %>(): void {}

我运行 schematics .:function --name=test --dry-run=false 我得到 <块引用>

创建/src/app/test.ts(33 字节)

但是,第二次.

<块引用>

错误!/src/app/test.ts 已经存在.

它不应该在没有错误的情况下覆盖文件 test.ts 吗?

所有答案都有效并且很棒,但似乎它们是解决方法,没有明显的正确"答案,可能基于偏好/自以为是.所以不确定如何标记为已回答.

解决方案

我遇到了同样的问题.我偶然发现将 forEach 添加到 apply 允许删除文件并创建新文件.这是@angular-devkit/schematics-cli@0.6.8.

export function indexPage(options: any): Rule {返回(树:树,_context:SchematicContext)=>{const 规则 = 合并(申请(网址('./文件'),[模板({ ...选项}),forEach((fileEntry: FileEntry) => {//仅通过添加 this 允许文件已存在时被覆盖if (tree.exists(fileEntry.path)) 返回 null;返回文件条目;})]));返回规则(树,_context);};}

I want to write a Rule that overwrites a file every time. In the following and have MergeStrategy set to Overwrite:

collection.json

{
  "$schema": "../node_modules/@angular-devkit/schematics/collection-schema.json",
  "schematics": {
    "function": {
      "aliases": [ "fn" ],
      "factory": "./function",
      "description": "Create a function.",
      "schema": "./function/schema.json"
    }
  }
}

function/index.ts

export default function(options: FunctionOptions): Rule {
  options.path = options.path ? normalize(options.path) : options.path;
  const sourceDir = options.sourceDir;
  if (!sourceDir) {
    throw new SchematicsException(`sourceDir option is required.`);
  }

  const templateSource: Source = apply(
    url('./files'),
    [
      template({
        ...strings,
        ...options,
      }),
      move(sourceDir),
    ]
  )

  return mergeWith(templateSource, MergeStrategy.Overwrite);

}

files/__path__/__name@dasherize__.ts

export function <%= camelize(name) %>(): void {
}

I run schematics .:function --name=test --dry-run=false I get

CREATE /src/app/test.ts (33 bytes)

but then, the second time.

ERROR! /src/app/test.ts already exists.

Should it not overwrite the file test.ts with out error?

Edit:

All of the answers work and are great but it seems they are workarounds and no obvious "right" answer and possibly based on preference / opinionated. So not sure how to mark as answered.

解决方案

I experienced the same issue. By accident I found that adding a forEach into the apply allowed the files to be deleted and the new files created. This is with @angular-devkit/schematics-cli@0.6.8.

export function indexPage(options: any): Rule {
    return (tree: Tree, _context: SchematicContext) => {
        const rule = mergeWith(
            apply(url('./files'), [
                template({ ...options }),
                forEach((fileEntry: FileEntry) => {
                    // Just by adding this is allows the file to be overwritten if it already exists
                    if (tree.exists(fileEntry.path)) return null;
                    return fileEntry;
                })

            ])
        );

        return rule(tree, _context);
    };
}

这篇关于如何用角度原理图覆盖文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆