From d1438991083c9e81606ba141dc79eb957ebe4c16 Mon Sep 17 00:00:00 2001 From: Adrian Dimech <51220968+agdimech@users.noreply.github.com> Date: Wed, 17 Apr 2024 20:56:48 +1000 Subject: [PATCH] fix: ensure link-workspace-packages is true in npmrc (#764) fix #761 --- .npmrc | 1 + .../src/components/nx-configurator.ts | 22 +++++++++++++++++++ .../test/__snapshots__/monorepo.test.ts.snap | 16 ++++++++++++++ .../type-safe-api/common/.pnpmfile.cjs | 18 +++++++++++++++ .../scripts/type-safe-api/common/common.sh | 2 ++ .../type-safe-api-project.test.ts.snap | 3 +++ ...pe-safe-websocket-api-project.test.ts.snap | 3 +++ 7 files changed, 65 insertions(+) create mode 100644 packages/type-safe-api/scripts/type-safe-api/common/.pnpmfile.cjs diff --git a/.npmrc b/.npmrc index dd0728008..3603a114f 100644 --- a/.npmrc +++ b/.npmrc @@ -3,3 +3,4 @@ resolution-mode=highest yes=true prefer-workspace-packages=true +link-workspace-packages=true diff --git a/packages/monorepo/src/components/nx-configurator.ts b/packages/monorepo/src/components/nx-configurator.ts index 18062e298..6ee9e6248 100644 --- a/packages/monorepo/src/components/nx-configurator.ts +++ b/packages/monorepo/src/components/nx-configurator.ts @@ -3,6 +3,7 @@ SPDX-License-Identifier: Apache-2.0 */ import * as path from "path"; import { Component, + IniFile, JsonFile, License, Project, @@ -485,6 +486,27 @@ export class NxConfigurator extends Component implements INxProjectCore { }, }).synthesize(); } + + if ( + !ProjectUtils.isNamedInstanceOf(this.project, NodeProject) && + !this.project.tryFindFile(".npmrc") + ) { + new IniFile(this.project, ".npmrc", { + obj: { + "resolution-mode": "highest", + yes: "true", + "prefer-workspace-packages": "true", + "link-workspace-packages": "true", + }, + }).synthesize(); + } else if ( + ProjectUtils.isNamedInstanceOf(this.project, NodeProject) && + this.project.package.packageManager === NodePackageManager.PNPM + ) { + this.project.npmrc.addConfig("prefer-workspace-packages", "true"); + this.project.npmrc.addConfig("link-workspace-packages", "true"); + this.project.npmrc.addConfig("yes", "true"); + } } private _invokeInstallCITasks() { diff --git a/packages/monorepo/test/__snapshots__/monorepo.test.ts.snap b/packages/monorepo/test/__snapshots__/monorepo.test.ts.snap index a0ea9df89..981330d53 100644 --- a/packages/monorepo/test/__snapshots__/monorepo.test.ts.snap +++ b/packages/monorepo/test/__snapshots__/monorepo.test.ts.snap @@ -233,6 +233,9 @@ tsconfig.tsbuildinfo ".npmrc": "# ~~ Generated by projen. To modify, edit .projenrc.ts and run "pnpm exec projen". resolution-mode=highest +prefer-workspace-packages=true +link-workspace-packages=true +yes=true ", ".nxignore": "# ~~ Generated by projen. To modify, edit .projenrc.ts and run "pnpm exec projen". .tmp @@ -19282,6 +19285,7 @@ exports[`NX Monorepo Unit Tests Empty Python Monorepo 1`] = ` /.gitattributes linguist-generated /.github/workflows/pull-request-lint.yml linguist-generated /.gitignore linguist-generated +/.npmrc linguist-generated /.nxignore linguist-generated /.projen/** linguist-generated /.projen/deps.json linguist-generated @@ -19410,8 +19414,16 @@ cython_debug/ !/.nxignore !/package.json !/pnpm-workspace.yaml +!/.npmrc !/LICENSE !/.projenrc.py +", + ".npmrc": "# ~~ Generated by projen. To modify, edit .projenrc.py and run "npx projen". + +resolution-mode=highest +yes=true +prefer-workspace-packages=true +link-workspace-packages=true ", ".nxignore": "# ~~ Generated by projen. To modify, edit .projenrc.py and run "npx projen". .tmp @@ -19439,6 +19451,7 @@ cython_debug/ ".gitattributes", ".github/workflows/pull-request-lint.yml", ".gitignore", + ".npmrc", ".nxignore", ".projen/deps.json", ".projen/files.json", @@ -21648,6 +21661,9 @@ tsconfig.tsbuildinfo ".npmrc": "# ~~ Generated by projen. To modify, edit .projenrc.ts and run "pnpm exec projen". resolution-mode=highest +prefer-workspace-packages=true +link-workspace-packages=true +yes=true ", ".nxignore": "# ~~ Generated by projen. To modify, edit .projenrc.ts and run "pnpm exec projen". .tmp diff --git a/packages/type-safe-api/scripts/type-safe-api/common/.pnpmfile.cjs b/packages/type-safe-api/scripts/type-safe-api/common/.pnpmfile.cjs new file mode 100644 index 000000000..410c75e7e --- /dev/null +++ b/packages/type-safe-api/scripts/type-safe-api/common/.pnpmfile.cjs @@ -0,0 +1,18 @@ +/*! Copyright [Amazon.com](http://amazon.com/), Inc. or its affiliates. All Rights Reserved. +SPDX-License-Identifier: Apache-2.0 */ + +const readPackage = (pkg, context) => { + if (pkg.peerDependencies && pkg.peerDependencies.react) { + // fixes react error "Error: Invalid hook call." + // with multiple copy of react in the node_modules folder + context.log(`[${pkg.name}] Removing react as a peer dependency (https://bit.ly/3jmD8J6).`) + delete pkg.peerDependencies.react; + } + return pkg; +}; + +module.exports = { + hooks: { + readPackage + } +}; \ No newline at end of file diff --git a/packages/type-safe-api/scripts/type-safe-api/common/common.sh b/packages/type-safe-api/scripts/type-safe-api/common/common.sh index 215cfe0a8..5181d3f87 100755 --- a/packages/type-safe-api/scripts/type-safe-api/common/common.sh +++ b/packages/type-safe-api/scripts/type-safe-api/common/common.sh @@ -63,6 +63,7 @@ run_install_command() { # installs the passed packages with the package manager in use _install_packages() { log "installing packages :: $@" + _script_dir="$( cd -- "$(dirname $([ -L "${BASH_SOURCE[0]:-$0}" ] && readlink -f "${BASH_SOURCE[0]:-$0}" || echo "${BASH_SOURCE[0]:-$0}"))" >/dev/null 2>&1 ; pwd -P )"; _install_packages_working_dir=`pwd` _install_packages_pdk_base_dir="$HOME/.pdk/$AWS_PDK_VERSION/type-safe-api/$pkg_manager" @@ -90,6 +91,7 @@ _install_packages() { # Install if any packages are missing if [ "$_install_packages_should_install" == "true" ]; then npm init --yes + cp $_script_dir/.pnpmfile.cjs $_install_packages_pdk_dir run_install_command "$@" fi diff --git a/packages/type-safe-api/test/project/__snapshots__/type-safe-api-project.test.ts.snap b/packages/type-safe-api/test/project/__snapshots__/type-safe-api-project.test.ts.snap index 04051eb9c..ed745f703 100644 --- a/packages/type-safe-api/test/project/__snapshots__/type-safe-api-project.test.ts.snap +++ b/packages/type-safe-api/test/project/__snapshots__/type-safe-api-project.test.ts.snap @@ -55821,6 +55821,9 @@ tsconfig.tsbuildinfo ".npmrc": "# ~~ Generated by projen. To modify, edit .projenrc.ts and run "pnpm exec projen". resolution-mode=highest +prefer-workspace-packages=true +link-workspace-packages=true +yes=true ", ".nxignore": "# ~~ Generated by projen. To modify, edit .projenrc.ts and run "pnpm exec projen". .tmp diff --git a/packages/type-safe-api/test/project/__snapshots__/type-safe-websocket-api-project.test.ts.snap b/packages/type-safe-api/test/project/__snapshots__/type-safe-websocket-api-project.test.ts.snap index 1d30dfc8a..71b7beac2 100644 --- a/packages/type-safe-api/test/project/__snapshots__/type-safe-websocket-api-project.test.ts.snap +++ b/packages/type-safe-api/test/project/__snapshots__/type-safe-websocket-api-project.test.ts.snap @@ -21497,6 +21497,9 @@ tsconfig.tsbuildinfo ".npmrc": "# ~~ Generated by projen. To modify, edit .projenrc.ts and run "pnpm exec projen". resolution-mode=highest +prefer-workspace-packages=true +link-workspace-packages=true +yes=true ", ".nxignore": "# ~~ Generated by projen. To modify, edit .projenrc.ts and run "pnpm exec projen". .tmp