原理图 - 管理模板文件复制错误
Schematics - Manage template files copy error
我正在设法将原理图 files
目录中的一些文件复制到主项目目标文件夹:
function addTplFiles(path: string): Source {
// copy templates
return apply(url('./files'), [
move(path as string)
]);
}
export function ngAdd(options: ISchema): Rule {
return (host: Tree/*, context: SchematicContext*/) => {
// get the workspace config of the consuming project
// i.e. angular.json file
const workspace = getWorkspace(host);
// identify the project config which is using our library
// or default to the default project in consumer workspace
const project = getProjectFromWorkspace(
workspace,
options.project || workspace.defaultProject
);
const projectType = project.projectType === 'application' ? 'app' : 'lib';
const path = (options.path === undefined) ? `${project.sourceRoot}/${projectType}` : options.path;
const templateSource = addTplFiles(project.sourceRoot || '');
// return updated tree
try {
return chain([
mergeWith(templateSource)
]);
} catch (e) {
return host;
}
};
代码运行良好,除非文件已经在主应用程序项目中:
ERROR! src/assets/i18n/en.json already exists. ERROR!
src/assets/i18n/it.json already exists. The Schematic workflow failed.
See above.
如何捕获和管理此异常?
您有 2 个选择:
- 在原理图命令中使用
--force
选项,强制覆盖所有现有文件。
ng g @custom/my-schematics:rule --force
- 检查文件是否已存在于您的原理图代码中,并在这种情况下应用特定行为。
const templateSource = apply(url('./files'), [
forEach((fileEntry: FileEntry) => {
if (tree.exists(fileEntry.path)) {
console.log('File already exists, but it\'s ok');
return null;
}
return fileEntry;
})
]);
return chain([
mergeWith(templateSource)
]);
我正在设法将原理图 files
目录中的一些文件复制到主项目目标文件夹:
function addTplFiles(path: string): Source {
// copy templates
return apply(url('./files'), [
move(path as string)
]);
}
export function ngAdd(options: ISchema): Rule {
return (host: Tree/*, context: SchematicContext*/) => {
// get the workspace config of the consuming project
// i.e. angular.json file
const workspace = getWorkspace(host);
// identify the project config which is using our library
// or default to the default project in consumer workspace
const project = getProjectFromWorkspace(
workspace,
options.project || workspace.defaultProject
);
const projectType = project.projectType === 'application' ? 'app' : 'lib';
const path = (options.path === undefined) ? `${project.sourceRoot}/${projectType}` : options.path;
const templateSource = addTplFiles(project.sourceRoot || '');
// return updated tree
try {
return chain([
mergeWith(templateSource)
]);
} catch (e) {
return host;
}
};
代码运行良好,除非文件已经在主应用程序项目中:
ERROR! src/assets/i18n/en.json already exists. ERROR! src/assets/i18n/it.json already exists. The Schematic workflow failed. See above.
如何捕获和管理此异常?
您有 2 个选择:
- 在原理图命令中使用
--force
选项,强制覆盖所有现有文件。
ng g @custom/my-schematics:rule --force
- 检查文件是否已存在于您的原理图代码中,并在这种情况下应用特定行为。
const templateSource = apply(url('./files'), [
forEach((fileEntry: FileEntry) => {
if (tree.exists(fileEntry.path)) {
console.log('File already exists, but it\'s ok');
return null;
}
return fileEntry;
})
]);
return chain([
mergeWith(templateSource)
]);