Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
CreativeTechGuy committed Mar 21, 2024
1 parent 7e5debd commit 676c57d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 44 deletions.
42 changes: 39 additions & 3 deletions src/lib/getPeerDependenciesFromRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Index<string>>, 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.
Expand All @@ -13,7 +49,7 @@ import { isCircularPeer } from './isCircularPeer'
* @param [options={}] Options.
* @returns Promised {packageName: peer dependencies} collection
*/
async function getPeerDependenciesFromRegistry(packageMap: Index<VersionSpec>, options: Options) {
async function getPeerDependenciesFromRegistry(packageMap: Index<Version>, options: Options) {
const packageManager = getPackageManager(options, options.packageManager)
if (!packageManager.getPeerDependencies) return {}

Expand Down
38 changes: 0 additions & 38 deletions src/lib/isCircularPeer.ts

This file was deleted.

6 changes: 3 additions & 3 deletions test/peer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ 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({
'ncu-test-return-version': '2.0.0',
})
})

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({
'ncu-test-return-version': '1.1.0',
})
})

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({
Expand Down

0 comments on commit 676c57d

Please sign in to comment.