Files
yaoayanui/src-tauri/scripts/zip-portable.cjs
fengchuanhn@gmail.com ce1fe99883 11
2026-05-22 14:40:32 +08:00

81 lines
2.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 将 target/release 下的可执行文件与 sidecar 资源打成 zip便携分发非安装包
* 在 `npm run tauri:build` 中于 `tauri build` 之后调用。
*/
const fs = require("node:fs");
const path = require("node:path");
const { execSync } = require("node:child_process");
const tauriDir = path.join(__dirname, "..");
const conf = JSON.parse(
fs.readFileSync(path.join(tauriDir, "tauri.conf.json"), "utf8"),
);
const { productName, version } = conf;
const releaseDir = path.join(tauriDir, "target", "release");
const cargoName = "tauri-app";
const exeName =
process.platform === "win32" ? `${cargoName}.exe` : cargoName;
const platformTag =
process.platform === "win32"
? "win"
: process.platform === "darwin"
? "osx"
: "linux";
const archTag = process.arch === "arm64" ? "arm64" : "x64";
const zipDir = path.join(releaseDir, "bundle", "zip");
const zipPath = path.join(
zipDir,
`${productName}_${version}_${archTag}-${platformTag}.zip`,
);
const exePath = path.join(releaseDir, exeName);
if (!fs.existsSync(exePath)) {
console.error(`未找到可执行文件: ${exePath},请先执行 tauri build`);
process.exit(1);
}
const stagingRoot = path.join(releaseDir, "bundle", "_portable_staging");
const stageDir = path.join(stagingRoot, productName);
fs.rmSync(stagingRoot, { recursive: true, force: true });
fs.mkdirSync(stageDir, { recursive: true });
const stagedExe =
process.platform === "win32" ? `${productName}.exe` : productName;
fs.copyFileSync(exePath, path.join(stageDir, stagedExe));
for (const dir of ["resources", "binaries"]) {
const src = path.join(releaseDir, dir);
if (fs.existsSync(src)) {
fs.cpSync(src, path.join(stageDir, dir), { recursive: true });
}
}
fs.mkdirSync(zipDir, { recursive: true });
if (fs.existsSync(zipPath)) {
fs.unlinkSync(zipPath);
}
if (process.platform === "win32") {
const psStage = stageDir.replace(/'/g, "''");
const psZip = zipPath.replace(/'/g, "''");
execSync(
`powershell -NoProfile -Command "Compress-Archive -Path '${psStage}\\*' -DestinationPath '${psZip}' -Force"`,
{ stdio: "inherit" },
);
} else {
execSync(`cd "${stageDir}" && zip -qr "${zipPath}" .`, { stdio: "inherit" });
}
fs.rmSync(stagingRoot, { recursive: true, force: true });
for (const dir of ["msi", "nsis"]) {
fs.rmSync(path.join(releaseDir, "bundle", dir), {
recursive: true,
force: true,
});
}
console.log(`便携压缩包已生成: ${zipPath}`);