MCPcopy
hub / github.com/inkeep/open-knowledge / writeBundle

Function writeBundle

packages/cli/src/diagnose/bundle.ts:503–563  ·  view source on GitHub ↗
(opts: WriteBundleOpts)

Source from the content-addressed store, hash-verified

501}
502
503export 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 });

Callers 2

bundle.test.tsFile · 0.90
runDiagnoseBundleFunction · 0.90

Calls 5

walkStagedFilesFunction · 0.85
relativeZipPathFunction · 0.85
endMethod · 0.65
onMethod · 0.65
basenameFunction · 0.50

Tested by

no test coverage detected