mc-publish/tests/utils/zip-utils.ts
2024-10-08 19:11:03 +09:00

18 lines
687 B
TypeScript

import { basename, resolve as resolvePath } from "node:path";
import Zip from "adm-zip";
export function zipFile(path: string | string[], zipPath?: string): Buffer {
const fs = jest.requireActual<typeof import("node:fs")>("node:fs");
const realPath = typeof path === "string" ? path : resolvePath(...path);
zipPath ||= basename(realPath);
const zip = new Zip();
zip.addFile(zipPath, fs.readFileSync(realPath));
return zip.toBuffer();
}
export function zipContent(content: string | Buffer, zipPath: string): Buffer {
const zip = new Zip();
zip.addFile(zipPath, typeof content === "string" ? Buffer.from(content) : content);
return zip.toBuffer();
}