From 993a832f3e434c86763ee4ce609942ab138c8d46 Mon Sep 17 00:00:00 2001 From: Dolan Miu Date: Mon, 4 Jul 2016 18:47:13 +0100 Subject: [PATCH] refactored local packer --- ts/export/packer/local.ts | 14 +++++--------- ts/export/packer/packer.ts | 20 +++++++++++++++++++- ts/tests/export/localPackerTest.ts | 4 ++-- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/ts/export/packer/local.ts b/ts/export/packer/local.ts index 513d474310..afdf1c5255 100644 --- a/ts/export/packer/local.ts +++ b/ts/export/packer/local.ts @@ -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(); - } - - super(document, style, properties, numbering); - this.stream = fs.createWriteStream(path); + constructor(document: Document, styles?: any, properties?: Properties, numbering?: Numbering) { + super(document, styles, properties, numbering); } - pack(): void { + pack(path: string): void { + this.stream = fs.createWriteStream(path); super.pack(this.stream); + this.stream.close(); } } \ No newline at end of file diff --git a/ts/export/packer/packer.ts b/ts/export/packer/packer.ts index 6b06e7cda2..7bf7f57526 100644 --- a/ts/export/packer/packer.ts +++ b/ts/export/packer/packer.ts @@ -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; }); diff --git a/ts/tests/export/localPackerTest.ts b/ts/tests/export/localPackerTest.ts index b68410242c..527a5f0af8 100644 --- a/ts/tests/export/localPackerTest.ts +++ b/ts/tests/export/localPackerTest.ts @@ -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); }); });