mirror of
https://github.com/changesets/action
synced 2026-10-26 16:39:56 +08:00
Validate that projects use Changesets CLI v3 and direct Changesets CLI v2 users to changesets/action@v1 (#699)
* Validate that projects use Changesets CLI v3 and direct Changesets CLI v2 users to `changesets/action@v1` * better spy/mocking * fmt * shuffle
This commit is contained in:
@@ -10,6 +10,8 @@ import {
|
||||
type ExecOutput,
|
||||
} from "@actions/exec";
|
||||
import { getPackages, type Package } from "@manypkg/get-packages";
|
||||
import major from "semver/functions/major.js";
|
||||
import subset from "semver/ranges/subset.js";
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
|
||||
@@ -154,6 +156,55 @@ export function throwOnRenamedInputs(renames: Record<string, string>) {
|
||||
}
|
||||
}
|
||||
|
||||
const changesetsCliCompatibilityError =
|
||||
"This version of the Changesets action is designed to work with Changesets CLI v3. " +
|
||||
"Changesets CLI v2 is not supported; use Changesets action v1 instead, which is compatible with CLI v2.";
|
||||
|
||||
export async function validateChangesetsCliVersion(cwd: string) {
|
||||
const { rootPackage } = await getPackages(cwd);
|
||||
const packageJson = rootPackage?.packageJson;
|
||||
const declaredVersion =
|
||||
packageJson?.devDependencies?.["@changesets/cli"] ??
|
||||
packageJson?.dependencies?.["@changesets/cli"];
|
||||
|
||||
if (typeof declaredVersion === "string") {
|
||||
const range = declaredVersion.startsWith("workspace:")
|
||||
? declaredVersion.slice("workspace:".length)
|
||||
: declaredVersion;
|
||||
|
||||
let isV2 = false;
|
||||
|
||||
try {
|
||||
isV2 = subset(range, ">=2.0.0-0 <3.0.0-0", {
|
||||
includePrerelease: true,
|
||||
});
|
||||
} catch {
|
||||
// it could be a non-semver protocol
|
||||
}
|
||||
|
||||
if (isV2) {
|
||||
throw new Error(changesetsCliCompatibilityError);
|
||||
}
|
||||
}
|
||||
|
||||
let cliPackageJson;
|
||||
|
||||
try {
|
||||
cliPackageJson = require(
|
||||
require.resolve("@changesets/cli/package.json", { paths: [cwd] }),
|
||||
);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
typeof cliPackageJson.version === "string" &&
|
||||
major(cliPackageJson.version) === 2
|
||||
) {
|
||||
throw new Error(changesetsCliCompatibilityError);
|
||||
}
|
||||
}
|
||||
|
||||
function resolveChangesetsCli(cwd: string) {
|
||||
return require.resolve("@changesets/cli/bin.js", {
|
||||
paths: [cwd],
|
||||
|
||||
Reference in New Issue
Block a user