【问题标题】:Schematics - Manage template files copy errorSchematics - 管理模板文件复制错误
【发布时间】:2020-06-23 17:03:40
【问题描述】:

我正在设法将一些文件从原理图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;
    }
  };

代码运行良好,除非文件已经在主应用项目中:

错误! src/assets/i18n/en.json 已经存在。错误! src/assets/i18n/it.json 已经存在。原理图工作流程失败。 见上文。

我怎样才能捕捉和管理这个异常?

【问题讨论】:

    标签: angular angular-schematics


    【解决方案1】:

    你有两个选择:

    • 在您的原理图命令中使用--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)
    ]);
    

    【讨论】:

      猜你喜欢
      • 2012-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-09
      • 2020-06-17
      • 2014-12-10
      • 1970-01-01
      • 2015-09-26
      相关资源
      最近更新 更多