示例工程 50-paths-and-assets

50-paths-and-assets/demo-02-asar-layout/asar.ts

typescript · 2.0 KB

type AsarPath = {
  archive: string;
  inner: string;
  file: string;
};

function parseAsarPath(value: string): AsarPath | null {
  const index = value.indexOf(".asar/");
  if (index >= 0) {
    const archive = value.slice(0, index + ".asar".length);
    const inner = value.slice(index + ".asar/".length);
    return {
      archive,
      inner,
      file: `${archive.split("/").pop()}/${inner}`,
    };
  }
  if (value.endsWith(".asar")) {
    return { archive: value, inner: "", file: "" };
  }
  return null;
}

type Layout = {
  appPath: string;
  resourcesPath: string;
  extraFile: string;
  bundledFile: string;
};

function layoutFor(packaged: boolean): Layout {
  if (packaged) {
    const resourcesPath = "/Applications/PathsAndAssets.app/Contents/Resources";
    const appPath = `${resourcesPath}/app.asar`;
    return {
      appPath,
      resourcesPath,
      extraFile: `${resourcesPath}/extra/extra.txt`,
      bundledFile: `${appPath}/.vite/build/assets/notice.txt`,
    };
  }
  const appPath = "/Users/demo/project/app";
  return {
    appPath,
    resourcesPath: "/Users/demo/Electron.app/Contents/Resources",
    extraFile: `${appPath}/resources/extra/extra.txt`,
    bundledFile: `${appPath}/.vite/build/assets/notice.txt`,
  };
}

const packaged = layoutFor(true);
const dev = layoutFor(false);

const bundledAsar = parseAsarPath(packaged.bundledFile);
const extraAsar = parseAsarPath(packaged.extraFile);
const devBundledAsar = parseAsarPath(dev.bundledFile);

const ok =
  parseAsarPath("/a/app.asar")?.archive === "/a/app.asar" &&
  parseAsarPath("/a/app.asar")?.inner === "" &&
  bundledAsar?.archive.endsWith("app.asar") === true &&
  bundledAsar?.inner === ".vite/build/assets/notice.txt" &&
  bundledAsar?.file === "app.asar/.vite/build/assets/notice.txt" &&
  extraAsar === null &&
  devBundledAsar === null &&
  packaged.appPath.endsWith("app.asar") &&
  !dev.appPath.includes("app.asar");

console.log(
  JSON.stringify({
    evt: "smoke",
    ok,
    packaged: { ...packaged, bundledAsar },
    dev: { ...dev, devBundledAsar },
  }),
);