Files
2026-09-18 09:41:27 +08:00

61 lines
2.2 KiB
JavaScript
Raw Permalink 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.
#!/usr/bin/env node
/**
* 打包:发布构建 + 打成可直接上传 Chrome / 用于自托管自更新的 zip。
*
* 只收运行时文件,源码、脚本、部署产物(updates.xml / install-policy.reg)不入包。
*
* 用法:
* node scripts/package.mjs 构建并打包
* node scripts/package.mjs --skip-build 仅打包(复用现有 dist)
*/
import { mkdir, readFile, readdir, rm, stat } from "node:fs/promises";
import path from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import AdmZip from "adm-zip";
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"];
/** 递归收集待打包文件,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);
}
}
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 });
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)));
}
zip.writeZip(zipPath);
console.log(`[package] v${manifest.version} → ${path.relative(ROOT, zipPath)}(${files.length} 个文件)`);
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") });
}