Files
docx-js/ts/export/packer/packer.ts
felipe c7b7b585be fix path to template when installed via npm
I'm not sure why, buy app-root-path was not picking up the correct
path for this module when installed via NPM. Since we're already using
relative paths for imports, I don't think we gain much from using it
anyway, so I've changed to using path.resolve instead
2017-03-13 20:40:16 +01:00

89 lines
2.6 KiB
TypeScript

import * as archiver from "archiver";
import * as path from "path";
import * as xml from "xml";
import { Document } from "../../docx";
import { Numbering } from "../../numbering";
import { Properties } from "../../properties";
import { Styles } from "../../styles";
import { DefaultStylesFactory } from "../../styles/factory";
import { Formatter } from "../formatter";
const templatePath = path.resolve(__dirname, "../../../template");
export abstract class Packer {
protected archive: any;
protected document: Document;
private formatter: Formatter;
private style: Styles;
private properties: Properties;
private numbering: Numbering;
constructor(document: Document, style?: Styles, properties?: Properties, numbering?: Numbering) {
this.formatter = new Formatter();
this.document = document;
this.archive = archiver.create("zip", {});
if (style) {
this.style = style;
} else {
const stylesFactory = new DefaultStylesFactory();
this.style = stylesFactory.newInstance();
}
if (properties) {
this.properties = properties;
} else {
this.properties = new Properties({
creator: "Un-named",
revision: "1",
lastModifiedBy: "Un-named",
});
}
if (numbering) {
this.numbering = numbering;
} else {
this.numbering = new Numbering();
}
this.archive.on("error", (err) => {
throw err;
});
}
public pack(output: any): void {
this.archive.pipe(output);
this.archive.glob("**", {
expand: true,
cwd: templatePath,
});
this.archive.glob("**/.rels", {
expand: true,
cwd: templatePath,
});
const xmlDocument = xml(this.formatter.format(this.document));
const xmlStyles = xml(this.formatter.format(this.style));
const xmlProperties = xml(this.formatter.format(this.properties), { declaration: { standalone: "yes", encoding: "UTF-8" } });
const xmlNumbering = xml(this.formatter.format(this.numbering));
this.archive.append(xmlDocument, {
name: "word/document.xml",
});
this.archive.append(xmlStyles, {
name: "word/styles.xml",
});
this.archive.append(xmlProperties, {
name: "docProps/core.xml",
});
this.archive.append(xmlNumbering, {
name: "word/numbering.xml",
});
this.archive.finalize();
}
}