Files
pikasTech-unidesk/src/components/microservices/todo-note/scripts/pack-import.ts
T

37 lines
1.4 KiB
TypeScript

import { createHash } from "node:crypto";
import { chmod, readFile, writeFile } from "node:fs/promises";
import { gzipSync } from "node:zlib";
function option(name: string): string {
const index = Bun.argv.indexOf(name);
const value = index >= 0 ? Bun.argv[index + 1] : undefined;
if (value === undefined || value.length === 0 || value.startsWith("--")) throw new Error(`${name} requires a value`);
return value;
}
async function main(): Promise<void> {
const inputPath = option("--input");
const outputPath = option("--output");
const source = await readFile(inputPath);
JSON.parse(source.toString("utf8"));
const compressed = gzipSync(source, { level: 9 });
const payload = `${JSON.stringify({ confirm: true, snapshotGzipBase64: compressed.toString("base64") })}\n`;
await writeFile(outputPath, payload, { encoding: "utf8", mode: 0o600 });
await chmod(outputPath, 0o600);
process.stdout.write(`${JSON.stringify({
ok: true,
output: outputPath,
sourceBytes: source.length,
compressedBytes: compressed.length,
requestBytes: Buffer.byteLength(payload),
sourceSha256: createHash("sha256").update(source).digest("hex"),
})}\n`);
}
if (import.meta.main && !process.execArgv.includes("--check")) {
main().catch((error) => {
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
process.exitCode = 1;
});
}