From ebec10e3120c4fb2e88740af87b6f2fe5dec5a89 Mon Sep 17 00:00:00 2001 From: Dolan Date: Wed, 6 Dec 2017 01:03:14 +0000 Subject: [PATCH] Refactor --- ts/export/packer/compiler.ts | 99 +++++++++++++++++++++++++++++ ts/export/packer/express.ts | 14 +++-- ts/export/packer/local.spec.ts | 4 +- ts/export/packer/local.ts | 15 +++-- ts/export/packer/pack-options.ts | 3 - ts/export/packer/packer.ts | 103 +------------------------------ ts/test-tsconfig.json | 3 +- ts/tsconfig.json | 3 +- 8 files changed, 123 insertions(+), 121 deletions(-) create mode 100644 ts/export/packer/compiler.ts delete mode 100644 ts/export/packer/pack-options.ts diff --git a/ts/export/packer/compiler.ts b/ts/export/packer/compiler.ts new file mode 100644 index 0000000000..b1e3f0a1d7 --- /dev/null +++ b/ts/export/packer/compiler.ts @@ -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 { + 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((resolve) => { + output.on("close", () => { + resolve(); + }); + }); + + } +} diff --git a/ts/export/packer/express.ts b/ts/export/packer/express.ts index 8c9bac1e6d..db5706543c 100644 --- a/ts/export/packer/express.ts +++ b/ts/export/packer/express.ts @@ -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); } } diff --git a/ts/export/packer/local.spec.ts b/ts/export/packer/local.spec.ts index f16e239c6e..3de061f270 100644 --- a/ts/export/packer/local.spec.ts +++ b/ts/export/packer/local.spec.ts @@ -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); diff --git a/ts/export/packer/local.ts b/ts/export/packer/local.ts index 94d9551a70..1f94830037 100644 --- a/ts/export/packer/local.ts +++ b/ts/export/packer/local.ts @@ -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 { @@ -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); diff --git a/ts/export/packer/pack-options.ts b/ts/export/packer/pack-options.ts deleted file mode 100644 index a7eeeb5b3b..0000000000 --- a/ts/export/packer/pack-options.ts +++ /dev/null @@ -1,3 +0,0 @@ -/* tslint:disable-next-line:no-empty-interface */ -export interface IPackOptions { -} diff --git a/ts/export/packer/packer.ts b/ts/export/packer/packer.ts index e90f1cb9cf..5751ebf84d 100644 --- a/ts/export/packer/packer.ts +++ b/ts/export/packer/packer.ts @@ -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 { - 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((resolve) => { - output.on("close", () => { - resolve(); - }); - }); - - } +export interface IPacker { + pack(path: string): void; } diff --git a/ts/test-tsconfig.json b/ts/test-tsconfig.json index a8a86483fb..b8c28cb188 100644 --- a/ts/test-tsconfig.json +++ b/ts/test-tsconfig.json @@ -8,7 +8,8 @@ "outDir": "../build-tests", "sourceRoot": "./", "rootDir": "./", - "module": "commonjs" + "module": "commonjs", + "noUnusedLocals": true }, "include": [ "**/*.spec.ts", diff --git a/ts/tsconfig.json b/ts/tsconfig.json index dcfbf57ac0..fc3e28c1a2 100644 --- a/ts/tsconfig.json +++ b/ts/tsconfig.json @@ -9,7 +9,8 @@ "sourceRoot": "./", "rootDir": "./", "module": "commonjs", - "declaration": true + "declaration": true, + "noUnusedLocals": true }, "exclude": [ "tests",