All files nx-project.ts

100% Statements 20/20
78.57% Branches 11/14
100% Functions 6/6
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100    1x 1x 1x 1x           1x             12x                     11x     11x                 1x                   20x 18x           2x             2x       2x 2x 2x         2x     16x                                 20x      
/*! Copyright [Amazon.com](http://amazon.com/), Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0 */
import * as path from "path";
import { Component, JsonFile, Project } from "projen";
import { NodeProject } from "projen/lib/javascript";
import { buildDownloadExecutableCommand } from "./nx-monorepo";
 
/**
 * Component which manged the project specific NX Config and is added to all NXMonorepo subprojects.
 *
 */
export class NxProject extends Component {
  /**
   * Retrieves an instance of NXProject if one is associated to the given project.
   *
   * @param project project instance.
   */
  static of(project: Project): NxProject | undefined {
    return project.components.find((c) => c instanceof NxProject) as
      | NxProject
      | undefined;
  }
 
  /**
   * List of implicit dependencies.
   *
   * @internal
   * @private
   */
  private readonly _implicitDependencies: string[] = [];
 
  constructor(project: Project) {
    super(project);
  }
 
  /**
   * Adds an implicit dependency between the dependant (this project) and dependee.
   *
   * @param dependee project to add the implicit dependency on.
   */
  public addImplicitDependency(dependee: Project | string) {
    this._implicitDependencies.push(
      dependee instanceof Project ? dependee.name : dependee
    );
  }
 
  /**
   * Generate a nx block in the package.json if this is a NodeProject. Otherwise generate a project.json with
   * the relevant NX configuration.
   */
  synthesize() {
    if (this.project instanceof NodeProject) {
      this._implicitDependencies.length > 0 &&
        this.project
          .tryFindObjectFile("package.json")
          ?.addOverride("nx.implicitDependencies", this._implicitDependencies);
    } else {
      const projectJson =
        this.project.tryFindObjectFile("project.json") ||
        new JsonFile(this.project, "project.json", {
          readonly: true,
          marker: true,
          obj: {},
        });
 
      const projectPath = path.relative(
        this.project.root.outdir,
        this.project.outdir
      );
      projectJson.addOverride("name", this.project.name);
      projectJson.addOverride("root", projectPath);
      this._implicitDependencies.length > 0 &&
        projectJson.addOverride(
          "implicitDependencies",
          this._implicitDependencies
        );
      projectJson.addOverride(
        "targets",
        this.project.tasks.all.reduce(
          (p, c) => ({
            [c.name]: {
              executor: "nx:run-commands",
              options: {
                command: `${buildDownloadExecutableCommand(
                  (this.project.root as NodeProject).package.packageManager,
                  `projen ${c.name}`
                )}`,
                cwd: projectPath,
              },
            },
            ...p,
          }),
          {}
        )
      );
    }
    super.synthesize();
  }
}