Skip to content

Commit

Permalink
fix: yarn getPeerDependencies doesnt work on private reporsitories wh…
Browse files Browse the repository at this point in the history
…en credentials are only set for yarn and not npm #1470
  • Loading branch information
rbnayax committed Oct 27, 2024
1 parent 264b7e6 commit 49d8e07
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/package-managers/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ const npmConfig = findNpmConfig()
* @param data
* @returns
*/
function parseJson<R>(result: string, data: { command?: string; packageName?: string }): R {
export function parseJson<R>(result: string, data: { command?: string; packageName?: string }): R {
let json
try {
json = JSON.parse(result)
Expand Down
28 changes: 23 additions & 5 deletions src/package-managers/yarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ function parseJsonLines(result: string): Promise<{ dependencies: Index<ParsedDep
})
}

const cmd = process.platform === 'win32' ? 'yarn.cmd' : 'yarn'

/**
* Spawn yarn requires a different command on Windows.
*
Expand All @@ -195,8 +197,6 @@ async function spawnYarn(
spawnPleaseOptions: SpawnPleaseOptions = {},
spawnOptions: SpawnOptions = {},
): Promise<string> {
const cmd = process.platform === 'win32' ? 'yarn.cmd' : 'yarn'

const fullArgs = [
...(yarnOptions.global ? ['global'] : []),
...(yarnOptions.prefix ? [`--prefix=${yarnOptions.prefix}`] : []),
Expand Down Expand Up @@ -226,8 +226,6 @@ export async function defaultPrefix(options: Options): Promise<string | null> {
return Promise.resolve(options.prefix)
}

const cmd = process.platform === 'win32' ? 'yarn.cmd' : 'yarn'

const { stdout: prefix } = await spawn(cmd, ['global', 'dir'])
// yarn 2.0 does not support yarn global
// catch error to prevent process from crashing
Expand Down Expand Up @@ -294,7 +292,27 @@ export const newest = withNpmConfigFromYarn(npm.newest)
export const patch = withNpmConfigFromYarn(npm.patch)
export const semver = withNpmConfigFromYarn(npm.semver)

export { getPeerDependencies } from './npm'
/**
* Fetches the list of peer dependencies for a specific package version.
*
* @param packageName
* @param version
* @returns Promised {packageName: version} collection
*/
export const getPeerDependencies = async (packageName: string, version: Version): Promise<Index<Version>> => {
const { stdout: yarnVersion } = await spawn(cmd, ['--version'], { rejectOnError: false }, {})
if (yarnVersion.startsWith('1')) {
const args = ['--json', 'info', `${packageName}@${version}`, 'peerDependencies']
const { stdout } = await spawn(cmd, args, { rejectOnError: false }, {})
return stdout ? npm.parseJson<{ data?: Index<Version> }>(stdout, { command: args.join(' ') }).data || {} : {}
} else {
const args = ['--json', 'npm', 'info', `${packageName}@${version}`, '--fields', 'peerDependencies']
const { stdout } = await spawn(cmd, args, { rejectOnError: false }, {})
return stdout
? npm.parseJson<{ peerDependencies?: Index<Version> }>(stdout, { command: args.join(' ') }).peerDependencies || {}
: {}
}
}

/**
* Fetches the engines list from the registry for a specific package version.
Expand Down
4 changes: 2 additions & 2 deletions test/package-managers/npm/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ describe('npm', function () {
})

it('getPeerDependencies', async () => {
await npm.getPeerDependencies('ncu-test-return-version', '1.0').should.eventually.deep.equal({})
await npm.getPeerDependencies('ncu-test-peer', '1.0').should.eventually.deep.equal({
await npm.getPeerDependencies('ncu-test-return-version', '1.0.0').should.eventually.deep.equal({})
await npm.getPeerDependencies('ncu-test-peer', '1.0.0').should.eventually.deep.equal({
'ncu-test-return-version': '1.x',
})
})
Expand Down
7 changes: 7 additions & 0 deletions test/package-managers/yarn/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ describe('yarn', function () {
await yarn.list({ cwd: testDir }, localYarnSpawnOptions).should.eventually.be.rejectedWith(lockFileErrorMessage)
})

it('getPeerDependencies', async () => {
await yarn.getPeerDependencies('ncu-test-return-version', '1.0.0').should.eventually.deep.equal({})
await yarn.getPeerDependencies('ncu-test-peer', '1.0.0').should.eventually.deep.equal({
'ncu-test-return-version': '1.x',
})
})

describe('npmAuthTokenKeyValue', () => {
it('npmRegistryServer with trailing slash', () => {
const authToken = yarn.npmAuthTokenKeyValue({}, 'fortawesome', {
Expand Down

0 comments on commit 49d8e07

Please sign in to comment.