示例工程 55-installers-and-channels

55-installers-and-channels/demo-01-channel-decision/decide.ts

typescript · 2.6 KB

type Requirement = {
  platform: "macos" | "windows" | "linux";
  store: boolean;
  enterprise: boolean;
  autoUpdate: boolean;
};

type Decision = {
  channel: string;
  artifact: string;
  reason: string;
};

function decide(req: Requirement): Decision {
  if (req.store) {
    if (req.platform === "linux") {
      return {
        channel: "flathub",
        artifact: "flatpak",
        reason: "商店负责更新与沙箱",
      };
    }
    if (req.platform === "windows") {
      return {
        channel: "msstore",
        artifact: "MSIX",
        reason: "商店与应用身份",
      };
    }
    return { channel: "mas", artifact: "pkg", reason: "Apple 审核与沙箱" };
  }
  if (req.enterprise) {
    return {
      channel: "direct",
      artifact:
        req.platform === "windows"
          ? "msi"
          : req.platform === "macos"
            ? "pkg"
            : "deb",
      reason: "内部署,安装器可控",
    };
  }
  if (req.autoUpdate) {
    return {
      channel: "direct",
      artifact:
        req.platform === "windows"
          ? "squirrel"
          : req.platform === "macos"
            ? "zip"
            : "AppImage",
      reason: "应用内更新",
    };
  }
  return {
    channel: "direct",
    artifact: req.platform === "linux" ? "AppImage" : "dmg / nsis",
    reason: "手动更新",
  };
}

const cases: Array<[string, Requirement]> = [
  [
    "mac-auto",
    { platform: "macos", store: false, enterprise: false, autoUpdate: true },
  ],
  [
    "win-auto",
    { platform: "windows", store: false, enterprise: false, autoUpdate: true },
  ],
  [
    "win-enterprise",
    { platform: "windows", store: false, enterprise: true, autoUpdate: false },
  ],
  [
    "mac-store",
    { platform: "macos", store: true, enterprise: false, autoUpdate: false },
  ],
  [
    "linux-store",
    { platform: "linux", store: true, enterprise: false, autoUpdate: true },
  ],
  [
    "linux-manual",
    { platform: "linux", store: false, enterprise: false, autoUpdate: false },
  ],
];

const results = cases.map(([label, req]) => ({
  label,
  platform: req.platform,
  ...decide(req),
}));

const byLabel = Object.fromEntries(results.map((row) => [row.label, row]));

const ok =
  byLabel["mac-auto"]?.artifact === "zip" &&
  byLabel["win-auto"]?.artifact === "squirrel" &&
  byLabel["win-enterprise"]?.artifact === "msi" &&
  byLabel["mac-store"]?.channel === "mas" &&
  byLabel["linux-store"]?.channel === "flathub" &&
  byLabel["linux-manual"]?.artifact === "AppImage" &&
  results.every((row) => row.channel !== "" && row.artifact !== "");

console.log(JSON.stringify({ evt: "smoke", ok, results }));