From 5249d6183b11f6c9eded643359e05d4f3ecf9f01 Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Tue, 30 Nov 2021 11:13:18 +0000 Subject: [PATCH] fix(index): correct message thrown for missing binaries (#363) * fix(index): correct message thrown for missing binaries * refactor(index): remove nested `else... if` * refactor(index): replace `os.platform()` with `process.platform` * chore(index): shorten error message * test(index): check thrown error on missing binary path param --- src/index.js | 34 ++++++++++++---------------------- src/index.test.js | 28 ++++++++++++++++++++-------- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/src/index.js b/src/index.js index 032f2fac..97114069 100644 --- a/src/index.js +++ b/src/index.js @@ -1,12 +1,10 @@ /* eslint-disable security/detect-child-process */ const camelCase = require("camelcase"); -const os = require("os"); const path = require("upath"); const { execFile } = require("child_process"); const util = require("util"); const execFileAsync = util.promisify(execFile); -const platform = os.platform(); const errorMessages = { 0: "No Error", @@ -89,27 +87,19 @@ class Poppler { constructor(binPath) { if (binPath) { this.popplerPath = path.normalizeTrim(binPath); + } else if (process.platform === "win32") { + this.popplerPath = path.joinSafe( + __dirname, + "lib", + "win32", + "poppler-21.11.0", + "Library", + "bin" + ); } else { - let popplerPath; - - // Build path to Poppler binaries based on OS - switch (platform) { - // Windows OS - case "win32": - popplerPath = path.joinSafe( - __dirname, - "lib", - "win32", - "poppler-21.11.0", - "Library", - "bin" - ); - break; - default: - throw new Error(`${platform} is NOT supported.`); - } - - this.popplerPath = popplerPath; + throw new Error( + `${process.platform} poppler-util binaries are not provided, please pass the installation directory as a parameter to the Poppler instance.` + ); } } diff --git a/src/index.test.js b/src/index.test.js index cfd16415..956e639e 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -3,7 +3,6 @@ /* eslint-disable security/detect-non-literal-fs-filename */ const fs = require("fs"); const glob = require("glob"); -const os = require("os"); const path = require("upath"); const { execFile } = require("child_process"); const util = require("util"); @@ -15,8 +14,7 @@ const testDirectory = `${__dirname}/../test_files/`; const file = `${testDirectory}pdf_1.3_NHS_Constitution.pdf`; let testBinaryPath; -const platform = os.platform(); -switch (platform) { +switch (process.platform) { // macOS case "darwin": testBinaryPath = "/usr/local/bin"; @@ -61,9 +59,9 @@ describe("Node-Poppler Module", () => { await clean(); }); - if (platform === "win32") { - describe("Constructor", () => { - test("Should convert PDF file to SVG file without binary set, and use included binaries", async () => { + describe("Constructor", () => { + if (process.platform === "win32") { + test("Should convert PDF file to SVG file without binary path set on win32, and use included binaries", async () => { const poppler = new Poppler(); const options = { svgFile: true, @@ -79,8 +77,22 @@ describe("Node-Poppler Module", () => { ) ).toBe(true); }); - }); - } + } + + if (process.platform !== "win32") { + test(`Should return an Error object if binary path unset on ${process.platform}`, async () => { + expect.assertions(1); + try { + // eslint-disable-next-line no-unused-vars + const poppler = new Poppler(); + } catch (err) { + expect(err.message).toBe( + `${process.platform} poppler-util binaries are not provided, please pass the installation directory as a parameter to the Poppler instance.` + ); + } + }); + } + }); describe("pdfAttach Function", () => { test("Should attach file to PDF file", async () => {