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

Commit

Permalink
feat: throw unsupported OS error on a method call (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
artiebits authored Nov 22, 2021
1 parent 41cd217 commit c93fbbd
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/get-default-printer/get-default-printer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 3 additions & 0 deletions src/get-default-printer/get-default-printer.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -8,6 +9,8 @@ export interface Printer {

async function getDefaultPrinter(): Promise<Printer | null> {
try {
throwIfUnsupportedOperatingSystem();

const { stdout } = await execFileAsync("Powershell.exe", [
"-Command",
"Get-CimInstance Win32_Printer -Property DeviceID,Name -Filter Default=true",
Expand Down
1 change: 1 addition & 0 deletions src/get-printers/get-printers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions src/get-printers/get-printers.ts
Original file line number Diff line number Diff line change
@@ -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<Printer[]> {
Expand All @@ -22,6 +23,7 @@ async function getPrinters(): Promise<Printer[]> {
}

try {
throwIfUnsupportedOperatingSystem();
const { stdout } = await execFileAsync("Powershell.exe", [
"-Command",
"Get-CimInstance Win32_Printer -Property DeviceID,Name",
Expand Down
14 changes: 0 additions & 14 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
);
}
);
5 changes: 0 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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");
}
2 changes: 1 addition & 1 deletion src/print/print.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/print/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -38,6 +39,7 @@ export default async function print(
pdf: string,
options: PrintOptions = {}
): Promise<void> {
throwIfUnsupportedOperatingSystem();
if (!pdf) throw "No PDF specified";
if (!fs.existsSync(pdf)) throw "No such file";

Expand Down
24 changes: 24 additions & 0 deletions src/utils/throw-if-unsupported-os.spec.ts
Original file line number Diff line number Diff line change
@@ -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"
);
});
});
7 changes: 7 additions & 0 deletions src/utils/throw-if-unsupported-os.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import os from "os";

export default function throwIfUnsupportedOs() {
if (os.platform() !== "win32") {
throw "Operating System not supported";
}
}

0 comments on commit c93fbbd

Please sign in to comment.