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