Skip to content

Commit

Permalink
fix: parsing for non-english systems, part 2 (#26)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jimnys-8 committed Nov 24, 2023
1 parent eceeea9 commit c8965ce
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/get-default-printer/get-default-printer.ts
Expand Up @@ -19,7 +19,7 @@ function getPrinterName(output: string): string {
}

async function getPrinterData(printer: string): Promise<Printer> {
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],
Expand Down
2 changes: 1 addition & 1 deletion src/get-printers/get-printers.ts
Expand Up @@ -4,7 +4,7 @@ import parsePrinterAttribute from "../utils/parse-printer-attribute";

export default async function getPrinters(): Promise<Printer[]> {
try {
const { stdout } = await execAsync("SOFTWARE= LANG=C lpstat -lp");
const { stdout } = await execAsync("lpstat -lp");

const isThereAnyPrinter = stdout.match("printer");
if (!isThereAnyPrinter) return [];
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Expand Up @@ -7,6 +7,6 @@ export interface Printer {
}

export interface ExecResponse {
stdout: string | null;
stderr: string | null;
stdout: string;
stderr: string;
}
26 changes: 22 additions & 4 deletions 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<ExecResponse> {
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});
}
});
});
}

0 comments on commit c8965ce

Please sign in to comment.