From e43b78456e4519977b2ec614fa83540f8d280516 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Pe=C3=B1afiel?= Date: Sat, 6 Aug 2022 01:41:44 -0400 Subject: [PATCH] feat: additional printer attributes (#7) --- .../get-default-printer.spec.ts | 3 +++ src/get-default-printer/get-default-printer.ts | 17 ++++++++++------- src/get-printers/get-printers.spec.ts | 6 ++++++ src/get-printers/get-printers.ts | 7 +++++-- src/types.ts | 3 +++ src/utils/parse-printer-attribute.ts | 10 ++++++++++ src/utils/parse-printer-description.ts | 7 ------- 7 files changed, 37 insertions(+), 16 deletions(-) create mode 100644 src/utils/parse-printer-attribute.ts delete mode 100644 src/utils/parse-printer-description.ts diff --git a/src/get-default-printer/get-default-printer.spec.ts b/src/get-default-printer/get-default-printer.spec.ts index 8cbead2..7740cf1 100644 --- a/src/get-default-printer/get-default-printer.spec.ts +++ b/src/get-default-printer/get-default-printer.spec.ts @@ -35,6 +35,9 @@ it("returns the system default printer", async () => { const expected: Printer = { printer: "Virtual_PDF_Printer", description: "Virtual PDF Printer", + status: "idle", + connection: "direct", + alerts: "none", }; await expect(getDefaultPrinter()).resolves.toEqual(expected); diff --git a/src/get-default-printer/get-default-printer.ts b/src/get-default-printer/get-default-printer.ts index 7ed75e6..1b218f8 100644 --- a/src/get-default-printer/get-default-printer.ts +++ b/src/get-default-printer/get-default-printer.ts @@ -1,16 +1,13 @@ import { Printer } from "../types"; import execAsync from "../utils/exec-async"; -import parsePrinterDescription from "../utils/parse-printer-description"; +import parsePrinterAttribute from "../utils/parse-printer-attribute"; export default async function getDefaultPrinter(): Promise { try { const { stdout } = await execAsync("lpstat -d"); const printer = getPrinterName(stdout); if (!printer) return null; - return { - printer, - description: await getPrinterDescription(printer), - }; + return await getPrinterData(printer); } catch (error) { throw error; } @@ -21,7 +18,13 @@ function getPrinterName(output: string): string { return startIndex === -1 ? "" : output.substr(startIndex + 1).trim(); } -async function getPrinterDescription(printer: string): Promise { +async function getPrinterData(printer: string): Promise { const { stdout } = await execAsync(`lpstat -lp ${printer}`); - return parsePrinterDescription(stdout); + return { + printer, + status: stdout.split(/.*is\s(\w+)\..*/gm)[1], + description: parsePrinterAttribute(stdout, "Description"), + alerts: parsePrinterAttribute(stdout, "Alerts"), + connection: parsePrinterAttribute(stdout, "Connection"), + }; } diff --git a/src/get-printers/get-printers.spec.ts b/src/get-printers/get-printers.spec.ts index bf83cbc..d9b7ba9 100644 --- a/src/get-printers/get-printers.spec.ts +++ b/src/get-printers/get-printers.spec.ts @@ -41,10 +41,16 @@ it("return a list of available printers", async () => { { printer: "Virtual_PDF_Printer", description: "Virtual PDF Printer", + status: "idle", + connection: "direct", + alerts: "none", }, { printer: "Zebra", description: "Zebra Printer", + status: "idle", + connection: "direct", + alerts: "none", }, ]; diff --git a/src/get-printers/get-printers.ts b/src/get-printers/get-printers.ts index 48dd68e..5ad8ef3 100644 --- a/src/get-printers/get-printers.ts +++ b/src/get-printers/get-printers.ts @@ -1,6 +1,6 @@ import { Printer } from "../types"; import execAsync from "../utils/exec-async"; -import parsePrinterDescription from "../utils/parse-printer-description"; +import parsePrinterAttribute from "../utils/parse-printer-attribute"; export default async function getPrinters(): Promise { try { @@ -14,7 +14,10 @@ export default async function getPrinters(): Promise { .filter((line: string) => line.trim().length) .map((line: string) => ({ printer: line.substr(0, line.indexOf(" ")), - description: parsePrinterDescription(line), + status: line.split(/.*is\s(\w+)\..*/gm)[1], + description: parsePrinterAttribute(line, "Description"), + alerts: parsePrinterAttribute(line, "Alerts"), + connection: parsePrinterAttribute(line, "Connection"), })); } catch (error) { throw error; diff --git a/src/types.ts b/src/types.ts index 63698fd..ebc19ec 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,7 @@ export interface Printer { printer: string; description: string | null; + status: string | null; + alerts: string | null; + connection: string | null; } diff --git a/src/utils/parse-printer-attribute.ts b/src/utils/parse-printer-attribute.ts new file mode 100644 index 0000000..13250f8 --- /dev/null +++ b/src/utils/parse-printer-attribute.ts @@ -0,0 +1,10 @@ +export default function parsePrinterAttribute( + stdout: string, + attribute: string +): string { + const attributeLine = stdout + .split("\n") + .slice(1) + .find((line: string) => line.indexOf(attribute) !== -1); + return attributeLine ? attributeLine.split(":")[1].trim() : ""; +} diff --git a/src/utils/parse-printer-description.ts b/src/utils/parse-printer-description.ts deleted file mode 100644 index 3a3a8a5..0000000 --- a/src/utils/parse-printer-description.ts +++ /dev/null @@ -1,7 +0,0 @@ -export default function parsePrinterDescription(stdout: string): string { - const descriptionLine = stdout - .split("\n") - .slice(1) - .find((line: string) => line.indexOf("Description") !== -1); - return descriptionLine ? descriptionLine.split(":")[1].trim() : ""; -}