How to overwrite file with angular schematics?

前端 未结 5 401
难免孤独
难免孤独 2020-12-31 06:31

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

coll

5条回答
  •  孤独总比滥情好
    2020-12-31 07:00

    Modifying chris' answer I was able to come up with the following solution:

    export function applyWithOverwrite(source: Source, rules: Rule[]): Rule {
      return (tree: Tree, _context: SchematicContext) => {
        const rule = mergeWith(
          apply(source, [
            ...rules,
            forEach((fileEntry) => {
              if (tree.exists(fileEntry.path)) {
                tree.overwrite(fileEntry.path, fileEntry.content);
                return null;
              }
              return fileEntry;
            }),
    
          ]),
        );
    
        return rule(tree, _context);
      };
    }
    

    Replace your usage of apply with calls to this function.

     applyWithOverwrite(url('./files/stylelint'), [
            template({
              dot: '.',
            }),
          ]),
    

提交回复
热议问题