From bf68f0c51888e18bde0bb7a8aa0fc2074a339065 Mon Sep 17 00:00:00 2001 From: Jeff Hansen Date: Tue, 2 Jul 2024 14:50:56 -0400 Subject: [PATCH] fix(package-source): fix check for deprecated package Closes #114 --- src/__tests__/package-source.test.ts | 8 ++++++++ src/package-source.ts | 25 ++++++++++++------------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/__tests__/package-source.test.ts b/src/__tests__/package-source.test.ts index 57a7f3f..cd2b6c8 100644 --- a/src/__tests__/package-source.test.ts +++ b/src/__tests__/package-source.test.ts @@ -6,6 +6,7 @@ test('package source', async () => { expect(result).not.toBeNull() expect(result.name).toBe('awilix') + expect(result.deprecated).toBe(false) expect(result.latestVersion).toBeDefined() // I wrote Awilix, and I know v2 didn't have typings while v3 did, @@ -28,3 +29,10 @@ test('not found', async () => { const result = await source.fetch(`some-nonexistent-package-${Date.now()}`) expect(result).toBeNull() }) + +test('deprecated', async () => { + const source = createPackageSource() + const result = await source.fetch(`@types/escape-goat`) + expect(result).not.toBeNull() + expect(result!.deprecated).toBe(true) +}) diff --git a/src/package-source.ts b/src/package-source.ts index b1d4a7b..4437014 100644 --- a/src/package-source.ts +++ b/src/package-source.ts @@ -28,24 +28,23 @@ export function createPackageSource(): IPackageSource { return null } - const versions = Object.keys(data.versions).map( - (v) => { - const item = data.versions[v] - return { - version: item.version, - containsInternalTypings: !!item.types || !!item.typings, - } - }, - ) + const versionIdentifiers = Object.keys(data.versions) + .sort(compare) + .reverse() + const versions = versionIdentifiers.map((v) => { + const item = data.versions[v] + return { + version: item.version, + containsInternalTypings: !!item.types || !!item.typings, + } + }) return { name: data.name, - deprecated: Boolean(data.deprecated), + deprecated: Boolean(data.versions[versionIdentifiers[0]].deprecated), latestVersion: data['dist-tags'].latest, // Sort by version, highest version first. - versions: versions - .sort((left, right) => compare(left.version, right.version)) - .reverse(), + versions: versions, } }, }