在新的 nx 工作区中创建自定义 .editorconfig、.eslintrc.json 和 .prettierrc

create custom .editorconfig, .eslintrc.json and .prettierrc in new nx workspace

当我从 angular 预设创建新的 nx-workspace 时,我想插入 .editorconfig、.eslintrc.json 和 .prettierrc 的自定义(公司范围)配置,而不是每次都复制它们时间.

有没有办法自定义 create-nx-workspace?

作为后备方案,我会对覆盖上述文件的某种“ng add ...”(或正确的 nx 替代方案)感到满意。

我借此机会调查了 Angular Schematics

我有一个小的“ng-add”原理图,它用我自己的覆盖文件并安装所需的依赖项。

由于这些都是根目录下的文件,原理图规则很简单(所有模板都在文件夹 files 中,如建议的那样):

import {
  Rule,
  SchematicContext,
  Tree,
  apply,
  url,
  mergeWith,
  template,
  MergeStrategy,
} from "@angular-devkit/schematics";

export function ngAdd(options: any): Rule {
  return (tree: Tree, context: SchematicContext) => {
    const templateSource = apply(url("./files"), [template(options)]);
    const merged = mergeWith(templateSource, MergeStrategy.Overwrite);
    return merged(tree, context);
  };
}

因此,在我使用 npx create-nx-workspace --preset=angular 创建工作区后,我可以 nx add my-schematics 并且它具有我需要的形状。