This repository has been archived by the owner on Mar 22, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace wmic with Powershell cmdlets (#269)
* 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
1 parent
fafa1f6
commit bc894a0
Showing
6 changed files
with
555 additions
and
6,645 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
node_modules | ||
coverage | ||
dist | ||
.DS_Store | ||
.DS_Store | ||
yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.