diff --git a/.changeset/itchy-seahorses-compare.md b/.changeset/itchy-seahorses-compare.md new file mode 100644 index 00000000000..18ba4642831 --- /dev/null +++ b/.changeset/itchy-seahorses-compare.md @@ -0,0 +1,5 @@ +--- +'@sap-ux/project-access': patch +--- + +Improve function `refreshSpecificationDistTags` - prevent caching `specification-dist-tags.json` if an error is returned in the JSON from `npm view @sap/ux-specification dist-tags --json` diff --git a/packages/project-access/src/project/specification.ts b/packages/project-access/src/project/specification.ts index 80b116c1398..33ad0866909 100644 --- a/packages/project-access/src/project/specification.ts +++ b/packages/project-access/src/project/specification.ts @@ -107,6 +107,10 @@ export async function refreshSpecificationDistTags(options?: { logger?: Logger } logger }); const distTags = JSON.parse(distTagsString) as Record; + if ('error' in distTags) { + // Abort writing cache: received error in dist-tags response + throw new Error(distTagsString); + } await writeFile(specificationDistTagPath, JSON.stringify(distTags, null, 4)); const uniqueVersions = new Set(Object.values(distTags)); diff --git a/packages/project-access/test/project/specification.test.ts b/packages/project-access/test/project/specification.test.ts index cb944688146..64af7002fd9 100644 --- a/packages/project-access/test/project/specification.test.ts +++ b/packages/project-access/test/project/specification.test.ts @@ -151,6 +151,16 @@ describe('Test refreshSpecificationDistTags()', () => { await refreshSpecificationDistTags({ logger }); expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('NPM_ERROR')); }); + + test('Refresh specification dist tags - error in response. Contains code and summary.', async () => { + const refreshResponse = '{"error": {"code": "ENOTFOUND", "summary": "Request to uri failed."}}'; + jest.spyOn(commandMock, 'execNpmCommand').mockResolvedValueOnce(refreshResponse); + const logger = getMockLogger(); + await refreshSpecificationDistTags({ logger }); + expect(logger.error).toHaveBeenCalledWith( + `Error refreshing specification dist-tags: Error: ${refreshResponse}` + ); + }); }); function getMockLogger(): Logger {