From cae826b8668e4d049096c2bece7a8eac9fa73846 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Thu, 7 Mar 2024 12:53:05 +0100 Subject: [PATCH] Use `yarn constraints` to update package.json --- .../e2e-babel7plugins-babel8core.sh | 2 +- .../babel-7-plugins-with-babel-8-core.sh | 26 ++++++++----------- yarn.config.cjs | 23 +++++++++++++--- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/scripts/integration-tests/e2e-babel7plugins-babel8core.sh b/scripts/integration-tests/e2e-babel7plugins-babel8core.sh index 3b43c36cc3c1..d1e83f5cd3f9 100755 --- a/scripts/integration-tests/e2e-babel7plugins-babel8core.sh +++ b/scripts/integration-tests/e2e-babel7plugins-babel8core.sh @@ -26,7 +26,7 @@ startLocalRegistry "$PWD"/scripts/integration-tests/verdaccio-config.yml export BABEL_8_BREAKING= # We use 'latest' for the published one because e2e CI # publishes Babel 8 marking it as Babel 7 stable -export BABEL_8_VERSION=latest +export BABEL_CORE_DEV_DEP_VERSION=latest ./test/babel-7-8-compat/babel-7-plugins-with-babel-8-core.sh diff --git a/test/babel-7-8-compat/babel-7-plugins-with-babel-8-core.sh b/test/babel-7-8-compat/babel-7-plugins-with-babel-8-core.sh index 2c2c98ba3e0f..5e6973951aee 100755 --- a/test/babel-7-8-compat/babel-7-plugins-with-babel-8-core.sh +++ b/test/babel-7-8-compat/babel-7-plugins-with-babel-8-core.sh @@ -1,6 +1,6 @@ #!/bin/bash -# Usage: BABEL_8_VERSION=next ./test/babel-7-8-compat/babel-7-plugins-with-babel-8-core.sh +# Usage: BABEL_CORE_DEV_DEP_VERSION=next ./test/babel-7-8-compat/babel-7-plugins-with-babel-8-core.sh # NOTE: This will change files in your monorepo, make sure to commit/stage # everything before running it. @@ -8,6 +8,11 @@ # SETUP # #==============================================================================# +[ -n "$BABEL_CORE_DEV_DEP_VERSION" ] || { + echo "Please specify BABEL_CORE_DEV_DEP_VERSION=x.y.z env var" >&2; + exit 1; +} + # Start in test/babel-7-8-compat even if run from root directory cd "$(dirname "$0")" || exit @@ -21,22 +26,13 @@ cd ../.. # TEST # #==============================================================================# -node -e ' - if (!process.env.BABEL_8_VERSION) throw new Error("Please specify BABEL_8_VERSION=x.y.z env var"); - - const pluginsList = require("./test/babel-7-8-compat/data.json")["babel7plugins-babel8core"]; - - for (const plugin of pluginsList) { - const pkgJsonPath = path.join("./packages", plugin, "package.json"); - const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath)); - pkgJson.devDependencies["@babel/core"] = process.env.BABEL_8_VERSION; - fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 2) + "\n"); - } -' || exit - export YARN_ENABLE_IMMUTABLE_INSTALLS=false -export SKIP_YARN_CORE_DEP_CHECK=true # Build and test +yarn constraints --fix make -j use-cjs yarn jest $(node -p 'require("./test/babel-7-8-compat/data.json")["babel7plugins-babel8core"].join(" ")' || exit) + +# Reset package.json changes +BABEL_CORE_DEV_DEP_VERSION= yarn constraints --fix +BABEL_CORE_DEV_DEP_VERSION= yarn diff --git a/yarn.config.cjs b/yarn.config.cjs index be5b2eb26bdd..6796336c4de7 100644 --- a/yarn.config.cjs +++ b/yarn.config.cjs @@ -1,9 +1,14 @@ // @ts-check +/// /** * @typedef {import('@yarnpkg/types').Yarn.Constraints.Context} Context * */ +const babel7plugins_babel8core = new Set( + require("./test/babel-7-8-compat/data.json")["babel7plugins-babel8core"] +); + /** * Enforces that all workspaces depend on other workspaces using `workspace:^` * @@ -21,7 +26,7 @@ function enforceWorkspaceDependencies({ Yarn }) { dependency.type === "dependencies" || dependency.type === "devDependencies" ) { - if (dependency.range.startsWith("workspace:")) { + if (/^workspace:(?!\^$)/.test(dependency.range)) { dependency.update("workspace:^"); } } @@ -268,8 +273,20 @@ function enforceBabelCoreNotInDeps({ Yarn }) { workspace.pkg.peerDependencies.has("@babel/core") && !workspace.manifest.dependencies?.["@babel/core"] ) { - if (process.env.SKIP_YARN_CORE_DEP_CHECK) continue; - workspace.set("devDependencies['@babel/core']", "workspace:^"); + if ( + process.env.BABEL_CORE_DEV_DEP_VERSION && + workspace.ident && + babel7plugins_babel8core.has( + workspace.ident.replace("@babel/", "babel-") + ) + ) { + workspace.set( + "devDependencies['@babel/core']", + process.env.BABEL_CORE_DEV_DEP_VERSION + ); + } else { + workspace.set("devDependencies['@babel/core']", "workspace:^"); + } } if ( workspace.ident !== null &&