refactored local packer
This commit is contained in:
@ -7,17 +7,13 @@ import {Numbering} from "../../numbering";
|
||||
export class LocalPacker extends Packer {
|
||||
private stream: fs.WriteStream;
|
||||
|
||||
constructor(document: Document, style: any, properties: Properties, path: string, numbering?: Numbering) {
|
||||
|
||||
if (!numbering) {
|
||||
numbering = new Numbering();
|
||||
constructor(document: Document, styles?: any, properties?: Properties, numbering?: Numbering) {
|
||||
super(document, styles, properties, numbering);
|
||||
}
|
||||
|
||||
super(document, style, properties, numbering);
|
||||
pack(path: string): void {
|
||||
this.stream = fs.createWriteStream(path);
|
||||
}
|
||||
|
||||
pack(): void {
|
||||
super.pack(this.stream);
|
||||
this.stream.close();
|
||||
}
|
||||
}
|
@ -6,6 +6,7 @@ import {Document} from "../../docx";
|
||||
import {Styles} from "../../styles";
|
||||
import {Properties} from "../../properties";
|
||||
import {Numbering} from "../../numbering";
|
||||
import {DefaultStylesFactory} from "../../styles/factory";
|
||||
|
||||
let appRoot = require("app-root-path");
|
||||
|
||||
@ -17,7 +18,7 @@ export abstract class Packer {
|
||||
private properties: Properties;
|
||||
private numbering: Numbering;
|
||||
|
||||
constructor(document: Document, style: any, properties: Properties, numbering: Numbering) {
|
||||
constructor(document: Document, style?: any, properties?: Properties, numbering?: Numbering) {
|
||||
this.formatter = new Formatter();
|
||||
this.document = document;
|
||||
this.style = style;
|
||||
@ -25,6 +26,23 @@ export abstract class Packer {
|
||||
this.numbering = numbering;
|
||||
this.archive = archiver.create("zip", {});
|
||||
|
||||
if (!style) {
|
||||
let stylesFactory = new DefaultStylesFactory();
|
||||
style = stylesFactory.newInstance();
|
||||
}
|
||||
|
||||
if (!properties) {
|
||||
properties = new Properties({
|
||||
creator: "Shan Fu",
|
||||
revision: "1",
|
||||
lastModifiedBy: "Shan Fu"
|
||||
});
|
||||
}
|
||||
|
||||
if (!numbering) {
|
||||
numbering = new Numbering();
|
||||
}
|
||||
|
||||
this.archive.on("error", (err) => {
|
||||
throw err;
|
||||
});
|
||||
|
@ -29,14 +29,14 @@ describe("Packer", () => {
|
||||
lastModifiedBy: "Shan Fu"
|
||||
});
|
||||
stylesFactory = new DefaultStylesFactory();
|
||||
packer = new LocalPacker(document, stylesFactory.newInstance(), properties, "build/tests/test.docx");
|
||||
packer = new LocalPacker(document, stylesFactory.newInstance(), properties);
|
||||
// packer = new LocalPacker(document, DefaultStyle(), properties, "build/tests/test.docx");
|
||||
});
|
||||
|
||||
describe("#pack()", () => {
|
||||
it("should create a standard docx file", function (done) {
|
||||
this.timeout(99999999);
|
||||
packer.pack();
|
||||
packer.pack("build/tests/test.docx");
|
||||
setTimeout(done, 1900);
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user