Merge branch 'master' into feat/tslint-to-eslint

# Conflicts:
#	src/file/numbering/level.spec.ts
#	src/file/numbering/level.ts
This commit is contained in:
Dolan Miu
2022-09-15 18:04:48 +01:00
11 changed files with 355 additions and 199 deletions

View File

@ -122,4 +122,52 @@ describe("Packer", () => {
(Packer as any).compiler.compile.restore();
});
});
describe("#toStream()", () => {
it("should create a standard docx file", async () => {
// tslint:disable-next-line: no-any
stub((Packer as any).compiler, "compile").callsFake(() => ({
// tslint:disable-next-line: no-empty
generateNodeStream: () => ({
on: (event: string, cb: () => void) => {
if (event === "end") {
cb();
}
},
}),
}));
const stream = await Packer.toStream(file);
return new Promise((resolve, reject) => {
stream.on("error", () => {
reject();
});
stream.on("end", () => {
resolve();
});
});
});
it("should handle exception if it throws any", async () => {
// tslint:disable-next-line:no-any
const compiler = stub((Packer as any).compiler, "compile").callsFake(() => ({
// tslint:disable-next-line: no-empty
on: (event: string, cb: () => void) => {
if (event === "error") {
cb();
}
},
}));
compiler.throwsException();
return Packer.toStream(file).catch((error) => {
assert.isDefined(error);
});
});
afterEach(() => {
// tslint:disable-next-line:no-any
(Packer as any).compiler.compile.restore();
});
});
});

View File

@ -1,4 +1,5 @@
import { File } from "@file/file";
import { Stream } from "stream";
import { Compiler } from "./next-compiler";
@ -57,5 +58,17 @@ export class Packer {
return zipData;
}
public static async toStream(file: File, prettify?: boolean | PrettifyType): Promise<Stream> {
const zip = this.compiler.compile(file, prettify);
const zipData = zip.generateNodeStream({
type: "nodebuffer",
streamFiles: true,
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
compression: "DEFLATE",
});
return zipData;
}
private static readonly compiler = new Compiler();
}