Files
descrybe/scripts/zip.mjs
T

183 lines
4.7 KiB
JavaScript
Raw Normal View History

2026-10-14 16:13:51 +02:00
#!/usr/bin/env node
/**
* Source snapshot zip: tracked + untracked files that are not gitignored.
* Excludes node_modules, .git, secrets, build artifacts, and the zip itself.
*
* Usage: npm run zip
* Output: <repo>/descrybe.zip (entries prefixed with descrybe-v2/)
*/
import { spawnSync } from "node:child_process";
import fs from "node:fs";
import path from "node:path";
import { crc32, deflateRawSync } from "node:zlib";
import { fileURLToPath } from "node:url";
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const prefix = "descrybe-v2/";
const outName = "descrybe.zip";
const outPath = path.join(root, outName);
const BLOCKED_DIRS = new Set([
".bin",
".codehelper",
".git",
".idea",
".svelte-kit",
".vscode",
"coverage",
"dist",
"node_modules",
]);
function posixRel(p) {
return p.replaceAll("\\", "/");
}
function shouldSkip(rel) {
const n = posixRel(rel);
if (!n || n === outName || n.endsWith(".zip")) return true;
if (n === ".env" || n.endsWith("/.env")) return true;
if (n.startsWith("data/uploads/") || n.startsWith("apps/api/data/uploads/")) return true;
if (n.startsWith("bin/") || n.startsWith("apps/api/bin/")) return true;
const parts = n.split("/");
return parts.some((part) => BLOCKED_DIRS.has(part));
}
function listSourceFiles() {
const r = spawnSync(
"git",
["ls-files", "-z", "--cached", "--others", "--exclude-standard"],
{ cwd: root, encoding: "buffer", maxBuffer: 32 * 1024 * 1024 },
);
if (r.error || r.status !== 0) {
const err = r.stderr?.toString("utf8")?.trim() || r.error?.message || "git ls-files failed";
console.error(`[zip] ${err}`);
process.exit(1);
}
const raw = r.stdout.toString("utf8");
return raw
.split("\0")
.filter(Boolean)
.filter((rel) => !shouldSkip(rel))
.filter((rel) => {
const abs = path.join(root, rel);
try {
return fs.statSync(abs).isFile();
} catch {
return false;
}
})
.sort();
}
function dosDateTime(d) {
const year = d.getFullYear();
if (year < 1980) return { time: 0, date: 0x21 };
const time = (d.getHours() << 11) | (d.getMinutes() << 5) | Math.floor(d.getSeconds() / 2);
const date = ((year - 1980) << 9) | ((d.getMonth() + 1) << 5) | d.getDate();
return { time, date };
}
function u16(n) {
const b = Buffer.alloc(2);
b.writeUInt16LE(n & 0xffff, 0);
return b;
}
function u32(n) {
const b = Buffer.alloc(4);
b.writeUInt32LE(n >>> 0, 0);
return b;
}
function main() {
const files = listSourceFiles();
if (files.length === 0) {
console.error("[zip] no files to archive");
process.exit(1);
}
const tmpPath = `${outPath}.tmp`;
const out = fs.createWriteStream(tmpPath);
const central = [];
let offset = 0;
let bytes = 0;
function write(buf) {
out.write(buf);
offset += buf.length;
}
for (const rel of files) {
const abs = path.join(root, rel);
const data = fs.readFileSync(abs);
const st = fs.statSync(abs);
const compressed = deflateRawSync(data, { level: 9 });
const useStore = compressed.length >= data.length;
const payload = useStore ? data : compressed;
const method = useStore ? 0 : 8;
const name = Buffer.from(`${prefix}${posixRel(rel)}`, "utf8");
const { time, date } = dosDateTime(st.mtime);
const crc = crc32(data) >>> 0;
const localOff = offset;
write(Buffer.from("PK\u0003\u0004", "binary"));
write(u16(20));
write(u16(0x0800));
write(u16(method));
write(u16(time));
write(u16(date));
write(u32(crc));
write(u32(payload.length));
write(u32(data.length));
write(u16(name.length));
write(u16(0));
write(name);
write(payload);
const cd = Buffer.concat([
Buffer.from("PK\u0001\u0002", "binary"),
u16(20),
u16(20),
u16(0x0800),
u16(method),
u16(time),
u16(date),
u32(crc),
u32(payload.length),
u32(data.length),
u16(name.length),
u16(0),
u16(0),
u16(0),
u16(0),
u32(0),
u32(localOff),
name,
]);
central.push(cd);
bytes += data.length;
}
const cdStart = offset;
for (const rec of central) write(rec);
const cdSize = offset - cdStart;
write(Buffer.from("PK\u0005\u0006", "binary"));
write(u16(0));
write(u16(0));
write(u16(files.length));
write(u16(files.length));
write(u32(cdSize));
write(u32(cdStart));
write(u16(0));
out.end(() => {
fs.renameSync(tmpPath, outPath);
const mb = (fs.statSync(outPath).size / (1024 * 1024)).toFixed(2);
const srcMb = (bytes / (1024 * 1024)).toFixed(2);
console.log(`[zip] wrote ${outName} (${files.length} files, ${srcMb} MB source → ${mb} MB zip)`);
});
}
main();