All files / libs/tools/plugin/src/generators/heal/healthpacks package-json.ts

59.18% Statements 29/49
33.33% Branches 1/3
100% Functions 1/1
59.18% Lines 29/49

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 46 47 48 49 501x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x                                         13x 13x 13x 13x 13x 13x 13x 13x  
import { readJson, updateJson, writeJson, type ProjectConfiguration, type Tree } from '@nx/devkit';
import { pick } from 'lodash-es';
import { type PackageJson } from 'type-fest';
 
export const updatePackageJson = (tree: Tree, projectConfiguration: ProjectConfiguration) => {
  const packageJsonPath = `${projectConfiguration.root}/package.json`;
  const projectJsonPath = `${projectConfiguration.root}/project.json`;
 
  let name: string | null = null;
  let nx: object = {};
 
  // If a project.json exist, move its content to package.json and delete it.
  if (tree.exists(projectJsonPath)) {
    const content = readJson<ProjectConfiguration>(tree, projectJsonPath);
    name = content.name ?? null;
    nx = pick(content, ['targets', 'tags', 'projectType', 'implicitDependencies']);
    tree.delete(projectJsonPath);
  }
 
  if (projectConfiguration.name !== '@amalia/monorepo') {
    if (tree.exists(packageJsonPath)) {
      updateJson(tree, packageJsonPath, (json: PackageJson) => {
        const currentNxConfig = json['nx'] as unknown as ProjectConfiguration | undefined;

        const updatedJson = {
          ...json,
          type: undefined,
          name: name ?? json.name,
          nx: { ...currentNxConfig, ...nx },
        };

        if (updatedJson.nx.projectType === 'library') {
          delete updatedJson.version;
          delete updatedJson.main;
          delete updatedJson.types;
          delete updatedJson.private;
          delete updatedJson.dependencies;
        }

        return updatedJson;
      });
    } else {
      writeJson(tree, packageJsonPath, {
        name,
        nx,
      });
    }
  }
};