Removed old compiler and unused dependencies
This commit is contained in:
@ -46,16 +46,11 @@
|
|||||||
],
|
],
|
||||||
"types": "./build/index.d.ts",
|
"types": "./build/index.d.ts",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/archiver": "^2.1.0",
|
|
||||||
"@types/bluebird": "3.5.20",
|
|
||||||
"@types/express": "^4.0.35",
|
|
||||||
"@types/image-size": "0.0.29",
|
"@types/image-size": "0.0.29",
|
||||||
"@types/jszip": "^3.1.3",
|
"@types/jszip": "^3.1.3",
|
||||||
"archiver": "^2.1.1",
|
|
||||||
"fast-xml-parser": "^3.3.6",
|
"fast-xml-parser": "^3.3.6",
|
||||||
"image-size": "^0.6.2",
|
"image-size": "^0.6.2",
|
||||||
"jszip": "^3.1.5",
|
"jszip": "^3.1.5",
|
||||||
"request": "^2.83.0",
|
|
||||||
"xml": "^1.0.1"
|
"xml": "^1.0.1"
|
||||||
},
|
},
|
||||||
"author": "Dolan Miu",
|
"author": "Dolan Miu",
|
||||||
|
@ -1,76 +0,0 @@
|
|||||||
/* tslint:disable:typedef space-before-function-paren */
|
|
||||||
import * as fs from "fs";
|
|
||||||
import * as JSZip from "jszip";
|
|
||||||
|
|
||||||
import { expect } from "chai";
|
|
||||||
import { File } from "../../file";
|
|
||||||
import { Compiler } from "./compiler";
|
|
||||||
|
|
||||||
describe("Compiler", () => {
|
|
||||||
let compiler: Compiler;
|
|
||||||
let file: File;
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
file = new File();
|
|
||||||
compiler = new Compiler(file);
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("#compile()", () => {
|
|
||||||
it("should pack all the content", async function() {
|
|
||||||
this.timeout(99999999);
|
|
||||||
const fileName = "build/tests/test.docx";
|
|
||||||
await compiler.compile(fs.createWriteStream(fileName));
|
|
||||||
|
|
||||||
const docxFile = fs.readFileSync(fileName);
|
|
||||||
const zipFile: JSZip = await JSZip.loadAsync(docxFile);
|
|
||||||
const fileNames = Object.keys(zipFile.files).map((f) => zipFile.files[f].name);
|
|
||||||
|
|
||||||
expect(fileNames).is.an.instanceof(Array);
|
|
||||||
expect(fileNames).has.length(13);
|
|
||||||
expect(fileNames).to.include("word/document.xml");
|
|
||||||
expect(fileNames).to.include("word/styles.xml");
|
|
||||||
expect(fileNames).to.include("docProps/core.xml");
|
|
||||||
expect(fileNames).to.include("docProps/app.xml");
|
|
||||||
expect(fileNames).to.include("word/numbering.xml");
|
|
||||||
expect(fileNames).to.include("word/header1.xml");
|
|
||||||
expect(fileNames).to.include("word/_rels/header1.xml.rels");
|
|
||||||
expect(fileNames).to.include("word/footer1.xml");
|
|
||||||
expect(fileNames).to.include("word/footnotes.xml");
|
|
||||||
expect(fileNames).to.include("word/_rels/footer1.xml.rels");
|
|
||||||
expect(fileNames).to.include("word/_rels/document.xml.rels");
|
|
||||||
expect(fileNames).to.include("[Content_Types].xml");
|
|
||||||
expect(fileNames).to.include("_rels/.rels");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should pack all additional headers and footers", async function() {
|
|
||||||
file.createFooter();
|
|
||||||
file.createFooter();
|
|
||||||
file.createHeader();
|
|
||||||
file.createHeader();
|
|
||||||
|
|
||||||
this.timeout(99999999);
|
|
||||||
const fileName = "build/tests/test2.docx";
|
|
||||||
await compiler.compile(fs.createWriteStream(fileName));
|
|
||||||
|
|
||||||
const docxFile = fs.readFileSync(fileName);
|
|
||||||
const zipFile: JSZip = await JSZip.loadAsync(docxFile);
|
|
||||||
const fileNames = Object.keys(zipFile.files).map((f) => zipFile.files[f].name);
|
|
||||||
|
|
||||||
expect(fileNames).is.an.instanceof(Array);
|
|
||||||
expect(fileNames).has.length(21);
|
|
||||||
|
|
||||||
expect(fileNames).to.include("word/header1.xml");
|
|
||||||
expect(fileNames).to.include("word/_rels/header1.xml.rels");
|
|
||||||
expect(fileNames).to.include("word/header2.xml");
|
|
||||||
expect(fileNames).to.include("word/_rels/header2.xml.rels");
|
|
||||||
expect(fileNames).to.include("word/header3.xml");
|
|
||||||
expect(fileNames).to.include("word/_rels/header3.xml.rels");
|
|
||||||
expect(fileNames).to.include("word/footer1.xml");
|
|
||||||
expect(fileNames).to.include("word/_rels/footer1.xml.rels");
|
|
||||||
expect(fileNames).to.include("word/footer2.xml");
|
|
||||||
expect(fileNames).to.include("word/_rels/footer2.xml.rels");
|
|
||||||
expect(fileNames).to.include("word/footer3.xml");
|
|
||||||
expect(fileNames).to.include("word/_rels/footer3.xml.rels");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
@ -1,114 +0,0 @@
|
|||||||
import * as archiver from "archiver";
|
|
||||||
import * as express from "express";
|
|
||||||
import { Writable } from "stream";
|
|
||||||
import * as xml from "xml";
|
|
||||||
|
|
||||||
import { File } from "file";
|
|
||||||
import { Formatter } from "../formatter";
|
|
||||||
|
|
||||||
export class Compiler {
|
|
||||||
protected archive: archiver.Archiver;
|
|
||||||
private readonly formatter: Formatter;
|
|
||||||
|
|
||||||
constructor(private readonly file: File) {
|
|
||||||
this.formatter = new Formatter();
|
|
||||||
this.archive = archiver.create("zip", {});
|
|
||||||
|
|
||||||
this.archive.on("error", (err) => {
|
|
||||||
throw err;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public async compile(output: Writable | express.Response): Promise<void> {
|
|
||||||
this.archive.pipe(output);
|
|
||||||
|
|
||||||
const xmlDocument = xml(this.formatter.format(this.file.Document));
|
|
||||||
const xmlStyles = xml(this.formatter.format(this.file.Styles));
|
|
||||||
const xmlProperties = xml(this.formatter.format(this.file.CoreProperties), {
|
|
||||||
declaration: {
|
|
||||||
standalone: "yes",
|
|
||||||
encoding: "UTF-8",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const xmlNumbering = xml(this.formatter.format(this.file.Numbering));
|
|
||||||
const xmlRelationships = xml(this.formatter.format(this.file.DocumentRelationships));
|
|
||||||
const xmlFileRelationships = xml(this.formatter.format(this.file.FileRelationships));
|
|
||||||
const xmlContentTypes = xml(this.formatter.format(this.file.ContentTypes));
|
|
||||||
const xmlAppProperties = xml(this.formatter.format(this.file.AppProperties));
|
|
||||||
const xmlFootnotes = xml(this.formatter.format(this.file.FootNotes));
|
|
||||||
|
|
||||||
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(xmlAppProperties, {
|
|
||||||
name: "docProps/app.xml",
|
|
||||||
});
|
|
||||||
|
|
||||||
this.archive.append(xmlNumbering, {
|
|
||||||
name: "word/numbering.xml",
|
|
||||||
});
|
|
||||||
|
|
||||||
// headers
|
|
||||||
for (let i = 0; i < this.file.Headers.length; i++) {
|
|
||||||
const element = this.file.Headers[i];
|
|
||||||
this.archive.append(xml(this.formatter.format(element.Header)), {
|
|
||||||
name: `word/header${i + 1}.xml`,
|
|
||||||
});
|
|
||||||
|
|
||||||
this.archive.append(xml(this.formatter.format(element.Relationships)), {
|
|
||||||
name: `word/_rels/header${i + 1}.xml.rels`,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// footers
|
|
||||||
for (let i = 0; i < this.file.Footers.length; i++) {
|
|
||||||
const element = this.file.Footers[i];
|
|
||||||
this.archive.append(xml(this.formatter.format(element.Footer)), {
|
|
||||||
name: `word/footer${i + 1}.xml`,
|
|
||||||
});
|
|
||||||
|
|
||||||
this.archive.append(xml(this.formatter.format(element.Relationships)), {
|
|
||||||
name: `word/_rels/footer${i + 1}.xml.rels`,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
this.archive.append(xmlFootnotes, {
|
|
||||||
name: "word/footnotes.xml",
|
|
||||||
});
|
|
||||||
|
|
||||||
this.archive.append(xmlRelationships, {
|
|
||||||
name: "word/_rels/document.xml.rels",
|
|
||||||
});
|
|
||||||
|
|
||||||
this.archive.append(xmlContentTypes, {
|
|
||||||
name: "[Content_Types].xml",
|
|
||||||
});
|
|
||||||
|
|
||||||
this.archive.append(xmlFileRelationships, {
|
|
||||||
name: "_rels/.rels",
|
|
||||||
});
|
|
||||||
|
|
||||||
for (const data of this.file.Media.Array) {
|
|
||||||
this.archive.append(data.stream, {
|
|
||||||
name: `word/media/${data.fileName}`,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
this.archive.finalize();
|
|
||||||
|
|
||||||
return new Promise<void>((resolve) => {
|
|
||||||
output.on("close", () => {
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user