UNPKG

anomaly-packer

Version:

Anomaly Packer is a utility package for STALKER Anomaly creators to help them develop addons at speed with TypeScript's type-safety and game-oriented build tools.

46 lines (45 loc) 1.56 kB
import { existsSync, readFileSync } from "fs"; import path from "path"; import { ZipArchive } from "archiver"; import { createWriteStream } from "node:fs"; //#region src/zip.ts function readManifest(cwd) { const manifest = path.join(cwd, "package.json"); if (!existsSync(manifest)) return {}; try { return JSON.parse(readFileSync(manifest, "utf8")); } catch { return {}; } } /** `<base>-<version>`, with the base defaulting to the package name and then the directory. */ function archiveName(cwd, name) { const pkg = readManifest(cwd); const base = name ?? pkg.name ?? path.basename(cwd); if (!pkg.version || base.endsWith(`-${pkg.version}`)) return base; return `${base}-${pkg.version}`; } async function zipBuild(options = {}) { const cwd = options.cwd ?? process.cwd(); const buildDir = path.resolve(cwd, options.buildDir ?? "build"); const outDir = path.resolve(cwd, options.outDir ?? "."); const outFile = path.join(outDir, `${archiveName(cwd, options.name)}.zip`); if (!existsSync(buildDir)) throw new Error(`Nothing to zip: ${buildDir} does not exist. Run the build first.`); const output = createWriteStream(outFile); const archive = new ZipArchive({ zlib: { level: 9 } }); const done = new Promise((resolve, reject) => { output.on("close", () => resolve()); archive.on("warning", reject); archive.on("error", reject); }); archive.pipe(output); archive.directory(buildDir, false); await archive.finalize(); await done; return { file: outFile, bytes: archive.pointer() }; } //#endregion export { zipBuild as t };