Files

61 lines
2.2 KiB
JavaScript
Raw Permalink Normal View History

2026-09-17 23:00:47 +08:00
#!/usr/bin/env node
/**
* 打包:发布构建 + 打成可直接上传 Chrome / 用于自托管自更新的 zip。
*
* 只收运行时文件,源码、脚本、部署产物(updates.xml / install-policy.reg)不入包。
*
* 用法:
* node scripts/package.mjs 构建并打包
* node scripts/package.mjs --skip-build 仅打包(复用现有 dist)
*/
2026-09-18 09:41:27 +08:00
import { mkdir, readFile, readdir, rm, stat } from "node:fs/promises";
2026-09-17 23:00:47 +08:00
import path from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
2026-09-18 09:41:27 +08:00
import AdmZip from "adm-zip";
2026-09-17 23:00:47 +08:00
import { buildAll } from "./build.mjs";
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
/** 扩展包内容:manifest 及其引用的所有运行时资源 */
const PACKAGE_FILES = ["manifest.json", "popup.html", "popup.css", "icons", "_locales", "dist"];
2026-09-18 09:41:27 +08:00
/** 递归收集待打包文件,zip 内条目路径统一用正斜杠(Chrome Web Store 要求) */
async function collectFiles(rel, out) {
const full = path.join(ROOT, rel);
const s = await stat(full);
if (s.isDirectory()) {
for (const name of await readdir(full)) {
await collectFiles(`${rel}/${name}`, out);
}
} else {
out.push(rel);
}
}
2026-09-17 23:00:47 +08:00
export async function packageExtension({ skipBuild = false } = {}) {
if (!skipBuild) await buildAll({ release: true });
const manifest = JSON.parse(await readFile(path.join(ROOT, "manifest.json"), "utf8"));
const outDir = path.join(ROOT, "release");
await mkdir(outDir, { recursive: true });
const zipPath = path.join(outDir, `auto-login-v${manifest.version}.zip`);
await rm(zipPath, { force: true });
2026-09-18 09:41:27 +08:00
const files = [];
for (const f of PACKAGE_FILES) await collectFiles(f, files);
const zip = new AdmZip();
for (const rel of files.sort()) {
zip.addFile(rel, await readFile(path.join(ROOT, rel)));
2026-09-17 23:00:47 +08:00
}
2026-09-18 09:41:27 +08:00
zip.writeZip(zipPath);
2026-09-17 23:00:47 +08:00
2026-09-18 09:41:27 +08:00
console.log(`[package] v${manifest.version} → ${path.relative(ROOT, zipPath)}(${files.length} 个文件)`);
2026-09-17 23:00:47 +08:00
return { version: manifest.version, zipPath };
}
const isDirectRun = process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href;
if (isDirectRun) {
await packageExtension({ skipBuild: process.argv.includes("--skip-build") });
}