From 676c57de19e12ae8468ed454c6a09a914b0f206a Mon Sep 17 00:00:00 2001 From: Jason O'Neill Date: Thu, 21 Mar 2024 18:59:49 +0000 Subject: [PATCH] PR feedback --- src/lib/getPeerDependenciesFromRegistry.ts | 42 ++++++++++++++++++++-- src/lib/isCircularPeer.ts | 38 -------------------- test/peer.test.ts | 6 ++-- 3 files changed, 42 insertions(+), 44 deletions(-) delete mode 100644 src/lib/isCircularPeer.ts diff --git a/src/lib/getPeerDependenciesFromRegistry.ts b/src/lib/getPeerDependenciesFromRegistry.ts index aea175e7..96d89c8a 100644 --- a/src/lib/getPeerDependenciesFromRegistry.ts +++ b/src/lib/getPeerDependenciesFromRegistry.ts @@ -2,9 +2,45 @@ import ProgressBar from 'progress' import nodeSemver from 'semver' import { Index } from '../types/IndexType' import { Options } from '../types/Options' -import { VersionSpec } from '../types/VersionSpec' +import { Version } from '../types/Version' import getPackageManager from './getPackageManager' -import { isCircularPeer } from './isCircularPeer' + +type CircularData = + | { + isCircular: true + offendingPackage: string + } + | { + isCircular: false + } + +/** + * Checks if the specified package will create a loop of peer dependencies by traversing all paths to find a cycle + * + * If a cycle was found, the offending peer dependency of the specified package is returned + */ +function isCircularPeer(peerDependencies: Index>, packageName: string): CircularData { + let queue = [[packageName]] + while (queue.length > 0) { + const nextQueue: string[][] = [] + for (const path of queue) { + const parents = Object.keys(peerDependencies[path[0]] ?? {}) + for (const name of parents) { + if (name === path.at(-1)) { + return { + isCircular: true, + offendingPackage: path[0], + } + } + nextQueue.push([name, ...path]) + } + } + queue = nextQueue + } + return { + isCircular: false, + } +} /** * Get the latest or greatest versions from the NPM repository based on the version target. @@ -13,7 +49,7 @@ import { isCircularPeer } from './isCircularPeer' * @param [options={}] Options. * @returns Promised {packageName: peer dependencies} collection */ -async function getPeerDependenciesFromRegistry(packageMap: Index, options: Options) { +async function getPeerDependenciesFromRegistry(packageMap: Index, options: Options) { const packageManager = getPackageManager(options, options.packageManager) if (!packageManager.getPeerDependencies) return {} diff --git a/src/lib/isCircularPeer.ts b/src/lib/isCircularPeer.ts deleted file mode 100644 index 15922fb0..00000000 --- a/src/lib/isCircularPeer.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { Index } from '../types/IndexType' - -type CircularData = - | { - isCircular: true - offendingPackage: string - } - | { - isCircular: false - } - -/** - * Checks if the specified package will create a loop of peer dependencies by traversing all paths to find a cycle - * - * If a cycle was found, the offending peer dependency of the specified package is returned - */ -export function isCircularPeer(peerDependencies: Index>, packageName: string): CircularData { - let queue = [[packageName]] - while (queue.length > 0) { - const nextQueue: string[][] = [] - for (const path of queue) { - const parents = Object.keys(peerDependencies[path[0]] ?? {}) - for (const name of parents) { - if (name === path.at(-1)) { - return { - isCircular: true, - offendingPackage: path[0], - } - } - nextQueue.push([name, ...path]) - } - } - queue = nextQueue - } - return { - isCircular: false, - } -} diff --git a/test/peer.test.ts b/test/peer.test.ts index e40e31a4..bd654336 100644 --- a/test/peer.test.ts +++ b/test/peer.test.ts @@ -5,7 +5,7 @@ import chaiSetup from './helpers/chaiSetup' chaiSetup() describe('peer dependencies', function () { - it('peer dependencies of installed packages are ignored by default', async () => { + it('peer dependencies are ignored by default', async () => { const cwd = path.join(__dirname, 'test-data/peer/') const upgrades = await ncu({ cwd }) upgrades!.should.deep.equal({ @@ -13,7 +13,7 @@ describe('peer dependencies', function () { }) }) - it('peer dependencies of installed packages are checked when using option peer', async () => { + it('peer dependencies are checked when using option peer', async () => { const cwd = path.join(__dirname, 'test-data/peer/') const upgrades = await ncu({ cwd, peer: true }) upgrades!.should.deep.equal({ @@ -21,7 +21,7 @@ describe('peer dependencies', function () { }) }) - it('peer dependencies of installed packages are checked iteratively when using option peer', async () => { + it('peer dependencies are checked iteratively when using option peer', async () => { const cwd = path.join(__dirname, 'test-data/peer-update/') const upgrades = await ncu({ cwd, peer: true }) upgrades!.should.deep.equal({