All files nx-monorepo.ts

86.77% Statements 105/121
75.92% Branches 123/162
87.87% Functions 29/33
86.77% Lines 105/121

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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619    1x 1x 1x                   1x         1x 1x       1x   1x                                                                         1x             1x             1x                                                                                                                                                                                                                                                                                               1x     10x                   10x                                           10x 10x 10x 10x     10x     10x 10x   10x 10x 10x   10x 10x   10x     10x     10x 10x 10x   10x             10x     10x               10x         10x                                                                                     1x     1x                       13x 14x   13x 13x   14x         13x                                   59x 59x       46x 24x               10x 9x 9x 9x       9x 9x 9x 8x 8x   8x       9x     9x                             10x   10x   10x       1x                     9x 9x 1x     1x 1x       1x 1x 1x 11x           1x   1x                         9x   9x   9x 9x 9x 8x 121x           1x     9x           9x 2x             7x                           9x 9x     9x   8x         1x   1x   1x   1x 3x     1x           1x 1x   1x         1x                         3x 3x                                         28x       10x    
/*! Copyright [Amazon.com](http://amazon.com/), Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0 */
import * as fs from "fs";
import * as path from "path";
import {
  DependencyType,
  IgnoreFile,
  JsonFile,
  Project,
  Task,
  TaskStep,
  TextFile,
  YamlFile,
} from "projen";
import {
  NodePackage,
  NodePackageManager,
  NodeProject,
} from "projen/lib/javascript";
import { PythonProject } from "projen/lib/python";
import {
  TypeScriptProject,
  TypeScriptProjectOptions,
} from "projen/lib/typescript";
import { DEFAULT_CONFIG, SyncpackConfig } from "./syncpack-options";
 
const NX_MONOREPO_PLUGIN_PATH: string = ".nx/plugins/nx-monorepo-plugin.js";
 
/**
 * Configuration for nx targetDependencies.
 */
export type TargetDependencies = { [target: string]: TargetDependency[] };
 
/**
 * Configuration for project specific targets.
 */
export type ProjectTargets = { [target: string]: ProjectTarget };
 
/**
 * Project Target.
 */
export interface ProjectTarget {
  /**
   * List of outputs to cache, relative to the root of the monorepo.
   *
   * note: must start with leading /
   */
  readonly outputs?: string[];
 
  /**
   * List of Target Dependencies.
   */
  readonly dependsOn: TargetDependency[];
}
 
/**
 * Implicit Dependencies map.
 */
export type ImplicitDependencies = { [pkg: string]: string[] };
 
/**
 * Supported enums for a TargetDependency.
 */
export enum TargetDependencyProject {
  /**
   * Only rely on the package where the target is called.
   *
   * This is usually done for test like targets where you only want to run unit
   * tests on the target packages without testing all dependent packages.
   */
  SELF = "self",
  /**
   * Target relies on executing the target against all dependencies first.
   *
   * This is usually done for build like targets where you want to build all
   * dependant projects first.
   */
  DEPENDENCIES = "dependencies",
}
 
/**
 * Represents an NX Target Dependency.
 */
export interface TargetDependency {
  /**
   * Projen target i.e: build, test, etc
   */
  readonly target: string;
 
  /**
   * Target dependencies.
   */
  readonly projects: TargetDependencyProject;
}
 
/**
 * NX configurations.
 *
 * @link https://nx.dev/configuration/packagejson
 */
export interface NXConfig {
  /**
   * Affected branch.
   *
   * @default mainline
   */
  readonly affectedBranch?: string;
  /**
   * Configuration for Implicit Dependnecies.
   *
   * @link https://nx.dev/configuration/packagejson#implicitdependencies
   */
  readonly implicitDependencies?: ImplicitDependencies;
 
  /**
   * Configuration for TargetDependencies.
   *
   * @link https://nx.dev/configuration/packagejson#target-dependencies
   */
  readonly targetDependencies?: TargetDependencies;
 
  /**
   * List of patterns to include in the .nxignore file.
   *
   * @link https://nx.dev/configuration/packagejson#nxignore
   */
  readonly nxIgnore?: string[];
 
  /**
   * Read only access token if enabling nx cloud.
   */
  readonly nxCloudReadOnlyAccessToken?: string;
}
 
/**
 * Workspace configurations.
 *
 * @link https://classic.yarnpkg.com/lang/en/docs/workspaces/
 */
export interface WorkspaceConfig {
  /**
   * List of package globs to exclude from hoisting in the workspace.
   *
   * @link https://classic.yarnpkg.com/blog/2018/02/15/nohoist/
   */
  readonly noHoist?: string[];
 
  /**
   * Disable automatically applying `noHoist` logic for all sub-project "bundledDependencies".
   *
   * @default false
   */
  readonly disableNoHoistBundled?: boolean;
 
  /**
   * List of additional package globs to include in the workspace.
   *
   * All packages which are parented by the monorepo are automatically added to the workspace, but you can use this
   * property to specify any additional paths to packages which may not be managed by projen.
   */
  readonly additionalPackages?: string[];
}
 
/**
 * Configuration for Monorepo Upgrade Deps task.
 */
export interface MonorepoUpgradeDepsOptions {
  /**
   * Name of the task to create.
   *
   * @default upgrade-deps
   */
  readonly taskName?: string;
 
  /**
   * Syncpack configuration.
   *
   * No merging is performed and as such a complete syncpackConfig is required if supplied.
   *
   * @default SyncpackConfig.DEFAULT_CONFIG
   */
  readonly syncpackConfig?: SyncpackConfig;
}
 
/**
 * Configuration options for the NxMonorepoProject.
 */
export interface NxMonorepoProjectOptions extends TypeScriptProjectOptions {
  /**
   * Configuration for NX.
   */
  readonly nxConfig?: NXConfig;
 
  /**
   * Configuration for workspace.
   */
  readonly workspaceConfig?: WorkspaceConfig;
 
  /**
   * Whether to include an upgrade-deps task at the root of the monorepo which will upgrade all dependencies.
   *
   * @default true
   */
  readonly monorepoUpgradeDeps?: boolean;
 
  /**
   * Monorepo Upgrade Deps options.
   *
   * This is only used if monorepoUpgradeDeps is true.
   *
   * @default undefined
   */
  readonly monorepoUpgradeDepsOptions?: MonorepoUpgradeDepsOptions;
}
 
/**
 * This project type will bootstrap a NX based monorepo with support for polygot
 * builds, build caching, dependency graph visualization and much more.
 *
 * @pjid nx-monorepo
 */
export class NxMonorepoProject extends TypeScriptProject {
  // mutable data structures
  private readonly implicitDependencies: ImplicitDependencies;
  private readonly targetOverrides: { [pkg: string]: ProjectTargets } = {};
 
  // immutable data structures
  private readonly nxConfig?: NXConfig;
  private readonly workspaceConfig?: WorkspaceConfig;
  private readonly workspacePackages: string[];
 
  private readonly nxJson: JsonFile;
 
  constructor(options: NxMonorepoProjectOptions) {
    super({
      ...options,
      github: options.github ?? false,
      package: options.package ?? false,
      prettier: options.prettier ?? true,
      projenrcTs: true,
      release: options.release ?? false,
      jest: options.jest ?? false,
      defaultReleaseBranch: options.defaultReleaseBranch ?? "mainline",
      sampleCode: false, // root should never have sample code,
      eslintOptions: options.eslintOptions ?? {
        dirs: ["."],
        ignorePatterns: ["packages/**/*.*"],
      },
      tsconfig: options.tsconfig ?? {
        compilerOptions: {
          rootDir: ".",
        },
        include: ["**/*.ts"],
      },
    });
 
    this.nxConfig = options.nxConfig;
    this.workspaceConfig = options.workspaceConfig;
    this.workspacePackages = options.workspaceConfig?.additionalPackages ?? [];
    this.implicitDependencies = this.nxConfig?.implicitDependencies || {};
 
    // Never publish a monorepo root package.
    this.package.addField("private", true);
 
    // No need to compile or test a monorepo root package.
    this.compileTask.reset();
    this.testTask.reset();
 
    this.addDevDeps("@nrwl/cli", "@nrwl/workspace");
    this.addDeps("aws-cdk-lib", "constructs", "cdk-nag"); // Needed as this can be bundled in aws-prototyping-sdk
    this.package.addPackageResolutions("@types/babel__traverse@7.18.2");
 
    if (options.monorepoUpgradeDeps !== false) {
      this.addDevDeps("npm-check-updates", "syncpack");
 
      const upgradeDepsTask = this.addTask(
        options.monorepoUpgradeDepsOptions?.taskName || "upgrade-deps"
      );
      upgradeDepsTask.exec(
        "npx npm-check-updates --deep --rejectVersion 0.0.0 -u"
      );
      upgradeDepsTask.exec("npx syncpack fix-mismatches");
      upgradeDepsTask.exec(`${this.package.packageManager} install`);
      upgradeDepsTask.exec("npx projen");
 
      new JsonFile(this, ".syncpackrc.json", {
        obj:
          options.monorepoUpgradeDepsOptions?.syncpackConfig || DEFAULT_CONFIG,
        readonly: true,
      });
    }
 
    options.nxConfig?.nxCloudReadOnlyAccessToken &&
      this.addDevDeps("@nrwl/nx-cloud");
 
    new IgnoreFile(this, ".nxignore").exclude(
      "test-reports",
      "target",
      ".env",
      ".pytest_cache",
      ...(this.nxConfig?.nxIgnore || [])
    );
 
    new TextFile(this, NX_MONOREPO_PLUGIN_PATH, {
      readonly: true,
      lines: fs.readFileSync(getPluginPath()).toString("utf-8").split("\n"),
    });
 
    this.nxJson = new JsonFile(this, "nx.json", {
      obj: {
        extends: "@nrwl/workspace/presets/npm.json",
        plugins: [`./${NX_MONOREPO_PLUGIN_PATH}`],
        npmScope: "monorepo",
        tasksRunnerOptions: {
          default: {
            runner: options.nxConfig?.nxCloudReadOnlyAccessToken
              ? "@nrwl/nx-cloud"
              : "@nrwl/workspace/tasks-runners/default",
            options: {
              useDaemonProcess: false,
              cacheableOperations: ["build", "test"],
              accessToken: options.nxConfig?.nxCloudReadOnlyAccessToken,
            },
          },
        },
        implicitDependencies: this.implicitDependencies,
        targetDependencies: {
          build: [
            {
              target: "build",
              projects: "dependencies",
            },
          ],
          ...(this.nxConfig?.targetDependencies || {}),
        },
        affected: {
          defaultBase: this.nxConfig?.affectedBranch || "mainline",
        },
      },
    });
  }
 
  /**
   * Create an implicit dependency between two Project's. This is typically
   * used in polygot repos where a Typescript project wants a build dependency
   * on a Python project as an example.
   *
   * @param dependent project you want to have the dependency.
   * @param dependee project you wish to depend on.
   */
  public addImplicitDependency(dependent: Project, dependee: Project) {
    Iif (this.implicitDependencies[dependent.name]) {
      this.implicitDependencies[dependent.name].push(dependee.name);
    } else {
      this.implicitDependencies[dependent.name] = [dependee.name];
    }
  }
 
  /**
   * Add one or more additional package globs to the workspace.
   * @param packageGlobs paths to the package to include in the workspace (for example packages/my-package)
   */
  public addWorkspacePackages(...packageGlobs: string[]) {
    // Any subprojects that were added since the last call to this method need to be added first, in order to ensure
    // we add the workspace packages in a sane order.
    const relativeSubProjectWorkspacePackages =
      this.instantiationOrderSubProjects.map((project) =>
        path.relative(this.outdir, project.outdir)
      );
    const existingWorkspacePackages = new Set(this.workspacePackages);
    this.workspacePackages.push(
      ...relativeSubProjectWorkspacePackages.filter(
        (pkg) => !existingWorkspacePackages.has(pkg)
      )
    );
 
    // Add the additional packages next
    this.workspacePackages.push(...packageGlobs);
  }
 
  /**
   * Allow project specific target overrides.
   */
  public overrideProjectTargets(project: Project, targets: ProjectTargets) {
    const _package = project.tryFindObjectFile("package.json");
    _package?.addOverride("nx", {
      targets: targets,
    });
 
    !_package && (this.targetOverrides[project.outdir] = targets);
  }
 
  // Remove this hack once subProjects is made public in Projen
  private get instantiationOrderSubProjects(): Project[] {
    // @ts-ignore
    const subProjects: Project[] = this.subprojects || [];
    return subProjects;
  }
 
  public get subProjects(): Project[] {
    return this.instantiationOrderSubProjects.sort((a, b) =>
      a.name.localeCompare(b.name)
    );
  }
 
  /**
   * @inheritDoc
   */
  synth() {
    this.validateSubProjects();
    this.updateWorkspace();
    this.wirePythonDependencies();
    this.synthesizeNonNodePackageJson();
 
    // Prevent sub NodeProject packages from `postSynthesis` which will cause individual/extraneous installs.
    // The workspace package install will handle all the sub NodeProject packages automatically.
    const subProjectPackages: NodePackage[] = [];
    this.subProjects.forEach((subProject) => {
      if (isNodeProject(subProject)) {
        const subNodeProject: NodeProject = subProject as NodeProject;
        subProjectPackages.push(subNodeProject.package);
        // @ts-ignore - `installDependencies` is private
        subNodeProject.package.installDependencies = () => {};
      }
    });
 
    super.synth();
 
    // Force workspace install deps if any node subproject package has change, unless the workspace changed
    Iif (
      // @ts-ignore - `file` is private
      (this.package.file as JsonFile).changed !== true &&
      // @ts-ignore - `file` is private
      subProjectPackages.find((pkg) => (pkg.file as JsonFile).changed === true)
    ) {
      // @ts-ignore - `installDependencies` is private
      this.package.installDependencies();
    }
  }
 
  /**
   * Ensures subprojects don't have a default task and that all packages use the same package manager.
   */
  private validateSubProjects() {
    this.subProjects.forEach((subProject: any) => {
      // Disable default task on subprojects as this isn't supported in a monorepo
      subProject.defaultTask?.reset();
 
      if (
        isNodeProject(subProject) &&
        subProject.package.packageManager !== this.package.packageManager
      ) {
        throw new Error(
          `${subProject.name} packageManager does not match the monorepo packageManager: ${this.package.packageManager}.`
        );
      }
    });
  }
 
  /**
   * For non-node projects, a package.json is required in order to be discovered by NX.
   */
  private synthesizeNonNodePackageJson() {
    this.subProjects
      .filter((subProject: any) => !isNodeProject(subProject))
      .filter((subProject: Project) => !subProject.tryFindFile("package.json"))
      .forEach((subProject: Project) => {
        // generate a package.json if not found
        const manifest: any = {};
        (manifest.nx = this.targetOverrides[subProject.outdir]
          ? { targets: this.targetOverrides[subProject.outdir] }
          : undefined),
          (manifest.name = subProject.name);
        manifest.private = true;
        manifest.__pdk__ = true;
        manifest.scripts = subProject.tasks.all.reduce(
          (p, c) => ({
            [c.name]: `npx projen ${c.name}`,
            ...p,
          }),
          {}
        );
        manifest.version = "0.0.0";
 
        new JsonFile(subProject, "package.json", {
          obj: manifest,
          readonly: true,
        });
      });
  }
 
  /**
   * Add a submodule entry to the appropriate workspace file.
   */
  private updateWorkspace() {
    // A final call to addWorkspacePackages will update the list of workspace packages with any subprojects that have
    // not yet been added, in the correct order
    this.addWorkspacePackages();
 
    let noHoist = this.workspaceConfig?.noHoist;
    // Automatically add all sub-project "bundledDependencies" to workspace "hohoist", otherwise they are not bundled in npm package
    if (this.workspaceConfig?.disableNoHoistBundled !== true) {
      const noHoistBundled = this.subProjects.flatMap((sub) => {
        if (sub instanceof NodeProject) {
          return sub.deps.all
            .filter((dep) => dep.type === DependencyType.BUNDLED)
            .flatMap((dep) => [
              `${sub.name}/${dep.name}`,
              `${sub.name}/${dep.name}/*`,
            ]);
        }
        return [];
      });
 
      Iif (noHoistBundled.length) {
        noHoist = [...(noHoist || []), ...noHoistBundled];
      }
    }
 
    // Add workspaces for each subproject
    if (this.package.packageManager === NodePackageManager.PNPM) {
      new YamlFile(this, "pnpm-workspace.yaml", {
        readonly: true,
        obj: {
          packages: this.workspacePackages,
        },
      });
    } else {
      this.package.addField("workspaces", {
        packages: this.workspacePackages,
        nohoist: noHoist,
      });
    }
  }
 
  /**
   * Updates the install task for python projects so that they are run serially and in dependency order such that python
   * projects within the monorepo can declare dependencies on one another.
   * @private
   */
  private wirePythonDependencies() {
    // Find any python projects
    const pythonProjects = this.subProjects.filter(
      (project) => project instanceof PythonProject
    ) as PythonProject[];
 
    if (pythonProjects.length === 0) {
      // Nothing to do for no python projects
      return;
    }
 
    // Move all install commands to install-py so that they are not installed in parallel by the monorepo package manager.
    // eg yarn install will run the install task for all packages in parallel which can lead to conflicts for python.
    pythonProjects.forEach((pythonProject) => {
      const installPyTask =
        pythonProject.tasks.tryFind("install-py") ??
        pythonProject.addTask("install-py");
      const installTask = pythonProject.tasks.tryFind("install");
 
      (installTask?.steps || []).forEach((step) => {
        this.copyStepIntoTask(step, installPyTask, pythonProject);
      });
 
      installTask?.reset();
    });
 
    // Add an install task to the monorepo to include running the install-py command serially to avoid conflicting writes
    // to a shared virtual env. This is also managed by nx so that installs occur in dependency order.
    const monorepoInstallTask =
      this.tasks.tryFind("install") ?? this.addTask("install");
    monorepoInstallTask.exec(
      `npx nx run-many --target install-py --projects ${pythonProjects
        .map((project) => project.name)
        .join(",")} --parallel=1`
    );
 
    // Update the nx.json to ensure that install-py follows dependency order
    this.nxJson.addOverride("targetDependencies.install-py", [
      {
        target: "install-py",
        projects: "dependencies",
      },
    ]);
  }
 
  /**
   * Copies the given step into the given task. Step and Task must be from the given Project
   * @private
   */
  private copyStepIntoTask(step: TaskStep, task: Task, project: Project) {
    if (step.exec) {
      task.exec(step.exec, { name: step.name, cwd: step.cwd });
    } else Eif (step.say) {
      task.say(step.say, { name: step.name, cwd: step.cwd });
    } else if (step.spawn) {
      const stepToSpawn = project.tasks.tryFind(step.spawn);
      Iif (stepToSpawn) {
        task.spawn(stepToSpawn, { name: step.name, cwd: step.cwd });
      }
    } else Iif (step.builtin) {
      task.builtin(step.builtin);
    }
  }
}
 
/**
 * Determines if the passed in project is of type NodeProject.
 *
 * @param project Project instance.
 * @returns true if the project instance is of type NodeProject.
 */
function isNodeProject(project: any) {
  return project instanceof NodeProject || project.package;
}
 
function getPluginPath() {
  return path.join(__dirname, "plugin/nx-monorepo-plugin.js");
}