From 0fc0fe2036e69bc87487c06d34dfc618a5493266 Mon Sep 17 00:00:00 2001 From: Kanad Gupta <8854718+kanadgupta@users.noreply.github.com> Date: Mon, 28 Nov 2022 19:09:01 -0600 Subject: [PATCH] refactor: add ability to grab major version + test this will set up some upcoming tagging changes --- __tests__/lib/getPkgVersion.test.ts | 5 +++++ src/lib/getPkgVersion.ts | 22 ++++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/__tests__/lib/getPkgVersion.test.ts b/__tests__/lib/getPkgVersion.test.ts index 0d957ff76..9a655593e 100644 --- a/__tests__/lib/getPkgVersion.test.ts +++ b/__tests__/lib/getPkgVersion.test.ts @@ -46,4 +46,9 @@ describe('#getPkgVersion()', () => { mock.done(); }); + + it('should grab major version from package.json', async () => { + const result = await getPkgVersion('major'); + return expect(result).not.toContain('.'); + }); }); diff --git a/src/lib/getPkgVersion.ts b/src/lib/getPkgVersion.ts index 136f01121..3f2671b29 100644 --- a/src/lib/getPkgVersion.ts +++ b/src/lib/getPkgVersion.ts @@ -18,23 +18,33 @@ export function getNodeVersion() { /** * The current `rdme` version * - * @param npmDistTag the `npm` dist tag to retrieve. If this value is omitted, - * the version from the `package.json` is returned. - * @example "8.0.0" + * @param type the value to retrieve. + * + * If set to `latest`, it returns the `latest` dist tag from the `npm` registry. * @see {@link https://docs.npmjs.com/cli/dist-tag} + * + * If `type` is set to `major`, the major version from the `package.json` is returned. + * + * If `type` is omitted, the raw version from the `package.json` is returned. + * @example "8.0.0" * @note we mock this function in our snapshots, hence it's not the default * @see {@link https://stackoverflow.com/a/54245672} */ -export async function getPkgVersion(npmDistTag?: 'latest' | 'next'): Promise { - if (npmDistTag) { +export async function getPkgVersion(type?: 'latest' | 'major'): Promise { + if (type === 'latest') { return fetch(registryUrl) .then(res => res.json()) - .then(body => body['dist-tags'][npmDistTag]) + .then(body => body['dist-tags'][type]) .catch(err => { // eslint-disable-next-line no-console console.error('error fetching version from npm registry:', err.message); return pkg.version; }); } + + if (type === 'major') { + return semver.major(pkg.version).toString(); + } + return pkg.version; }