我想写,每次覆盖文件的Rule
。在下面的并具有MergeStrategy
设置为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 {
}
我跑schematics .:function --name=test --dry-run=false
我得到
CREATE /src/app/test.ts(33个字节)
但随后,第二次。
错误! /src/app/test.ts已经存在。
如果它不覆盖与出错误的文件test.ts?
编辑:
所有的答案的工作,是伟大的,但似乎他们是基于偏好/自以为是的变通方法,并没有明显的“正确答案”,并可能。所以不知道如何标记为已回答。
我经历了同样的问题。偶然我发现,添加forEach
到apply
允许的要删除的文件,并创建新的文件。这与@角的devkit /原理图-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);
};
}
修改克里斯的回答,我能想出以下解决方案:
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);
};
}
与调用该函数替换您apply
的使用。
applyWithOverwrite(url('./files/stylelint'), [
template({
dot: '.',
}),
]),
如果您要更换许多文件时,这可能不是很理想。我遇到了这个问题,在更换favicon.ico的同时添加样板到项目中。我使用的解决方案是第一个明确地将其删除。
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);
};
}
注意:此方法不再与原理图的当前版本的作品。
以下似乎为0.6.8原理工作。
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);
};
}
作为奖励,我不再需要显式删除该文件。
感谢@cgatian该提示!
遗憾的是它没有努力move
模板到某个位置,所以我不得不添加:
forEach(fileEntry => {
const destPath = join(options.output, fileEntry.path);
if (tree.exists(destPath)) {
tree.overwrite(destPath, fileEntry.content);
} else {
tree.create(destPath, fileEntry.content);
}
return null;
})
直到MergeStrategy
按预期工作,
这将这样的伎俩传递目的地路径options.output
!
再次感谢!