示例工程 55-installers-and-channels

55-installers-and-channels/demo-02-installer-model/installers.ts

typescript · 2.1 KB

type Installer = {
  id: string;
  location: string;
  uninstall: string;
  silent: string;
  residue: string;
};

const INSTALLERS: Installer[] = [
  {
    id: "dmg",
    location: "/Applications",
    uninstall: "拖到废纸篓",
    silent: "不支持",
    residue: "~/Library 应用数据",
  },
  {
    id: "pkg",
    location: "/Applications",
    uninstall: "脚本/工具",
    silent: "installer -pkg",
    residue: "receipts、LaunchDaemons",
  },
  {
    id: "squirrel",
    location: "%LocalAppData%",
    uninstall: "应用内/设置",
    silent: "--silent",
    residue: "用户数据、更新缓存",
  },
  {
    id: "nsis",
    location: "Program Files",
    uninstall: "卸载器",
    silent: "/S",
    residue: "注册表、用户数据",
  },
  {
    id: "msi",
    location: "Program Files",
    uninstall: "msiexec",
    silent: "msiexec /qn",
    residue: "注册表、用户数据",
  },
  {
    id: "appimage",
    location: "任意位置",
    uninstall: "删除文件",
    silent: "不适用",
    residue: "~/.config",
  },
];

function find(id: string): Installer {
  const row = INSTALLERS.find((item) => item.id === id);
  if (!row) throw new Error(`UNKNOWN_INSTALLER:${id}`);
  return row;
}

function supportsSilent(id: string): boolean {
  return find(id).silent !== "不支持" && find(id).silent !== "不适用";
}

const dmg = find("dmg");
const nsis = find("nsis");
const appimage = find("appimage");

const ok =
  dmg.location === "/Applications" &&
  dmg.uninstall.includes("废纸篓") &&
  !supportsSilent("dmg") &&
  supportsSilent("nsis") &&
  supportsSilent("msi") &&
  !supportsSilent("appimage") &&
  appimage.uninstall.includes("删除") &&
  INSTALLERS.every((row) => row.residue.length > 0) &&
  INSTALLERS.every((row) => row.uninstall.length > 0) &&
  nsis.silent === "/S";

let unknown = "";
try {
  find("zip");
} catch (error) {
  unknown = error instanceof Error ? error.message : String(error);
}

console.log(
  JSON.stringify({
    evt: "smoke",
    ok: ok && unknown === "UNKNOWN_INSTALLER:zip",
    silent: INSTALLERS.filter((row) => supportsSilent(row.id)).map(
      (row) => row.id,
    ),
    unknown,
  }),
);