yarn-plugin: memoize manifest fetching
Signed-off-by: MT Lewis <mtlewis@users.noreply.github.com>
This commit is contained in:
@@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user