diff --git a/src/package-managers/npm.ts b/src/package-managers/npm.ts index 910775d1..df08ee98 100644 --- a/src/package-managers/npm.ts +++ b/src/package-managers/npm.ts @@ -298,7 +298,7 @@ const npmConfig = findNpmConfig() * @param data * @returns */ -function parseJson(result: string, data: { command?: string; packageName?: string }): R { +export function parseJson(result: string, data: { command?: string; packageName?: string }): R { let json try { json = JSON.parse(result) diff --git a/src/package-managers/yarn.ts b/src/package-managers/yarn.ts index e27b864d..36068f48 100644 --- a/src/package-managers/yarn.ts +++ b/src/package-managers/yarn.ts @@ -181,6 +181,8 @@ function parseJsonLines(result: string): Promise<{ dependencies: Index { - const cmd = process.platform === 'win32' ? 'yarn.cmd' : 'yarn' - const fullArgs = [ ...(yarnOptions.global ? ['global'] : []), ...(yarnOptions.prefix ? [`--prefix=${yarnOptions.prefix}`] : []), @@ -226,8 +226,6 @@ export async function defaultPrefix(options: Options): Promise { 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 @@ -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> => { + 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 }>(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 }>(stdout, { command: args.join(' ') }).peerDependencies || {} + : {} + } +} /** * Fetches the engines list from the registry for a specific package version. diff --git a/test/package-managers/npm/index.test.ts b/test/package-managers/npm/index.test.ts index 7ccc1cff..fca5d772 100644 --- a/test/package-managers/npm/index.test.ts +++ b/test/package-managers/npm/index.test.ts @@ -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', }) }) diff --git a/test/package-managers/yarn/index.test.ts b/test/package-managers/yarn/index.test.ts index db2c7386..8a5c63d0 100644 --- a/test/package-managers/yarn/index.test.ts +++ b/test/package-managers/yarn/index.test.ts @@ -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', {