(opts: WriteBundleOpts)
| 501 | } |
| 502 | |
| 503 | export async function writeBundle(opts: WriteBundleOpts): Promise<string> { |
| 504 | const { collected, outputPath } = opts; |
| 505 | const parent = dirname(outputPath); |
| 506 | if (!existsSync(parent)) { |
| 507 | throw new Error( |
| 508 | `ok diagnose bundle: parent directory does not exist: ${parent}. ` + |
| 509 | 'Create it or pass --out with an existing parent.', |
| 510 | ); |
| 511 | } |
| 512 | |
| 513 | if (collected.redactionMapPayload !== null) { |
| 514 | const sidecarName = `${basename(outputPath, '.zip')}.docnames.json`; |
| 515 | const stampedManifest: BundleManifest = { |
| 516 | ...collected.manifest, |
| 517 | redaction: { |
| 518 | ...collected.manifest.redaction, |
| 519 | applied: true, |
| 520 | docNameMapSidecar: sidecarName, |
| 521 | }, |
| 522 | }; |
| 523 | writeFileSync( |
| 524 | join(collected.stagingDir, 'manifest.json'), |
| 525 | `${JSON.stringify(stampedManifest, null, 2)}\n`, |
| 526 | ); |
| 527 | } |
| 528 | |
| 529 | const zipfile = new ZipFile(); |
| 530 | const absStagedFiles = walkStagedFiles(collected.stagingDir); |
| 531 | for (const absPath of absStagedFiles) { |
| 532 | const relPath = relativeZipPath(collected.stagingDir, absPath); |
| 533 | zipfile.addFile(absPath, relPath); |
| 534 | } |
| 535 | zipfile.end(); |
| 536 | |
| 537 | if (collected.redactionMapPayload !== null) { |
| 538 | const sidecarName = `${basename(outputPath, '.zip')}.docnames.json`; |
| 539 | const sidecarPath = join(parent, sidecarName); |
| 540 | writeFileSync( |
| 541 | sidecarPath, |
| 542 | `${JSON.stringify( |
| 543 | { |
| 544 | docNameMap: collected.redactionMapPayload.docNameMap, |
| 545 | docNameCollisions: collected.redactionMapPayload.docNameCollisions, |
| 546 | }, |
| 547 | null, |
| 548 | 2, |
| 549 | )}\n`, |
| 550 | { mode: 0o600 }, |
| 551 | ); |
| 552 | } |
| 553 | |
| 554 | const writer = createWriteStream(outputPath); |
| 555 | zipfile.outputStream.pipe(writer); |
| 556 | await new Promise<void>((resolveWait, rejectWait) => { |
| 557 | writer.on('close', resolveWait); |
| 558 | writer.on('error', rejectWait); |
| 559 | zipfile.outputStream.on('error', rejectWait); |
| 560 | }); |
no test coverage detected