Skip to content

Commit

Permalink
Use yarn constraints to update package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Mar 7, 2024
1 parent f274bfc commit cae826b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion scripts/integration-tests/e2e-babel7plugins-babel8core.sh
Expand Up @@ -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

Expand Down
26 changes: 11 additions & 15 deletions test/babel-7-8-compat/babel-7-plugins-with-babel-8-core.sh
@@ -1,13 +1,18 @@
#!/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.

#==============================================================================#
# 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

Expand All @@ -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
23 changes: 20 additions & 3 deletions yarn.config.cjs
@@ -1,9 +1,14 @@
// @ts-check
/// <reference lib="es2015" />

/**
* @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:^`
*
Expand All @@ -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:^");
}
}
Expand Down Expand Up @@ -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 &&
Expand Down

0 comments on commit cae826b

Please sign in to comment.