open-agreements
Version:
Open-source legal template filling CLI and library
65 lines • 2.31 kB
JavaScript
/**
* Compile-time state shared by the part emitters.
*
* The context owns the part registry (part name → content type + optional
* document-level relationship), the relationship-id allocator, and the
* ordered file map handed to the zip assembler. All allocation is
* deterministic: ids are sequential counters, never random, and no part of
* the compiler reads the clock.
*/
export class CompileContext {
partsByName = new Map();
fileContents = new Map();
nextRid = 1;
nextHeaderIndex = 1;
nextFooterIndex = 1;
/** Register a part that needs a content-type Override (content set separately). */
registerPart(name, contentType, documentRelType) {
if (this.partsByName.has(name)) {
return this.partsByName.get(name);
}
const part = { name, contentType };
if (documentRelType) {
part.documentRel = { type: documentRelType, rId: this.allocateRid() };
}
this.partsByName.set(name, part);
return part;
}
allocateRid() {
return `rId${this.nextRid++}`;
}
allocateHeaderPartName() {
return `word/header${this.nextHeaderIndex++}.xml`;
}
allocateFooterPartName() {
return `word/footer${this.nextFooterIndex++}.xml`;
}
setFileContent(name, content) {
this.fileContents.set(name, content);
}
registeredParts() {
return Array.from(this.partsByName.values());
}
documentRelParts() {
return this.registeredParts().filter((p) => p.documentRel);
}
/**
* Assemble the final zip-file record. [Content_Types].xml is placed first —
* createZipBuffer preserves insertion order but does not enforce ordering
* itself, so the contract lives here.
*/
toFileRecord() {
const contentTypes = this.fileContents.get('[Content_Types].xml');
if (contentTypes === undefined) {
throw new Error('compile bug: [Content_Types].xml was never emitted');
}
const record = { '[Content_Types].xml': contentTypes };
for (const [name, content] of this.fileContents) {
if (name === '[Content_Types].xml')
continue;
record[name] = content;
}
return record;
}
}
//# sourceMappingURL=context.js.map