您可以将模板文件放在目录中,加载它们然后复制到目标destination.eg
目录结构:
Project_root
|- tools
|- schematics
|- your_schematics
|- index.ts
|- templates
|- __name@dasherize__.custom.ts
内容:
/* __name@dasherize__.custom.ts */
export class <%= classify(name) %>Custom {
constructor () {}
}
/* index.ts */
import { chain, externalSchematic, Rule, url, apply, move, mergeWith, MergeStrategy, template, SchematicContext } from '@angular-devkit/schematics';
import { dasherize, classify } from '@angular-devkit/core/src/utils/strings';
import { normalize, strings } from '@angular-devkit/core';
export default function(schema: any): Rule {
const libFileName = dasherize(schema.name);
const projectDirectory = schema.directory
? normalize(schema.directory + '/' + libFileName)
: libFileName;
const projectRoot = normalize('libs/' + projectDirectory);
return chain([
externalSchematic('@nrwl/workspace', 'lib', schema),
addCustomFileToLib(schema, projectRoot)
]);
}
function addCustomFileToLib(schema: any, projectRoot: string): Rule {
const templateFiles = url("./templates");
const newTree = apply(templateFiles, [
move(projectRoot),
template({
...strings,
...schema // pass the objects containing the properties & functions to be used in template file
})
]);
return mergeWith(newTree, MergeStrategy.Default);
}
注意:模板文件的名称(__name@dasherize__.custom.ts
)是一个表达式,在执行原理图时将根据从 cli 传递的名称arg 进行编译/更改。