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

Commit

Permalink
Replace wmic with Powershell cmdlets (#269)
Browse files Browse the repository at this point in the history
* Removed yarn.lock

* Replace wmic with Powershell cmdlets

* Fixed inconsistencies

* Minor fix

* Remove duplicate code by creating isValidPrinter function

* Add yarn.lock to .gitignore
  • Loading branch information
ferretwithaberet authored Aug 7, 2021
1 parent fafa1f6 commit bc894a0
Show file tree
Hide file tree
Showing 6 changed files with 555 additions and 6,645 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
coverage
dist
.DS_Store
.DS_Store
yarn.lock
25 changes: 25 additions & 0 deletions src/utils/windows-printer-valid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = function isValidPrinter(printer) {
const printerData = {
deviceId: null,
name: null,
};

const isValid = printer.split(/\r?\n/).some((line) => {
const [label, value] = line.split(":").map((el) => el.trim());

const lowerLabel = label.toLowerCase();

if (lowerLabel === "deviceid") printerData.deviceId = value;

if (lowerLabel === "name") printerData.name = value;

if (printerData.deviceId && printerData.name) return true;

return false;
});

return {
isValid,
printerData,
};
};
44 changes: 19 additions & 25 deletions src/win32/get-default-printer.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,29 @@
"use strict";

const execAsync = require("../utils/exec-file-async");
const execFileAsync = require("../utils/exec-file-async");
const isValidPrinter = require("../utils/windows-printer-valid");

const getDefaultPrinter = () => {
const stdoutHandler = (stdout) => {
const result = stdout
.trim()
.split(/[\r\n]+/)
.map((line) => line.trim().split(/\s{2,}/));

const headers = result[0].reduce((acc, curr, index) => {
acc[curr] = index;
return acc;
}, {});

const defaultPrinter = result
.slice(1)
.find((printerData) => printerData[headers["Default"]] === "TRUE");

if (!defaultPrinter) return false;

return {
deviceId: defaultPrinter[headers["DeviceID"]],
name: defaultPrinter[headers["Name"]],
};
const printer = stdout.trim();

// If stdout is empty, there is no default printer
if (!stdout) return null;

const { isValid, printerData } = isValidPrinter(printer);

// DeviceID or Name not found
if (!isValid) return null;

return printerData;
};

// https://ss64.com/nt/wmic.html#alias_options
return execAsync(
"wmic",
["printer", "get", "default,deviceid,name"],
return execFileAsync(
"Powershell.exe",
[
"-Command",
"Get-CimInstance Win32_Printer -Property DeviceID,Name -Filter Default=true",
],
stdoutHandler
);
};
Expand Down
39 changes: 22 additions & 17 deletions src/win32/get-printers.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
"use strict";

const execAsync = require("../utils/exec-file-async");
const execFileAsync = require("../utils/exec-file-async");
const isValidPrinter = require("../utils/windows-printer-valid");

const getPrinters = () => {
const stdoutHandler = (stdout) => {
const result = stdout
.trim()
.split(/[\r\n]+/)
.map((line) => line.trim().split(/\s{2,}/));

const headers = result[0].reduce((acc, curr, index) => {
acc[curr] = index;
return acc;
}, {});

return result.slice(1).map((printerData) => ({
deviceId: printerData[headers["DeviceID"]],
name: printerData[headers["Name"]],
}));
const printers = [];

stdout
.split(/(\r?\n){2,}/)
.map((printer) => printer.trim())
.filter((printer) => !!printer)
.forEach((printer) => {
const { isValid, printerData } = isValidPrinter(printer);

if (!isValid) return;

printers.push(printerData);
});

return printers;
};

// https://ss64.com/nt/wmic.html#alias_options
return execAsync("wmic", ["printer", "get", "deviceid,name"], stdoutHandler);
return execFileAsync(
"Powershell.exe",
["-Command", "Get-CimInstance Win32_Printer -Property DeviceID,Name"],
stdoutHandler
);
};

module.exports = getPrinters;
Loading

0 comments on commit bc894a0

Please sign in to comment.