Skip to content

Commit

Permalink
fix: npm get peer deps returns list and not object when using range
Browse files Browse the repository at this point in the history
  • Loading branch information
rbnayax committed Aug 7, 2024
1 parent ed0767b commit a07b404
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/package-managers/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,8 @@ export const greatest: GetVersion = async (
export const getPeerDependencies = async (packageName: string, version: Version): Promise<Index<Version>> => {
const args = ['view', `${packageName}@${version}`, 'peerDependencies']
const result = await spawnNpm(args, {}, { rejectOnError: false })
return result ? parseJson(result, { command: [...args, '--json'].join(' ') }) : {}
const parsed = result ? parseJson(result, { command: [...args, '--json'].join(' ') }) : {}
return Array.isArray(parsed) ? parsed.at(-1) : parsed
}

/**
Expand Down
11 changes: 11 additions & 0 deletions test/getPeerDependenciesFromRegistry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ describe('getPeerDependenciesFromRegistry', function () {
})
})

it('single package with range', async () => {
await chalkInit()
const data = await getPeerDependenciesFromRegistry({ 'eslint-plugin-unused-imports': '^4' }, {})
data.should.deep.equal({
'eslint-plugin-unused-imports': {
'@typescript-eslint/eslint-plugin': '^8.0.0-0',
eslint: '^9.0.0',
},
})
})

it('single package empty', async () => {
await chalkInit()
const data = await getPeerDependenciesFromRegistry({ 'ncu-test-return-version': '1.0' }, {})
Expand Down

0 comments on commit a07b404

Please sign in to comment.