Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

Commit

Permalink
Fix regression with printer and win32 options (#228)
Browse files Browse the repository at this point in the history
* Update electron-util

Use exports to allow real-time debugging of unbuilt source

* Fix regression with printer and win32 options

They could have not been set together due to a recent change,
this fixes it and adds tests.
  • Loading branch information
mcdado authored Feb 10, 2021
1 parent f7d9e49 commit 3658c37
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/electron-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ const isUsingAsar =
process.mainModule &&
process.mainModule.filename.includes("app.asar");

export const fixPathForAsarUnpack = (path) => {
exports.fixPathForAsarUnpack = (path) => {
return isUsingAsar ? path.replace("app.asar", "app.asar.unpacked") : path;
};
21 changes: 20 additions & 1 deletion src/win32/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ const fs = require("fs");
const execAsync = require("../execAsync");
const { fixPathForAsarUnpack } = require("../electron-util");

const validDestinationArgs = [
"-print-to",
"-print-to-default",
"-print-dialog",
];

const print = (pdf, options = {}) => {
if (!pdf) throw "No PDF specified";
if (typeof pdf !== "string") throw "Invalid PDF name";
Expand All @@ -20,7 +26,20 @@ const print = (pdf, options = {}) => {
if (win32) {
if (!Array.isArray(win32)) throw "options.win32 should be an array";
win32.map((win32Arg) => args.push(...win32Arg.split(" ")));
} else {
}

let validDestination = false;
args.some((a) => {
const fullMatch = validDestinationArgs.indexOf(a) > -1;
if (fullMatch) {
validDestination = true;
return true;
} else {
return false;
}
});

if (!validDestination) {
if (printer) {
args.push("-print-to", printer);
} else {
Expand Down
49 changes: 48 additions & 1 deletion src/win32/print.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,61 @@ test("sends PDF file to the specific printer", () => {
});
});

test("allows users to pass OS specific options", () => {
test("allows users to pass OS specific options and a printer", () => {
const filename = "assets/pdf-sample.pdf";
const printer = "Zebra";
const options = { printer, win32: ['-print-settings "1,2,fit"'] };
return print(filename, options).then(() => {
expect(execAsync).toHaveBeenCalledWith("mocked_path_SumatraPDF.exe", [
"-print-settings",
'"1,2,fit"',
"-print-to",
printer,
"-silent",
filename,
]);
});
});

test("allows users to pass OS specific options without a printer", () => {
const filename = "assets/pdf-sample.pdf";
const options = { win32: ['-print-settings "1,3,fit"'] };
return print(filename, options).then(() => {
expect(execAsync).toHaveBeenCalledWith("mocked_path_SumatraPDF.exe", [
"-print-settings",
'"1,3,fit"',
"-print-to-default",
"-silent",
filename,
]);
});
});

test("does not set a printer when -print-dialog is set", () => {
const filename = "assets/pdf-sample.pdf";
const options = { win32: ["-print-dialog", '-print-settings "1,4,fit"'] };
return print(filename, options).then(() => {
expect(execAsync).toHaveBeenCalledWith("mocked_path_SumatraPDF.exe", [
"-print-dialog",
"-print-settings",
'"1,4,fit"',
filename,
]);
});
});

test("ignores the passed printer when -print-dialog is set", () => {
const filename = "assets/pdf-sample.pdf";
const printer = "Zebra";
const options = {
printer,
win32: ["-print-dialog", '-print-settings "1,4,fit"'],
};
return print(filename, options).then(() => {
expect(execAsync).toHaveBeenCalledWith("mocked_path_SumatraPDF.exe", [
"-print-dialog",
"-print-settings",
'"1,4,fit"',
filename,
]);
});
Expand Down

0 comments on commit 3658c37

Please sign in to comment.