patchImportsOf

tcmts.cli.main.patchImportsOf · #wthlg6tm · term · runs anywhere

The imports a patch asks for (--imports, or a patch's imports in --patches), from its JSON: each a name and where it's from, and maybe as and type.

import type { PatchImport } from "#codebase/tcmts/cli/main";
import { UsageError } from "#codebase/tcmts/cli/ui";

export function patchImportsOf(value: unknown, where: string): PatchImport[] {
  if (value === undefined) return [];
  if (!Array.isArray(value))
    throw new UsageError(
      `${where} isn't a list of imports: [{"name": "…", "from": "…", "as": "…"}, …]`,
    );
  return value.map(
    (v: { name?: unknown; from?: unknown; as?: unknown; type?: unknown } | null) => ({
      name: String(v?.name ?? ""),
      from: String(v?.from ?? ""),
      ...(v?.as !== undefined && v.as !== null ? { as: String(v.as) } : {}),
      ...(v?.type === true ? { type: true } : {}),
    }),
  );
}

uses

used by