Refactor
This commit is contained in:
99
ts/export/packer/compiler.ts
Normal file
99
ts/export/packer/compiler.ts
Normal file
@ -0,0 +1,99 @@
|
||||
import * as archiver from "archiver";
|
||||
import * as express from "express";
|
||||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
import * as xml from "xml";
|
||||
|
||||
import { Document } from "../../docx";
|
||||
import { Media } from "../../media";
|
||||
import { Numbering } from "../../numbering";
|
||||
import { Properties } from "../../properties";
|
||||
import { Styles } from "../../styles";
|
||||
import { DefaultStylesFactory } from "../../styles/factory";
|
||||
import { Formatter } from "../formatter";
|
||||
|
||||
const TEMPLATE_PATH = path.resolve(__dirname, "../../../template");
|
||||
|
||||
export class Compiler {
|
||||
protected archive: archiver.Archiver;
|
||||
private formatter: Formatter;
|
||||
private style: Styles;
|
||||
|
||||
constructor(
|
||||
protected document: Document,
|
||||
style?: Styles,
|
||||
private properties: Properties = new Properties({
|
||||
creator: "Un-named",
|
||||
revision: "1",
|
||||
lastModifiedBy: "Un-named",
|
||||
}),
|
||||
private numbering: Numbering = new Numbering(),
|
||||
private media: Media = new Media(),
|
||||
) {
|
||||
this.formatter = new Formatter();
|
||||
this.archive = archiver.create("zip", {});
|
||||
|
||||
if (style) {
|
||||
this.style = style;
|
||||
} else {
|
||||
const stylesFactory = new DefaultStylesFactory();
|
||||
this.style = stylesFactory.newInstance();
|
||||
}
|
||||
|
||||
this.archive.on("error", (err) => {
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
|
||||
public async compile(output: fs.WriteStream | express.Response): Promise<void> {
|
||||
this.archive.pipe(output);
|
||||
this.archive.glob("**", {
|
||||
cwd: TEMPLATE_PATH,
|
||||
});
|
||||
|
||||
this.archive.glob("**/.rels", {
|
||||
cwd: TEMPLATE_PATH,
|
||||
});
|
||||
|
||||
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",
|
||||
});
|
||||
|
||||
for (const data of this.media.array) {
|
||||
this.archive.append(data.stream, {
|
||||
name: `media/${data.fileName}`,
|
||||
});
|
||||
}
|
||||
|
||||
this.archive.finalize();
|
||||
|
||||
return new Promise<void>((resolve) => {
|
||||
output.on("close", () => {
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
}
|
@ -5,14 +5,16 @@ import { Media } from "../../media";
|
||||
import { Numbering } from "../../numbering";
|
||||
import { Properties } from "../../properties";
|
||||
import { Styles } from "../../styles";
|
||||
import { IPackOptions } from "./pack-options";
|
||||
import { Packer } from "./packer";
|
||||
import { Compiler } from "./compiler";
|
||||
import { IPacker } from "./packer";
|
||||
|
||||
export class ExpressPacker extends Packer {
|
||||
export class ExpressPacker implements IPacker {
|
||||
private res: express.Response;
|
||||
private packer: Compiler;
|
||||
|
||||
constructor(document: Document, res: express.Response, styles?: Styles, properties?: Properties, numbering?: Numbering, media?: Media) {
|
||||
super(document, styles, properties, numbering, media);
|
||||
this.packer = new Compiler(document, styles, properties, numbering, media);
|
||||
|
||||
this.res = res;
|
||||
|
||||
this.res.on("close", () => {
|
||||
@ -20,10 +22,10 @@ export class ExpressPacker extends Packer {
|
||||
});
|
||||
}
|
||||
|
||||
public pack(name: string, options: IPackOptions): void {
|
||||
public pack(name: string): void {
|
||||
name = name.replace(/.docx$/, "");
|
||||
|
||||
this.res.attachment(`${name}.docx`);
|
||||
super.compile(this.res);
|
||||
this.packer.compile(this.res);
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import { LocalPacker } from "../../export/packer/local";
|
||||
import { Properties } from "../../properties";
|
||||
import { DefaultStylesFactory } from "../../styles/factory";
|
||||
|
||||
describe("Packer", () => {
|
||||
describe("LocalPacker", () => {
|
||||
let packer: LocalPacker;
|
||||
let stylesFactory: DefaultStylesFactory;
|
||||
|
||||
@ -52,7 +52,9 @@ describe("Packer", () => {
|
||||
}
|
||||
}, 2000);
|
||||
});
|
||||
});
|
||||
|
||||
describe("#packPdf", () => {
|
||||
it("should create a standard PDF file", async function () {
|
||||
this.timeout(99999999);
|
||||
|
||||
|
@ -1,32 +1,31 @@
|
||||
import * as fs from "fs";
|
||||
import * as os from "os";
|
||||
import * as path from "path";
|
||||
import * as util from "util";
|
||||
|
||||
import { Document } from "../../docx/document";
|
||||
import { Media } from "../../media";
|
||||
import { Numbering } from "../../numbering";
|
||||
import { Properties } from "../../properties";
|
||||
import { Styles } from "../../styles";
|
||||
import { IPackOptions } from "./pack-options";
|
||||
import { Packer } from "./packer";
|
||||
import { Compiler } from "./compiler";
|
||||
import { IPacker } from "./packer";
|
||||
import { PdfConvertWrapper } from "./pdf-convert-wrapper";
|
||||
|
||||
export class LocalPacker extends Packer {
|
||||
export class LocalPacker implements IPacker {
|
||||
private stream: fs.WriteStream;
|
||||
private pdfConverter: PdfConvertWrapper;
|
||||
private packer: Compiler;
|
||||
|
||||
constructor(document: Document, styles?: Styles, properties?: Properties, numbering?: Numbering, media?: Media) {
|
||||
super(document, styles, properties, numbering, media);
|
||||
|
||||
this.pdfConverter = new PdfConvertWrapper();
|
||||
this.packer = new Compiler(document, styles, properties, numbering, media);
|
||||
}
|
||||
|
||||
public pack(filePath: string): void {
|
||||
filePath = filePath.replace(/.docx$/, "");
|
||||
|
||||
this.stream = fs.createWriteStream(`${filePath}.docx`);
|
||||
super.compile(this.stream);
|
||||
this.packer.compile(this.stream);
|
||||
}
|
||||
|
||||
public async packPdf(filePath: string): Promise<void> {
|
||||
@ -35,7 +34,7 @@ export class LocalPacker extends Packer {
|
||||
const fileName = path.basename(filePath, path.extname(filePath));
|
||||
const tempPath = path.join(os.tmpdir(), `${fileName}.docx`);
|
||||
this.stream = fs.createWriteStream(tempPath);
|
||||
await super.compile(this.stream);
|
||||
await this.packer.compile(this.stream);
|
||||
const text = await this.pdfConverter.convert(tempPath);
|
||||
// const writeFile = util.promisify(fs.writeFile); --use this in future, in 3 years time. Only in node 8
|
||||
// return writeFile(`${filePath}.pdf`, text);
|
||||
|
@ -1,3 +0,0 @@
|
||||
/* tslint:disable-next-line:no-empty-interface */
|
||||
export interface IPackOptions {
|
||||
}
|
@ -1,102 +1,3 @@
|
||||
import * as archiver from "archiver";
|
||||
import * as express from "express";
|
||||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
import * as xml from "xml";
|
||||
|
||||
import { Document } from "../../docx";
|
||||
import { Media } from "../../media";
|
||||
import { Numbering } from "../../numbering";
|
||||
import { Properties } from "../../properties";
|
||||
import { Styles } from "../../styles";
|
||||
import { DefaultStylesFactory } from "../../styles/factory";
|
||||
import { Formatter } from "../formatter";
|
||||
import { IPackOptions } from "./pack-options";
|
||||
|
||||
const TEMPLATE_PATH = path.resolve(__dirname, "../../../template");
|
||||
|
||||
export abstract class Packer {
|
||||
protected archive: archiver.Archiver;
|
||||
private formatter: Formatter;
|
||||
private style: Styles;
|
||||
|
||||
constructor(
|
||||
protected document: Document,
|
||||
style?: Styles,
|
||||
private properties: Properties = new Properties({
|
||||
creator: "Un-named",
|
||||
revision: "1",
|
||||
lastModifiedBy: "Un-named",
|
||||
}),
|
||||
private numbering: Numbering = new Numbering(),
|
||||
private media: Media = new Media(),
|
||||
) {
|
||||
this.formatter = new Formatter();
|
||||
this.archive = archiver.create("zip", {});
|
||||
|
||||
if (style) {
|
||||
this.style = style;
|
||||
} else {
|
||||
const stylesFactory = new DefaultStylesFactory();
|
||||
this.style = stylesFactory.newInstance();
|
||||
}
|
||||
|
||||
this.archive.on("error", (err) => {
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
|
||||
public abstract pack(path: string, options?: IPackOptions): void;
|
||||
|
||||
protected async compile(output: fs.WriteStream | express.Response): Promise<void> {
|
||||
this.archive.pipe(output);
|
||||
this.archive.glob("**", {
|
||||
cwd: TEMPLATE_PATH,
|
||||
});
|
||||
|
||||
this.archive.glob("**/.rels", {
|
||||
cwd: TEMPLATE_PATH,
|
||||
});
|
||||
|
||||
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",
|
||||
});
|
||||
|
||||
for (const data of this.media.array) {
|
||||
this.archive.append(data.stream, {
|
||||
name: `media/${data.fileName}`,
|
||||
});
|
||||
}
|
||||
|
||||
this.archive.finalize();
|
||||
|
||||
return new Promise<void>((resolve) => {
|
||||
output.on("close", () => {
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
export interface IPacker {
|
||||
pack(path: string): void;
|
||||
}
|
||||
|
@ -8,7 +8,8 @@
|
||||
"outDir": "../build-tests",
|
||||
"sourceRoot": "./",
|
||||
"rootDir": "./",
|
||||
"module": "commonjs"
|
||||
"module": "commonjs",
|
||||
"noUnusedLocals": true
|
||||
},
|
||||
"include": [
|
||||
"**/*.spec.ts",
|
||||
|
@ -9,7 +9,8 @@
|
||||
"sourceRoot": "./",
|
||||
"rootDir": "./",
|
||||
"module": "commonjs",
|
||||
"declaration": true
|
||||
"declaration": true,
|
||||
"noUnusedLocals": true
|
||||
},
|
||||
"exclude": [
|
||||
"tests",
|
||||
|
Reference in New Issue
Block a user