2016-03-31 18:07:22 +01:00
|
|
|
import * as archiver from "archiver";
|
2016-03-31 19:28:12 +01:00
|
|
|
import * as fs from "fs";
|
2016-04-05 01:49:12 +01:00
|
|
|
import * as xml from "xml";
|
2016-03-31 19:28:12 +01:00
|
|
|
import {Formatter} from "../formatter";
|
2016-03-31 23:36:42 +01:00
|
|
|
import {Document} from "../../docx";
|
2016-04-09 04:27:49 +01:00
|
|
|
import {Styles} from "../../styles";
|
2016-04-01 04:09:24 +01:00
|
|
|
import {Properties} from "../../properties";
|
2016-05-21 03:08:09 +01:00
|
|
|
import {Numbering} from "../../numbering";
|
2016-07-04 18:47:13 +01:00
|
|
|
import {DefaultStylesFactory} from "../../styles/factory";
|
2016-05-21 03:08:09 +01:00
|
|
|
|
2016-05-26 15:08:34 +01:00
|
|
|
let appRoot = require("app-root-path");
|
2016-03-31 15:49:42 +01:00
|
|
|
|
2016-03-31 19:28:12 +01:00
|
|
|
export abstract class Packer {
|
2016-03-31 18:07:22 +01:00
|
|
|
protected archive: any;
|
2016-03-31 19:28:12 +01:00
|
|
|
private formatter: Formatter;
|
2016-03-31 23:36:42 +01:00
|
|
|
protected document: Document;
|
2016-04-09 04:27:49 +01:00
|
|
|
private style: Styles;
|
2016-04-03 05:28:37 +01:00
|
|
|
private properties: Properties;
|
2016-05-21 03:08:09 +01:00
|
|
|
private numbering: Numbering;
|
2016-05-26 15:08:34 +01:00
|
|
|
|
2016-07-04 18:47:13 +01:00
|
|
|
constructor(document: Document, style?: any, properties?: Properties, numbering?: Numbering) {
|
2016-03-31 19:28:12 +01:00
|
|
|
this.formatter = new Formatter();
|
2016-03-31 23:36:42 +01:00
|
|
|
this.document = document;
|
2016-04-03 05:28:37 +01:00
|
|
|
this.style = style;
|
|
|
|
this.properties = properties;
|
2016-05-21 03:08:09 +01:00
|
|
|
this.numbering = numbering;
|
2016-03-31 18:07:22 +01:00
|
|
|
this.archive = archiver.create("zip", {});
|
2016-03-31 19:04:54 +01:00
|
|
|
|
2016-07-04 18:47:13 +01:00
|
|
|
if (!style) {
|
|
|
|
let stylesFactory = new DefaultStylesFactory();
|
2016-07-04 19:01:30 +01:00
|
|
|
this.style = stylesFactory.newInstance();
|
2016-07-04 18:47:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!properties) {
|
2016-07-04 19:01:30 +01:00
|
|
|
this.properties = new Properties({
|
2016-07-19 18:43:11 +01:00
|
|
|
creator: "Un-named",
|
2016-07-04 18:47:13 +01:00
|
|
|
revision: "1",
|
2016-07-19 18:43:11 +01:00
|
|
|
lastModifiedBy: "Un-named"
|
2016-07-04 18:47:13 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!numbering) {
|
2016-07-04 19:01:30 +01:00
|
|
|
this.numbering = new Numbering();
|
2016-07-04 18:47:13 +01:00
|
|
|
}
|
|
|
|
|
2016-05-26 15:08:34 +01:00
|
|
|
this.archive.on("error", (err) => {
|
2016-03-31 19:04:54 +01:00
|
|
|
throw err;
|
|
|
|
});
|
2016-03-31 18:03:16 +01:00
|
|
|
}
|
|
|
|
|
2016-03-31 19:28:12 +01:00
|
|
|
pack(output: any): void {
|
2016-03-31 18:03:16 +01:00
|
|
|
this.archive.pipe(output);
|
2016-04-05 22:11:21 +01:00
|
|
|
console.log(appRoot.path + "/template");
|
2017-03-07 13:49:21 +01:00
|
|
|
this.archive.glob("**", {
|
|
|
|
expand: true,
|
|
|
|
cwd: appRoot.path + "/template",
|
|
|
|
});
|
|
|
|
|
|
|
|
this.archive.glob("**/.rels", {
|
|
|
|
expand: true,
|
|
|
|
cwd: appRoot.path + "/template",
|
|
|
|
});
|
2016-03-31 19:04:54 +01:00
|
|
|
|
2016-05-26 15:08:34 +01:00
|
|
|
// this.archive.file(appRoot.path + "/template/[Content_Types].xml", { name: "[Content_Types].xml" });
|
|
|
|
// console.log(__dirname + "/packer.js");
|
|
|
|
// this.archive.file(__dirname + "/packer.js", { name: "/[Content_Types].xml" });
|
2016-04-05 22:11:21 +01:00
|
|
|
|
|
|
|
/*this.archive.directory(appRoot.path + "/template", {
|
|
|
|
name: "/root/g.txt",
|
|
|
|
prefix: "root"
|
|
|
|
});*/
|
2016-05-26 15:08:34 +01:00
|
|
|
let xmlDocument = xml(this.formatter.format(this.document));
|
|
|
|
let xmlStyles = xml(this.formatter.format(this.style));
|
|
|
|
let xmlProperties = xml(this.formatter.format(this.properties), { declaration: { standalone: "yes", encoding: "UTF-8" } });
|
|
|
|
let xmlNumbering = xml(this.formatter.format(this.numbering));
|
|
|
|
// console.log(JSON.stringify(this.numbering, null, " "));
|
|
|
|
console.log(xmlNumbering);
|
2016-04-05 01:49:12 +01:00
|
|
|
this.archive.append(xmlDocument, {
|
2016-05-26 15:08:34 +01:00
|
|
|
name: "word/document.xml"
|
2016-04-03 05:28:37 +01:00
|
|
|
});
|
|
|
|
|
2016-05-21 03:08:09 +01:00
|
|
|
this.archive.append(xmlStyles, {
|
2016-05-26 15:08:34 +01:00
|
|
|
name: "word/styles.xml"
|
2016-05-09 03:44:16 +01:00
|
|
|
});
|
2016-04-03 05:28:37 +01:00
|
|
|
|
2016-04-05 01:49:12 +01:00
|
|
|
this.archive.append(xmlProperties, {
|
2016-05-26 15:08:34 +01:00
|
|
|
name: "docProps/core.xml"
|
2016-04-03 05:28:37 +01:00
|
|
|
});
|
2016-05-26 15:08:34 +01:00
|
|
|
|
2016-05-21 03:08:09 +01:00
|
|
|
this.archive.append(xmlNumbering, {
|
2016-05-26 15:08:34 +01:00
|
|
|
name: "word/numbering.xml"
|
|
|
|
});
|
2016-04-03 05:28:37 +01:00
|
|
|
|
2016-03-31 19:04:54 +01:00
|
|
|
this.archive.finalize();
|
2016-03-31 18:03:16 +01:00
|
|
|
}
|
2016-03-31 15:45:48 +01:00
|
|
|
}
|