Skip to content

Method updateTsConfig doesn't validate if the short import is already added #23449

@Hyperxq

Description

@Hyperxq

🐞 Bug report

File

Command

  • new
  • build
  • serve
  • test
  • e2e
  • generate
  • add
  • update
  • lint
  • extract-i18n
  • run
  • config
  • help
  • version
  • doc

Is this a regression?

No

Description

When you call the method updateTsConfig and you already has this short import in paths.

🔥 Exception or Error

Duplicate Object key

Proposed solution

export function updateTsConfig(packageName: string, ...paths: string[]) {
  return (host: Tree) => {
    if (!host.exists("tsconfig.json")) {
      return host;
    }

    const file = new JSONFile(host, "tsconfig.json");
    const jsonPath = ["compilerOptions", "paths", packageName.toLowerCase()];
    const value: string[] | undefined = file.get(jsonPath) as string[];
    let pathsFiltered: string[] = [];
    if (value) {
      pathsFiltered = paths?.filter((item) => !value!.some((path) => path === item));
    }

    file.modify(jsonPath, Array.isArray(value) ? [...value, ...pathsFiltered] : paths);
  };
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions