Skip to content

Commit

Permalink
fix(cli): add a flag to relax version checks (#2590)
Browse files Browse the repository at this point in the history
This change adds an `--ignore-version-check` flag to the CLI to allow
for selectively ignoring the semver check the CLI performs when
evaluating the plugin package dependencies.  This can be used to work
around issues where a plugin has not been updated for awhile but it is
known to work because it relies on interfaces and functions that have
not changed.

Signed-off-by: Stan Lewis <gashcrumb@gmail.com>
Co-authored-by: Stan Lewis <gashcrumb@gmail.com>
  • Loading branch information
openshift-cherrypick-robot and gashcrumb authored Dec 3, 2024
1 parent 8f66c89 commit 1277acb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/dull-pillows-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@janus-idp/cli": patch
---

fix(cli): add a flag to relax semver checks. This change updates the CLI to add an option to relax the semver checks on a per-package basis to cater for plugins where it is known there should be runtime compabability due to no interface changes.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export async function backend(opts: OptionValues): Promise<string> {
const packagesToEmbed = (opts.embedPackage || []) as string[];
const allowNative = (opts.allowNativePackage || []) as string[];
const suppressNative = (opts.suppressNativePackage || []) as string[];
const ignoreVersionCheck = (opts.ignoreVersionCheck || []) as string[];
const monoRepoPackages = await getPackages(paths.targetDir);
const embeddedResolvedPackages = await searchEmbedded(
pkg,
Expand Down Expand Up @@ -302,7 +303,11 @@ throw new Error(
`Hoisting peer dependencies of embedded packages to the main package`,
);
const mainPeerDependencies = mainPkg.peerDependencies || {};
addToMainDependencies(embeddedPeerDependencies, mainPeerDependencies);
addToMainDependencies(
embeddedPeerDependencies,
mainPeerDependencies,
ignoreVersionCheck,
);
if (Object.keys(mainPeerDependencies).length > 0) {
mainPkg.peerDependencies = mainPeerDependencies;
}
Expand Down
24 changes: 15 additions & 9 deletions packages/cli/src/commands/export-dynamic-plugin/backend-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import semver from 'semver';

import path from 'path';

import { Task } from '../../lib/tasks';

export function addToDependenciesForModule(
dependency: { name: string; version: string },
dependencies: { [key: string]: string },
Expand All @@ -43,24 +45,22 @@ export function addToDependenciesForModule(
existingDependencyMinVersion &&
semver.satisfies(existingDependencyMinVersion, dependency.version)
) {
console.log(
Task.log(
`Several compatible versions ('${existingDependencyVersion}', '${dependency.version}') of the same transitive dependency ('${dependency.name}') for embedded module ('${module}'): keeping '${existingDependencyVersion}'`,
);
return;
}

const newDependencyMinVersion = semver.minVersion(dependency.version);
if (
newDependencyMinVersion &&
semver.satisfies(newDependencyMinVersion, existingDependencyVersion)
) {
dependencies[dependency.name] = dependency.version;
console.log(
Task.log(
`Several compatible versions ('${existingDependencyVersion}', '${dependency.version}') of the same transitive dependency ('${dependency.name}') for embedded module ('${module}'): keeping '${dependency.version}'`,
);
return;
}

throw new Error(
`Several incompatible versions ('${existingDependencyVersion}', '${dependency.version}') of the same transitive dependency ('${dependency.name}') for embedded module ('${module}')`,
);
Expand All @@ -69,6 +69,7 @@ export function addToDependenciesForModule(
export function addToMainDependencies(
dependenciesToAdd: { [key: string]: string },
mainDependencies: { [key: string]: string },
ignoreVersionCheck: string[] = [],
): void {
for (const dep in dependenciesToAdd) {
if (!Object.prototype.hasOwnProperty.call(dependenciesToAdd, dep)) {
Expand All @@ -81,18 +82,23 @@ export function addToMainDependencies(
}
if (existingVersion !== dependenciesToAdd[dep]) {
const existingMinVersion = semver.minVersion(existingVersion);

if (
existingMinVersion &&
semver.satisfies(existingMinVersion, dependenciesToAdd[dep])
) {
console.log(
Task.log(
`The version of a dependency ('${dep}') of an embedded module differs from the main module's dependencies: '${dependenciesToAdd[dep]}', '${existingVersion}': keeping it as it is compatible`,
);
} else {
throw new Error(
`The version of a dependency ('${dep}') of an embedded module conflicts with main module dependencies: '${dependenciesToAdd[dep]}', '${existingVersion}': cannot proceed!`,
);
if (!ignoreVersionCheck.includes(dep)) {
throw new Error(
`The version of a dependency ('${dep}') of an embedded module conflicts with main module dependencies: '${dependenciesToAdd[dep]}', '${existingVersion}': cannot proceed!`,
);
} else {
Task.log(
`The version of a dependency ('${dep}') of an embedded module conflicts with the main module's dependencies: '${dependenciesToAdd[dep]}', '${existingVersion}': however this has been overridden`,
);
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ export function registerScriptCommand(program: Command) {
'--suppress-native-package [package-name...]',
'Optional list of native package names to be excluded from the exported plugin',
)
.option(
'--ignore-version-check [packageName...]',
'Optional list of package names to ignore when doing semver dependency checks',
)
.option(
'--no-install',
'Do not run `yarn install` to fill the dynamic plugin `node_modules` folder (backend plugin only).',
Expand Down

0 comments on commit 1277acb

Please sign in to comment.