All files / libs/tools/plugin/src/executors/gitlab-badge-updater getPackageJsonVersions.ts

0% Statements 0/44
0% Branches 0/1
0% Functions 0/1
0% Lines 0/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45                                                                                         
import { resolve } from 'node:path';

import { readJsonFile } from '@nx/devkit';
import { get, mapValues } from 'lodash-es';

type PackageJson = {
  dependencies?: {
    react?: string;
  };
  devDependencies?: {
    nx?: string;
    '@nestjs/core'?: string;
  };
  volta?: {
    node?: string;
    yarn?: string;
  };
};

// Packages we'll need
export enum PackageNames {
  NODE = 'NODE',
  YARN = 'YARN',
  NX = 'NX',
  NEST = 'NEST',
  REACT = 'REACT',
}

const packagePaths = {
  [PackageNames.NODE]: 'volta.node',
  [PackageNames.YARN]: 'volta.yarn',
  [PackageNames.NX]: 'devDependencies.nx',
  [PackageNames.NEST]: 'dependencies.@nestjs/core',
  [PackageNames.REACT]: 'dependencies.react',
} as const satisfies Record<PackageNames, string>;

const packageJsonPath = resolve('package.json');

export function getPackageJsonVersions(): Record<PackageNames, string | undefined> {
  // Parse package.json file into an object and use lodash get method to get the version of each package by mapping into packagePaths
  const packageJson = readJsonFile<PackageJson>(packageJsonPath);

  return mapValues(packagePaths, (path) => get(packageJson, path)?.replaceAll(/["\s,^~]/gu, ''));
}