From c8965ce4c524ce5d74872c726e87a0c0577ab878 Mon Sep 17 00:00:00 2001 From: jimnys-8 Date: Fri, 24 Nov 2023 08:41:35 +0100 Subject: [PATCH] fix: parsing for non-english systems, part 2 (#26) * Removed null type from ExecResponse (as far as I can tell, exec always returns strings) * Always use the language env vars in execAsync (since the output is always assumed to be in English) * Removed redundant env vars from cmd input to execAsync * Added a bit of documentation to the env vars --- .../get-default-printer.ts | 2 +- src/get-printers/get-printers.ts | 2 +- src/types.ts | 4 +-- src/utils/exec-async.ts | 26 ++++++++++++++++--- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/get-default-printer/get-default-printer.ts b/src/get-default-printer/get-default-printer.ts index d3cdb2e..1b218f8 100644 --- a/src/get-default-printer/get-default-printer.ts +++ b/src/get-default-printer/get-default-printer.ts @@ -19,7 +19,7 @@ function getPrinterName(output: string): string { } async function getPrinterData(printer: string): Promise { - const { stdout } = await execAsync(`SOFTWARE= LANG=C lpstat -lp ${printer}`); + const { stdout } = await execAsync(`lpstat -lp ${printer}`); return { printer, status: stdout.split(/.*is\s(\w+)\..*/gm)[1], diff --git a/src/get-printers/get-printers.ts b/src/get-printers/get-printers.ts index 28096e7..5ad8ef3 100644 --- a/src/get-printers/get-printers.ts +++ b/src/get-printers/get-printers.ts @@ -4,7 +4,7 @@ import parsePrinterAttribute from "../utils/parse-printer-attribute"; export default async function getPrinters(): Promise { try { - const { stdout } = await execAsync("SOFTWARE= LANG=C lpstat -lp"); + const { stdout } = await execAsync("lpstat -lp"); const isThereAnyPrinter = stdout.match("printer"); if (!isThereAnyPrinter) return []; diff --git a/src/types.ts b/src/types.ts index c4fd4af..0629426 100644 --- a/src/types.ts +++ b/src/types.ts @@ -7,6 +7,6 @@ export interface Printer { } export interface ExecResponse { - stdout: string | null; - stderr: string | null; + stdout: string; + stderr: string; } diff --git a/src/utils/exec-async.ts b/src/utils/exec-async.ts index b0c855c..e5be78d 100644 --- a/src/utils/exec-async.ts +++ b/src/utils/exec-async.ts @@ -1,7 +1,25 @@ "use strict"; -const { exec } = require("child_process"); -const util = require("util"); -const execAsync = util.promisify(exec); +import { ExecResponse } from "../types"; +import { exec } from "child_process"; -export default execAsync; +export default function execAsync(cmd: string): Promise { + return new Promise((resolve, reject) => { + exec(cmd, { + // The output from lp and lpstat is parsed assuming the language is English. + // LANG=C sets the language and the SOFTWARE variable is necessary + // on MacOS due to a detail in Apple's CUPS implementation + // (see https://unix.stackexchange.com/a/33836) + env: { + SOFTWARE: "", + LANG: "C" + } + }, (err, stdout, stderr) => { + if (err) { + reject(err); + } else { + resolve({stdout, stderr}); + } + }); + }); +}