constructor()

in packages/monorepo/src/components/nx-project/index.ts [88:141]


  constructor(project: Project) {
    // Make sure we only ever have 1 instance of NxProject component per project
    if (NxProject.of(project))
      throw new Error(
        `Project ${project.name} already has associated NxProject component.`
      );

    const _existingFile = project.tryFindObjectFile("project.json");
    if (
      _existingFile &&
      !ProjectUtils.isNamedInstanceOf(_existingFile, JsonFile)
    ) {
      throw new Error(
        `Project "${project.name}" contains a "project.json" file that is not a JsonFile instance. NxProject is unable to support this project.`
      );
    }

    super(project);

    const _obj: Record<keyof Nx.ProjectConfig, () => any> = {
      name: () => this.project.name,
      root: () => path.relative(this.project.root.outdir, this.project.outdir),
      namedInputs: () => asUndefinedIfEmpty(this.namedInputs),
      targets: () => asUndefinedIfEmpty(this.targets),
      tags: () => asUndefinedIfEmpty(this.tags),
      implicitDependencies: () => asUndefinedIfEmpty(this.implicitDependencies),
      includedScripts: () => asUndefinedIfEmpty(this.includedScripts),
    };

    this.file =
      (_existingFile as JsonFile) ||
      new JsonFile(project, "project.json", {
        readonly: true,
        marker: true,
        obj: _obj,
      });

    if (_existingFile) {
      project.logger.warn(
        `[NxProject] Project "${
          project.name
        }" defined independent project.json file, which might conflict with NxProject managed properties [${Object.keys(
          _obj
        ).join(",")}]`
      );
      Object.entries(_obj).forEach(([key, value]) => {
        _existingFile.addOverride(key, value);
      });
    }

    if (NxWorkspace.of(project)?.autoInferProjectTargets) {
      this.inferTargets();
    }
  }