yarn-plugin: memoize manifest fetching

Signed-off-by: MT Lewis <mtlewis@users.noreply.github.com>
This commit is contained in:
MT Lewis
2024-07-27 14:04:22 +01:00
parent 2bae52597d
commit b207f69ba1
4 changed files with 38 additions and 1 deletions
+1
View File
@@ -35,6 +35,7 @@
"@yarnpkg/core": "^4.0.3",
"@yarnpkg/fslib": "^3.0.2",
"chalk": "^4.0.0",
"lodash": "^4.17.21",
"semver": "^7.6.0"
},
"devDependencies": {
@@ -16,6 +16,7 @@
import { structUtils } from '@yarnpkg/core';
import { npath, ppath } from '@yarnpkg/fslib';
import { getManifestByVersion } from '@backstage/release-manifests';
import { BackstageResolver } from './BackstageResolver';
import { createMockDirectory } from '@backstage/backend-test-utils';
@@ -31,6 +32,10 @@ jest.mock('@backstage/release-manifests', () => ({
}),
}));
const getManifestByVersionMock = getManifestByVersion as jest.MockedFunction<
typeof getManifestByVersion
>;
describe('BackstageResolver', () => {
const mockDir = createMockDirectory();
let backstageResolver: BackstageResolver;
@@ -177,5 +182,29 @@ describe('BackstageResolver', () => {
),
).rejects.toThrow(/invalid backstage version/i);
});
it('memoizes manifest retrieval', async () => {
const descriptor1 = structUtils.makeDescriptor(
structUtils.makeIdent('backstage', 'core'),
'backstage:1.23.45',
);
for (let i = 0; i < 5; i++) {
await backstageResolver.getCandidates(descriptor1);
}
expect(getManifestByVersionMock).toHaveBeenCalledTimes(1);
const descriptor2 = structUtils.makeDescriptor(
structUtils.makeIdent('backstage', 'core'),
'backstage:6.78.90',
);
for (let i = 0; i < 5; i++) {
await backstageResolver.getCandidates(descriptor2);
}
expect(getManifestByVersionMock).toHaveBeenCalledTimes(2);
});
});
});
+7 -1
View File
@@ -16,11 +16,17 @@
import { ppath, xfs } from '@yarnpkg/fslib';
import { valid as semverValid } from 'semver';
import { getManifestByVersion } from '@backstage/release-manifests';
import { memoize } from 'lodash';
import { getManifestByVersion as getManifestByVersionBase } from '@backstage/release-manifests';
import { BACKSTAGE_JSON, findPaths } from '@backstage/cli-common';
import { Descriptor, structUtils } from '@yarnpkg/core';
import { PROTOCOL } from './constants';
const getManifestByVersion = memoize(
getManifestByVersionBase,
({ version }) => version,
);
export const getCurrentBackstageVersion = () => {
const workspaceRoot = ppath.resolve(findPaths(ppath.cwd()).targetRoot);
+1
View File
@@ -45239,6 +45239,7 @@ __metadata:
"@yarnpkg/core": ^4.0.3
"@yarnpkg/fslib": ^3.0.2
chalk: ^4.0.0
lodash: ^4.17.21
nodemon: ^3.0.1
semver: ^7.6.0
languageName: unknown