UNPKG

everything-dev

Version:

A consolidated product package for building Module Federation apps with oRPC APIs.

50 lines (48 loc) • 1.97 kB
import { computeSriHash } from "./integrity.mjs"; import { listWorkspaceSubtreeFiles } from "../../utils/workspace-subtree.mjs"; import { mkdtempSync, readFileSync, renameSync, rmSync } from "node:fs"; import { join } from "node:path"; import { createHash } from "node:crypto"; import { tmpdir } from "node:os"; import { create } from "tar"; //#region src/services/deploy/source-archive.ts /** Temp directory for a source archive leg (caller passes it as `outDir`). */ function makeSourceArchiveTempDir(prefix = "bos-source-archive-") { return mkdtempSync(join(tmpdir(), prefix)); } /** * Build the workspace's source archive in `outDir`: the shared subtree ignore * set (`listWorkspaceSubtreeFiles`) selects the files; `tar.create` receives * that explicit list, so its own ignore logic never re-derives the selection. * The archive name is content-addressed — identical source bytes produce the * identical `source.<16hex>.tar.gz` (gzip is deterministic for the same input * bytes; file mtimes differ only when source files change, which changes the * name anyway). */ async function buildSourceArchive(input) { const files = await listWorkspaceSubtreeFiles(input.repoRoot, input.workspace); if (files.length === 0) throw new Error(`[source-archive] workspace subtree is empty: ${input.workspace}`); const pendingPath = join(input.outDir, ".pending.tar.gz"); await create({ file: pendingPath, cwd: input.repoRoot, portable: true, gzip: { level: 9 } }, files); const bytes = readFileSync(pendingPath); const name = `source.${createHash("sha256").update(bytes).digest("hex").slice(0, 16)}.tar.gz`; const path = join(input.outDir, name); renameSync(pendingPath, path); return { file: `source/${name}`, path, integrity: computeSriHash(bytes), cleanup: () => rmSync(input.outDir, { recursive: true, force: true }) }; } //#endregion export { buildSourceArchive, makeSourceArchiveTempDir }; //# sourceMappingURL=source-archive.mjs.map