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.
--force
flag to enforce overwriting files, like so:schematics .:function --name=test --dry-run=false --force
. BTW: What did you import for...strings
in template function? – PortamentoMergeStrategy.Overwrite
then, if that is not the behavior then what is the point/meaning ofMergeStrategy.Overwrite
. – SeidelMergeStrategy.Overwrite
, now I agree it should totally work. At least there was once an issue for it in the archived angular/dev-kit project: github.com/angular/devkit/issues/745 – Portamentoimport { strings } from '@angular-devkit/core';
– Seidelschematics .:function --name=test --dry-run=false --force
– Tonometer