All files nx-monorepo.ts

82.19% Statements 157/191
77.02% Branches 181/235
76.92% Functions 30/39
82.53% Lines 156/189

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 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895    1x 1x                   1x         1x 1x       1x   1x               1x       84x     84x 84x             84x                   1x       217x     217x 217x     172x   45x                                                                                                                                                                                                                                                                                                                                                     1x                     15x                                                   15x     15x 15x       3x 3x     11x 11x     1x 1x       15x 15x 15x     15x     15x         15x                         15x             15x 15x           15x 15x   15x 15x   15x 15x       15x 15x   15x     15x           15x 15x   15x 15x   15x 15x                 15x 15x 15x   15x 15x   15x     15x                   15x             15x 15x       15x             15x     15x               15x                                                                                               27x   27x               120x 120x     120x     120x     120x 15x   120x 15x   120x 120x   120x     120x     120x       120x                                             135x 15x     135x 15x     120x   15x     120x       120x     120x   120x   15x     120x                                                   3x   3x         3x                     1x     1x         1x                                         2x 1x           1x     1x                               18x 30x   18x 18x   30x         18x           73x 73x       55x 136x                                                                               13x   13x       13x     2x 2x                               14x 13x 13x       13x 13x 24x 19x 19x   19x       13x     13x                             14x   25x   25x       1x                         13x   13x   13x 13x 24x 19x         5x     13x           13x 2x             13x                       13x   24x     13x   2x 2x       3x           2x                                 51x             295x    
/*! 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 {
  Dependency,
  DependencyType,
  IgnoreFile,
  JsonFile,
  Project,
  Task,
  YamlFile,
} from "projen";
import { JavaProject } from "projen/lib/java";
import {
  NodePackage,
  NodePackageManager,
  NodeProject,
} from "projen/lib/javascript";
import { Poetry, PythonProject } from "projen/lib/python";
import {
  TypeScriptProject,
  TypeScriptProjectOptions,
} from "projen/lib/typescript";
import { NxProject } from "./nx-project";
import { Nx } from "./nx-types";
import { DEFAULT_CONFIG, SyncpackConfig } from "./syncpack-options";
 
/**
 * Execute download/execute command to run based on package manager configured.
 *
 * @param packageManager package manager being used.
 * @param args args to append.
 */
export function buildDownloadExecutableCommand(
  packageManager: NodePackageManager,
  ...args: string[]
) {
  Iif (args.length === 0) {
    throw new Error("args must be specified");
  }
  const argLiteral = args.join(" ");
  switch (packageManager) {
    case NodePackageManager.YARN2:
      return `yarn dlx ${argLiteral}`;
    case NodePackageManager.PNPM:
      return `pnpm dlx ${argLiteral}`;
    case NodePackageManager.YARN:
    default:
      return `npx ${argLiteral}`;
  }
}
 
/**
 * Execute command to run based on package manager configured.
 *
 * @param packageManager package manager being used.
 * @param args args to append.
 */
export function buildExecutableCommand(
  packageManager: NodePackageManager,
  ...args: string[]
) {
  Iif (args.length === 0) {
    throw new Error("args must be specified");
  }
  const argLiteral = args.join(" ");
  switch (packageManager) {
    case NodePackageManager.YARN:
    case NodePackageManager.YARN2:
      return `yarn run ${argLiteral}`;
    case NodePackageManager.PNPM:
      return `pnpm exec ${argLiteral}`;
    default:
      return `npx ${argLiteral}`;
  }
}
 
function binCommand(packageManager: NodePackageManager): string {
  switch (packageManager) {
    case NodePackageManager.YARN:
    case NodePackageManager.YARN2:
      return `yarn bin`;
    case NodePackageManager.PNPM:
      return `pnpm bin`;
    default:
      return `npm bin`;
  }
}
 
/**
 * Workspace configurations.
 *
 * @see https://classic.yarnpkg.com/lang/en/docs/workspaces/
 */
export interface WorkspaceConfig {
  /**
   * List of package globs to exclude from hoisting in the workspace.
   *
   * @see 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;
 
  /**
   * Links all local workspace project bins so they can be used for local development.
   *
   * Package bins are only linked when installed from the registry, however it is very useful
   * for monorepo development to also utilize these bin scripts. When enabled, this flag will
   * recursively link all bins from packages.json files to the root node_modules/.bin.
   */
  readonly linkLocalWorkspaceBins?: 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?: Nx.WorkspaceConfig;
 
  /**
   * 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;
}
 
/**
 *
 * @see https://nx.dev/packages/nx/documents/run-many#options
 */
export interface NxRunManyOptions {
  /** Task to run for affected projects */
  readonly target: string;
  /** This is the configuration to use when performing tasks on projects */
  readonly configuration?: string;
  /** Exclude certain projects from being processed */
  readonly exclude?: string;
  /**
   * Do not stop command execution after the first failed task.
   */
  readonly noBail?: boolean;
  /**
   * Defines how Nx emits outputs tasks logs
   * @default "stream"
   */
  readonly outputStyle?:
    | "dynamic"
    | "static"
    | "stream"
    | "stream-without-prefixes";
  /**
   * Max number of parallel processes
   * @default 3
   */
  readonly parallel?: number;
  /** Project to run as list project names and/or patterns. */
  readonly projects?: string[];
  /** This is the name of the tasks runner configuration in nx.json */
  readonly runner?: string;
  /** Rerun the tasks even when the results are available in the cache. */
  readonly skipCache?: boolean;
  /** Ignore cycles in the task graph */
  readonly ignoreCycles?: boolean;
  /** Prints additional information about the commands (e.g. stack traces). */
  readonly verbose?: boolean;
}
 
/**
 * Options for overriding nx build tasks
 * @internal
 */
interface OverrideNxBuildTaskOptions {
  /**
   * Force unlocking task (eg: build task is locked)
   */
  readonly force?: boolean;
  /**
   * Disable further resets of the task by other components in further lifecycle stages
   * (eg eslint resets during preSynthesize)
   */
  readonly disableReset?: boolean;
}
 
/**
 * 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 {
  // immutable data structures
  private readonly nxConfig?: Nx.WorkspaceConfig;
  private readonly workspaceConfig?: WorkspaceConfig;
  private readonly workspacePackages: string[];
 
  public readonly nxJson: JsonFile;
 
  private readonly _options: NxMonorepoProjectOptions;
 
  constructor(options: NxMonorepoProjectOptions) {
    super({
      ...options,
      github: options.github ?? false,
      package: options.package ?? false,
      projenCommand: options.packageManager
        ? buildExecutableCommand(options.packageManager, "projen")
        : undefined,
      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,
      gitignore: [".nx/cache", ...(options.gitignore ?? [])],
      eslintOptions: options.eslintOptions ?? {
        dirs: ["."],
        ignorePatterns: ["packages/**/*.*"],
      },
      tsconfig: options.tsconfig ?? {
        compilerOptions: {
          rootDir: ".",
        },
        include: ["**/*.ts"],
      },
    });
 
    this._options = options;
 
    // engines
    this.package.addEngine("node", ">=16");
    switch (this.package.packageManager) {
      case NodePackageManager.PNPM: {
        // https://pnpm.io/package_json
        // https://github.com/pnpm/pnpm/releases/tag/v8.0.0
        this.package.addEngine("pnpm", ">=8");
        break;
      }
      case NodePackageManager.YARN: {
        this.package.addEngine("yarn", ">=1 <2");
        break;
      }
      case NodePackageManager.YARN2: {
        this.package.addEngine("yarn", ">=2 <3");
        break;
      }
    }
 
    this.nxConfig = options.nxConfig;
    this.workspaceConfig = options.workspaceConfig;
    this.workspacePackages = options.workspaceConfig?.additionalPackages ?? [];
 
    // Never publish a monorepo root package.
    this.package.addField("private", true);
 
    // Add alias task for "projen" to synthesize workspace
    this.addTask("synth-workspace", {
      exec: buildExecutableCommand(this.package.packageManager, "projen"),
      description: "Synthesize workspace",
    });
 
    this.addTask("run-many", {
      receiveArgs: true,
      exec: buildExecutableCommand(
        this.package.packageManager,
        "nx",
        "run-many"
      ),
      env: {
        NX_NON_NATIVE_HASHER: "true",
      },
      description: "Run task against multiple workspace projects",
    });
 
    this.addTask("graph", {
      receiveArgs: true,
      exec: buildExecutableCommand(this.package.packageManager, "nx", "graph"),
      description: "Generate dependency graph for monorepo",
    });
 
    // Map tasks to nx run-many
    if (options.scripts == null || options.scripts.build == null) {
      this._overrideNxBuildTask(
        this.buildTask,
        { target: "build" },
        { force: true }
      );
    }
    if (options.scripts == null || options.scripts["pre-compile"] == null) {
      this._overrideNxBuildTask(this.preCompileTask, { target: "pre-compile" });
    }
    if (options.scripts == null || options.scripts.compile == null) {
      this._overrideNxBuildTask(this.compileTask, { target: "compile" });
    }
    if (options.scripts == null || options.scripts["post-compile"] == null) {
      this._overrideNxBuildTask(this.postCompileTask, {
        target: "post-compile",
      });
    }
    if (options.scripts == null || options.scripts.test == null) {
      this._overrideNxBuildTask(this.testTask, { target: "test" });
    }
    if (options.scripts == null || options.scripts.eslint == null) {
      // The Projenrc component of TypeScriptProject resets the eslint task as part of preSynthesize which would undo
      // our changes, so we disable further resets.
      this._overrideNxBuildTask(
        this.eslint?.eslintTask,
        { target: "eslint" },
        { disableReset: true }
      );
    }
    if (options.scripts == null || options.scripts.package == null) {
      this._overrideNxBuildTask(this.packageTask, { target: "package" });
    }
    if (options.scripts == null || options.scripts.prepare == null) {
      this._overrideNxBuildTask("prepare", { target: "prepare" });
    }
    if (options.scripts == null || options.scripts.watch == null) {
      this._overrideNxBuildTask(this.watchTask, {
        target: "watch",
        noBail: false,
        ignoreCycles: true,
        skipCache: true,
        outputStyle: "stream",
      });
    }
 
    this.addDevDeps("nx", "@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(
        buildExecutableCommand(
          this.package.packageManager,
          "npm-check-updates",
          "--deep",
          "--rejectVersion",
          "0.0.0",
          "-u"
        )
      );
      upgradeDepsTask.exec(
        buildExecutableCommand(
          this.package.packageManager,
          "syncpack",
          "fix-mismatches"
        )
      );
      upgradeDepsTask.exec(`${this.package.packageManager} install`);
      upgradeDepsTask.exec(
        buildExecutableCommand(this.package.packageManager, "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 || [])
    );
 
    this.nxJson = new JsonFile(this, "nx.json", {
      obj: {
        extends: "@nrwl/workspace/presets/npm.json",
        npmScope: "monorepo",
        tasksRunnerOptions: {
          default: {
            runner: options.nxConfig?.nxCloudReadOnlyAccessToken
              ? "@nrwl/nx-cloud"
              : "@nrwl/workspace/tasks-runners/default",
            options: {
              useDaemonProcess: false,
              cacheableOperations: options.nxConfig?.cacheableOperations || [
                "build",
                "test",
              ],
              accessToken: options.nxConfig?.nxCloudReadOnlyAccessToken,
            },
          },
        },
        namedInputs: {
          // https://nx.dev/more-concepts/customizing-inputs#defaults
          default: ["{projectRoot}/**/*"],
          ...options.nxConfig?.namedInputs,
        },
        targetDefaults: options.nxConfig?.targetDefaults,
        targetDependencies: {
          build: [
            {
              target: "build",
              projects: "dependencies",
            },
          ],
          ...(this.nxConfig?.targetDependencies || {}),
        },
        affected: {
          defaultBase: this.nxConfig?.affectedBranch || "mainline",
        },
      },
    });
  }
 
  /**
   * Ensure NXProject is added when adding subprojects to the monorepo.
   *
   * @internal
   * @param subproject project to add.
   */
  _addSubProject(subproject: Project) {
    !NxProject.of(subproject) &&
      subproject._addComponent(new NxProject(subproject));
    super._addSubProject(subproject);
  }
 
  /**
   * Helper to format `npx nx run-many ...` style command.
   * @param options
   */
  public formatNxRunManyCommand(options: NxRunManyOptions): string {
    const args: string[] = [];
    Iif (options.configuration) {
      args.push(`--configuration=${options.configuration}`);
    }
    Iif (options.runner) {
      args.push(`--runner=${options.runner}`);
    }
    Iif (options.parallel) {
      args.push(`--parallel=${options.parallel}`);
    }
    if (options.skipCache) {
      args.push("--skip-nx-cache");
    }
    if (options.ignoreCycles) {
      args.push("--nx-ignore-cycles");
    }
    if (options.noBail !== true) {
      args.push("--nx-bail");
    }
    Iif (options.projects && options.projects.length) {
      args.push(`--projects=${options.projects.join(",")}`);
    }
    Iif (options.exclude) {
      args.push(`--exclude=${options.exclude}`);
    }
    Iif (options.verbose) {
      args.push("--verbose");
    }
 
    return buildExecutableCommand(
      this.package.packageManager,
      "nx",
      "run-many",
      `--target=${options.target}`,
      `--output-style=${options.outputStyle || "stream"}`,
      ...args
    );
  }
 
  /**
   * Overrides "build" related project tasks (build, compile, test, etc.) with `npx nx run-many` format.
   * @param task - The task or task name to override
   * @param options - Nx run-many options
   * @param overrideOptions - Options for overriding the task
   * @returns - The task that was overridden
   * @internal
   */
  protected _overrideNxBuildTask(
    task: Task | string | undefined,
    options: NxRunManyOptions,
    overrideOptions?: OverrideNxBuildTaskOptions
  ): Task | undefined {
    if (typeof task === "string") {
      task = this.tasks.tryFind(task);
    }
 
    if (task == null) {
      return;
    }
 
    if (overrideOptions?.force) {
      // @ts-ignore - private property
      task._locked = false;
    }
 
    task.reset(this.formatNxRunManyCommand(options), {
      receiveArgs: true,
    });
 
    task.description += " for all affected projects";
 
    // Fix for https://github.com/nrwl/nx/pull/15071
    task.env("NX_NON_NATIVE_HASHER", "true");
 
    if (overrideOptions?.disableReset) {
      // Prevent any further resets of the task to force it to remain as the overridden nx build task
      task.reset = () => {};
    }
 
    return task;
  }
 
  /**
   * Add project task that executes `npx nx run-many ...` style command.
   */
  public addNxRunManyTask(name: string, options: NxRunManyOptions): Task {
    return this.addTask(name, {
      receiveArgs: true,
      exec: this.formatNxRunManyCommand(options),
      env: {
        NX_NON_NATIVE_HASHER: "true",
      },
    });
  }
 
  /**
   * Create an implicit dependency between two Projects. 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.
   * @throws error if this is called on a dependent which does not have a NXProject component attached.
   */
  public addImplicitDependency(dependent: Project, dependee: Project | string) {
    const nxProject = NxProject.of(dependent);
 
    Iif (!nxProject) {
      throw new Error(
        `${dependent.name} does not have an NXProject associated.`
      );
    } else {
      nxProject.addImplicitDependency(dependee);
    }
  }
 
  /**
   * Adds a dependency between two Java Projects in the monorepo.
   * @param dependent project you want to have the dependency
   * @param dependee project you wish to depend on
   */
  public addJavaDependency(dependent: JavaProject, dependee: JavaProject) {
    // Add implicit dependency for build order
    this.addImplicitDependency(dependent, dependee);
 
    // Add dependency in pom.xml
    dependent.addDependency(
      `${dependee.pom.groupId}/${dependee.pom.artifactId}@${dependee.pom.version}`
    );
 
    // Add a repository so that the dependency in the pom can be resolved
    dependent.pom.addRepository({
      id: dependee.name,
      url: `file://${path.join(
        path.relative(dependent.outdir, dependee.outdir),
        "dist",
        "java"
      )}`,
    });
  }
 
  /**
   * Adds a dependency between two Python Projects in the monorepo. The dependent must have Poetry enabled.
   * @param dependent project you want to have the dependency (must be a Poetry Python Project)
   * @param dependee project you wish to depend on
   * @throws error if the dependent does not have Poetry enabled
   */
  public addPythonPoetryDependency(
    dependent: PythonProject,
    dependee: PythonProject
  ) {
    // Check we're adding the dependency to a poetry python project
    if (!(dependent.depsManager instanceof Poetry)) {
      throw new Error(
        `${dependent.name} must be a PythonProject with Poetry enabled to add this dependency`
      );
    }
 
    // Add implicit dependency for build order
    this.addImplicitDependency(dependent, dependee);
 
    // Add local path dependency
    dependent.addDependency(
      `${dependee.name}@{path="${path.relative(
        dependent.outdir,
        dependee.outdir
      )}", develop=true}`
    );
  }
 
  /**
   * 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);
  }
 
  // 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)
    );
  }
 
  /**
   * Create symbolic links to all local workspace bins. This enables the usage of bins the same
   * way as consumers of the packages have when installing from the registry.
   */
  protected linkLocalWorkspaceBins(): void {
    const bins: [string, string][] = [];
 
    this.subProjects.forEach((subProject) => {
      Iif (subProject instanceof NodeProject) {
        const pkgBins: Record<string, string> =
          subProject.package.manifest.bin() || {};
        bins.push(
          ...Object.entries(pkgBins).map(([cmd, bin]) => {
            const resolvedBin = path.join(
              "$PWD",
              path.relative(this.outdir, subProject.outdir),
              bin
            );
            return [cmd, resolvedBin] as [string, string];
          })
        );
      }
    });
 
    const linkTask = this.addTask("workspace:bin:link", {
      steps: bins.map(([cmd, bin]) => ({
        exec: `ln -s ${bin} $(${binCommand(
          this.package.packageManager
        )})/${cmd} &>/dev/null; exit 0;`,
      })),
    });
 
    (this.tasks.tryFind("prepare") || this.addTask("prepare")).spawn(linkTask);
  }
 
  preSynthesize(): void {
    super.preSynthesize();
 
    Iif (this._options.workspaceConfig?.linkLocalWorkspaceBins === true) {
      this.linkLocalWorkspaceBins();
    }
 
    if (this.package.packageManager === NodePackageManager.PNPM) {
      // PNPM hoisting hides transitive bundled dependencies which results in
      // transitive dependencies being packed incorrectly.
      this.subProjects.forEach((subProject) => {
        Iif (isNodeProject(subProject) && getBundledDeps(subProject).length) {
          const pkgFolder = path.relative(this.root.outdir, subProject.outdir);
          // Create a symlink in the sub-project node_modules for all transitive deps
          // before running "package" task
          subProject.packageTask.prependExec(
            `pdk@pnpm-link-bundled-transitive-deps ${pkgFolder}`
          );
        }
      });
    }
  }
 
  /**
   * @inheritDoc
   */
  synth() {
    this.validateSubProjects();
    this.updateWorkspace();
    this.installNonNodeDependencies();
 
    // 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 changed, 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}.`
        );
      }
    });
  }
 
  /**
   * 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 getBundledDeps(sub).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,
        },
      });
    }
    this.package.addField("workspaces", {
      packages: this.workspacePackages,
      nohoist: noHoist,
    });
  }
 
  /**
   * Ensures the install task for non-node projects is executed postinstall.
   *
   * @private
   */
  private installNonNodeDependencies() {
    const installProjects = this.subProjects.filter(
      (project) =>
        !(project instanceof NodeProject) && project.tasks.tryFind("install")
    );
 
    if (installProjects.length > 0) {
      const monorepoInstallTask =
        this.tasks.tryFind("postinstall") ?? this.addTask("postinstall");
      monorepoInstallTask.exec(
        buildExecutableCommand(
          this.package.packageManager,
          `nx run-many --target install --projects ${installProjects
            .map((project) => project.name)
            .join(",")} --parallel=1`
        )
      );
 
      // Update the nx.json to ensure that install-py follows dependency order
      this.nxJson.addOverride("targetDependencies.install", [
        {
          target: "install",
          projects: "dependencies",
        },
      ]);
    }
  }
}
 
/**
 * 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): project is NodeProject {
  return project instanceof NodeProject || project.package;
}
 
/**
 * Gets bundled dependencies for a given project
 */
function getBundledDeps(project: Project): Dependency[] {
  return project.deps.all.filter((dep) => dep.type === DependencyType.BUNDLED);
}