UNPKG

usb-mountpoint

Version:

Library for listing USB mass storage devices, their serial numbers and mount points

112 lines (108 loc) 4.86 kB
"use strict"; /* MIT License Copyright (c) 2020 Rob Moran Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const util_1 = require("util"); const child_process_1 = require("child_process"); const asyncExec = util_1.promisify(child_process_1.exec); const EOL = '\r\r\n'; class WindowsImplementation { listDevices() { return __awaiter(this, void 0, void 0, function* () { const results = []; const drives = yield this.getDrives(); for (const drive of drives) { const partitions = yield this.getPartitions(drive.DeviceID); if (!partitions.length) { continue; } const disks = yield this.getDisks(partitions[0].DeviceID); if (!disks.length) { continue; } results.push({ serialNumber: drive.SerialNumber, mountPoint: disks[0].DeviceID }); } return results; }); } getDrives() { return __awaiter(this, void 0, void 0, function* () { const command = `Win32_DiskDrive WHERE (InterfaceType='USB') GET DeviceID, SerialNumber /FORMAT:list`; const data = yield this.wmic(command); return this.parse(data); }); } getPartitions(deviceId) { return __awaiter(this, void 0, void 0, function* () { let command = `Win32_DiskDrive WHERE (DeviceID='${deviceId}') ASSOC:list /RESULTCLASS:Win32_DiskPartition`; const data = yield this.wmic(command); return this.parse(data); }); } getDisks(deviceId) { return __awaiter(this, void 0, void 0, function* () { let command = `Win32_DiskPartition WHERE (DeviceID='${deviceId}') ASSOC:list /RESULTCLASS:Win32_LogicalDisk`; const data = yield this.wmic(command); return this.parse(data); }); } wmic(command) { return __awaiter(this, void 0, void 0, function* () { command = command.replace(/\\/g, '\\\\'); command = `wmic /NAMESPACE: \\\\root\\cimv2 /NODE: 127.0.0.1 PATH ${command}`; const { stdout } = yield asyncExec(command); return stdout.toString(); }); } parse(data) { return __awaiter(this, void 0, void 0, function* () { const results = []; const items = data.split(`${EOL}${EOL}`); for (const item of items) { if (item) { const lines = item.split(`${EOL}`); if (lines.length > 1) { const result = {}; for (const line of lines) { const keyval = line.split('='); result[keyval[0]] = keyval[1]; } results.push(result); } } } return results; }); } } exports.WindowsImplementation = WindowsImplementation; //# sourceMappingURL=windows.js.map