How to overwrite file with angular schematics?

前端 未结 5 389
难免孤独
难免孤独 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 06:56

    This may not be ideal if you are replacing many files. I came across this issue replacing the favicon.ico while adding boilerplate to the project. The solution I use is to explicitly delete it first.

    export function scaffold(options: any): Rule {
      return (tree: Tree, _context: SchematicContext) => {
      
        tree.delete('src/favicon.ico');
    
        const templateSource = apply(url('./files'), [
          template({
            ...options,
          }),
          move('.'),
        ]);
    
        return chain([
          branchAndMerge(chain([
            mergeWith(templateSource, MergeStrategy.Overwrite),
          ]), MergeStrategy.AllowOverwriteConflict),
        ])(tree, _context);
      };
    }

    NOTE: This approach no longer works with current versions of Schematics.

    The following seems to work for the 0.6.8 schematics.

    export function scaffold(options: any): Rule {
      return (tree: Tree, _context: SchematicContext) => {
      
        const templateSource = apply(url('./files'), [
          template({
            ...options,
          }),
          move('.'),
        ]);
    
        return chain([
          branchAndMerge(chain([
            mergeWith(templateSource, MergeStrategy.Overwrite),
          ]), MergeStrategy.Overwrite),
        ])(tree, _context);
      };
    }

    As a bonus, I no longer need to explicitly delete the file.

提交回复
热议问题