Skip to content

Commit

Permalink
feat: additional printer attributes (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio-P committed Aug 6, 2022
1 parent f9c8318 commit e43b784
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 16 deletions.
3 changes: 3 additions & 0 deletions src/get-default-printer/get-default-printer.spec.ts
Expand Up @@ -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);
Expand Down
17 changes: 10 additions & 7 deletions 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<Printer | null> {
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;
}
Expand All @@ -21,7 +18,13 @@ function getPrinterName(output: string): string {
return startIndex === -1 ? "" : output.substr(startIndex + 1).trim();
}

async function getPrinterDescription(printer: string): Promise<string> {
async function getPrinterData(printer: string): Promise<Printer> {
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"),
};
}
6 changes: 6 additions & 0 deletions src/get-printers/get-printers.spec.ts
Expand Up @@ -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",
},
];

Expand Down
7 changes: 5 additions & 2 deletions 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<Printer[]> {
try {
Expand All @@ -14,7 +14,10 @@ export default async function getPrinters(): Promise<Printer[]> {
.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;
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
@@ -1,4 +1,7 @@
export interface Printer {
printer: string;
description: string | null;
status: string | null;
alerts: string | null;
connection: string | null;
}
10 changes: 10 additions & 0 deletions 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() : "";
}
7 changes: 0 additions & 7 deletions src/utils/parse-printer-description.ts

This file was deleted.

0 comments on commit e43b784

Please sign in to comment.