// SPDX-License-Identifier: AGPL-3.0-or-later // Packs the built extension (dist/) for the stores, into release/: // // docwell-web-clipper--chrome.zip Chrome Web Store, Edge Add-ons // docwell-web-clipper--firefox.zip addons.mozilla.org // docwell-web-clipper--source.zip the source AMO asks for, since dist/ is built // // Run `npm run package -w @docwell/web-clipper` (it builds first). Uses only // Node's own modules: the zips are written by hand with zlib's deflate. import { deflateRawSync } from "node:zlib"; import { mkdirSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs"; import { dirname, join, relative, resolve, sep } from "node:path"; import { fileURLToPath } from "node:url"; const root = resolve(dirname(fileURLToPath(import.meta.url)), ".."); const dist = join(root, "dist"); const out = join(root, "release"); const crcTable = new Uint32Array(256).map((_, n) => { let c = n; for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1; return c >>> 0; }); function crc32(buf) { let c = 0xffffffff; for (const b of buf) c = crcTable[(c ^ b) & 0xff] ^ (c >>> 8); return (c ^ 0xffffffff) >>> 0; } // A fixed time, so the same files make the same zip. const DOS_TIME = 0; const DOS_DATE = ((2024 - 1980) << 9) | (1 << 5) | 1; /** Writes files ({name, data}) as a zip. */ function zip(path, files) { const parts = []; const central = []; let offset = 0; for (const { name, data } of files) { const nameBuf = Buffer.from(name, "utf8"); const packed = deflateRawSync(data, { level: 9 }); const stored = packed.length >= data.length; const body = stored ? data : packed; const crc = crc32(data); const local = Buffer.alloc(30); local.writeUInt32LE(0x04034b50, 0); local.writeUInt16LE(20, 4); local.writeUInt16LE(0x0800, 6); // UTF-8 names local.writeUInt16LE(stored ? 0 : 8, 8); local.writeUInt16LE(DOS_TIME, 10); local.writeUInt16LE(DOS_DATE, 12); local.writeUInt32LE(crc, 14); local.writeUInt32LE(body.length, 18); local.writeUInt32LE(data.length, 22); local.writeUInt16LE(nameBuf.length, 26); local.writeUInt16LE(0, 28); parts.push(local, nameBuf, body); const entry = Buffer.alloc(46); entry.writeUInt32LE(0x02014b50, 0); entry.writeUInt16LE(20, 4); entry.writeUInt16LE(20, 6); entry.writeUInt16LE(0x0800, 8); entry.writeUInt16LE(stored ? 0 : 8, 10); entry.writeUInt16LE(DOS_TIME, 12); entry.writeUInt16LE(DOS_DATE, 14); entry.writeUInt32LE(crc, 16); entry.writeUInt32LE(body.length, 20); entry.writeUInt32LE(data.length, 24); entry.writeUInt16LE(nameBuf.length, 28); entry.writeUInt32LE(offset, 42); central.push(entry, nameBuf); offset += local.length + nameBuf.length + body.length; } const centralBuf = Buffer.concat(central); const end = Buffer.alloc(22); end.writeUInt32LE(0x06054b50, 0); end.writeUInt16LE(files.length, 8); end.writeUInt16LE(files.length, 10); end.writeUInt32LE(centralBuf.length, 12); end.writeUInt32LE(offset, 16); writeFileSync(path, Buffer.concat([...parts, centralBuf, end])); console.log(`${relative(root, path)} (${files.length} files)`); } /** Every file under dir, as zip names relative to base. */ function walk(dir, base, skip = () => false) { const files = []; for (const entry of readdirSync(dir).sort()) { const full = join(dir, entry); const name = relative(base, full).split(sep).join("/"); if (skip(name)) continue; if (statSync(full).isDirectory()) files.push(...walk(full, base, skip)); else files.push({ name, data: readFileSync(full) }); } return files; } const manifest = JSON.parse(readFileSync(join(dist, "manifest.json"), "utf8")); const version = manifest.version; mkdirSync(out, { recursive: true }); const built = walk(dist, dist).filter((f) => f.name !== "manifest.json"); const withManifest = (m) => [{ name: "manifest.json", data: Buffer.from(JSON.stringify(m, null, 2) + "\n") }, ...built]; // Chrome and Edge do not know Firefox's settings and warn about them. const chrome = { ...manifest }; delete chrome.browser_specific_settings; zip(join(out, `docwell-web-clipper-${version}-chrome.zip`), withManifest(chrome)); zip(join(out, `docwell-web-clipper-${version}-firefox.zip`), withManifest(manifest)); // AMO reviews the code the extension is built from: this folder as // Web-Clipper/, next to the repository files its build reads. It builds on // its own: cd Web-Clipper && npm install && npm run build. const repo = resolve(root, ".."); const source = walk(root, root, (n) => /^(node_modules|dist|release)(\/|$)/.test(n)).map((f) => ({ ...f, name: `Web-Clipper/${f.name}`, })); for (const f of ["tsconfig.base.json", "LICENSE"]) source.push({ name: f, data: readFileSync(join(repo, f)) }); zip(join(out, `docwell-web-clipper-${version}-source.zip`), source);