diff --git a/src/get-default-printer/get-default-printer.spec.ts b/src/get-default-printer/get-default-printer.spec.ts index 981e290..c7a7000 100644 --- a/src/get-default-printer/get-default-printer.spec.ts +++ b/src/get-default-printer/get-default-printer.spec.ts @@ -2,6 +2,7 @@ import { mocked } from "ts-jest/utils"; import getDefaultPrinter from "./get-default-printer"; import execAsync from "../utils/exec-file-async"; +jest.mock("../utils/throw-if-unsupported-os"); jest.mock("../utils/exec-file-async"); const mockedExecAsync = mocked(execAsync); diff --git a/src/get-default-printer/get-default-printer.ts b/src/get-default-printer/get-default-printer.ts index 64c7b6e..b4e1496 100644 --- a/src/get-default-printer/get-default-printer.ts +++ b/src/get-default-printer/get-default-printer.ts @@ -1,4 +1,5 @@ import execFileAsync from "../utils/exec-file-async"; +import throwIfUnsupportedOperatingSystem from "../utils/throw-if-unsupported-os"; import isValidPrinter from "../utils/windows-printer-valid"; export interface Printer { @@ -8,6 +9,8 @@ export interface Printer { async function getDefaultPrinter(): Promise { try { + throwIfUnsupportedOperatingSystem(); + const { stdout } = await execFileAsync("Powershell.exe", [ "-Command", "Get-CimInstance Win32_Printer -Property DeviceID,Name -Filter Default=true", diff --git a/src/get-printers/get-printers.spec.ts b/src/get-printers/get-printers.spec.ts index 0606391..344846d 100644 --- a/src/get-printers/get-printers.spec.ts +++ b/src/get-printers/get-printers.spec.ts @@ -2,6 +2,7 @@ import { mocked } from "ts-jest/utils"; import execAsync from "../utils/exec-file-async"; import getPrinters from "./get-printers"; +jest.mock("../utils/throw-if-unsupported-os"); jest.mock("../utils/exec-file-async"); const mockedExecAsync = mocked(execAsync); diff --git a/src/get-printers/get-printers.ts b/src/get-printers/get-printers.ts index 93f7d11..0c083f3 100644 --- a/src/get-printers/get-printers.ts +++ b/src/get-printers/get-printers.ts @@ -1,5 +1,6 @@ import execFileAsync from "../utils/exec-file-async"; import isValidPrinter from "../utils/windows-printer-valid"; +import throwIfUnsupportedOperatingSystem from "../utils/throw-if-unsupported-os"; import { Printer } from "../get-default-printer/get-default-printer"; async function getPrinters(): Promise { @@ -22,6 +23,7 @@ async function getPrinters(): Promise { } try { + throwIfUnsupportedOperatingSystem(); const { stdout } = await execFileAsync("Powershell.exe", [ "-Command", "Get-CimInstance Win32_Printer -Property DeviceID,Name", diff --git a/src/index.spec.ts b/src/index.spec.ts index f83c0f5..9335c39 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -12,17 +12,3 @@ test("has `print`, `getDefaultPrinter` and `getPrinters` methods", () => { expect(printer.getDefaultPrinter).toBeDefined(); expect(printer.getPrinters).toBeDefined(); }); - -test.each(["linux", "darwin", "test"])( - "throws on unsupported platform %i", - (platform) => { - jest.resetModules(); - - const os = require("os"); - os.platform.mockImplementation(() => platform); - - expect(() => require("./index")).toThrowError( - new Error("Platform not supported") - ); - } -); diff --git a/src/index.ts b/src/index.ts index 8216ac4..701a3af 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,3 @@ export { default as print } from "./print/print"; export { default as getPrinters } from "./get-printers/get-printers"; export { default as getDefaultPrinter } from "./get-default-printer/get-default-printer"; -import os from "os"; - -if (os.platform() !== "win32") { - throw new Error("Platform not supported"); -} diff --git a/src/print/print.spec.ts b/src/print/print.spec.ts index 0d68ac9..0dbb1ed 100644 --- a/src/print/print.spec.ts +++ b/src/print/print.spec.ts @@ -9,7 +9,7 @@ jest.mock("fs"); jest.mock("path"); jest.mock("../utils/exec-file-async"); jest.mock("../utils/electron-util"); - +jest.mock("../utils/throw-if-unsupported-os"); const mockedFixPathForAsarUnpack = mocked(fixPathForAsarUnpack); const mockedExistsSync = mocked(existsSync); const mockedExecAsync = mocked(execAsync); diff --git a/src/print/print.ts b/src/print/print.ts index 61b4b42..776aa65 100644 --- a/src/print/print.ts +++ b/src/print/print.ts @@ -2,6 +2,7 @@ import path from "path"; import fs from "fs"; import execAsync from "../utils/exec-file-async"; import fixPathForAsarUnpack from "../utils/electron-util"; +import throwIfUnsupportedOperatingSystem from "../utils/throw-if-unsupported-os"; export interface PrintOptions { printer?: string; @@ -38,6 +39,7 @@ export default async function print( pdf: string, options: PrintOptions = {} ): Promise { + throwIfUnsupportedOperatingSystem(); if (!pdf) throw "No PDF specified"; if (!fs.existsSync(pdf)) throw "No such file"; diff --git a/src/utils/throw-if-unsupported-os.spec.ts b/src/utils/throw-if-unsupported-os.spec.ts new file mode 100644 index 0000000..724c84f --- /dev/null +++ b/src/utils/throw-if-unsupported-os.spec.ts @@ -0,0 +1,24 @@ +jest.mock("os"); + +let os; +let throwIfUnsupportedOs; + +beforeEach(() => { + jest.resetModules(); + os = require("os"); + throwIfUnsupportedOs = require("./throw-if-unsupported-os").default; +}); + +it("does not throw on Windows", () => { + os.platform.mockImplementation(() => "win32"); + expect(() => throwIfUnsupportedOs()).not.toThrowError(); +}); + +["linux", "darwin", "test"].forEach((platform) => { + it(`throws on unsupported platform ${platform}`, () => { + os.platform.mockImplementation(() => platform); + expect(() => throwIfUnsupportedOs()).toThrowError( + "Operating System not supported" + ); + }); +}); diff --git a/src/utils/throw-if-unsupported-os.ts b/src/utils/throw-if-unsupported-os.ts new file mode 100644 index 0000000..9b4c04e --- /dev/null +++ b/src/utils/throw-if-unsupported-os.ts @@ -0,0 +1,7 @@ +import os from "os"; + +export default function throwIfUnsupportedOs() { + if (os.platform() !== "win32") { + throw "Operating System not supported"; + } +}