UNPKG

lup-system

Version:

NodeJS library to retrieve system information and utilization.

73 lines (72 loc) 3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDrives = getDrives; const utils_1 = require("./utils"); const VIRTUAL_DRIVE_TYPES = [ 'devtmpfs', // Device filesystem 'tmpfs', // Temporary filesystem 'overlay', // Overlay filesystem ]; /** * Returns information about the drives on the system (in Windows the logical drives are returned). * * @param includeVirtual If virtual drives should be included in the results (only relevant for Linux and macOS). * @returns List of drive information objects. */ async function getDrives(includeVirtual = false) { const drives = []; switch (process.platform) { case 'darwin': case 'linux': { const output = await (0, utils_1.execCommand)('df -PT --block-size=1').catch(() => ''); const lines = output.split('\n'); for (let i = 1; i < lines.length; i++) { // skip first line (header) const line = lines[i].trim(); if (!line) continue; const parts = line.split(/\s+/); // columns are separated by one or more spaces if (parts.length < 5) continue; // expect at least 5 columns const type = parts[1].toLowerCase(); if (!includeVirtual && VIRTUAL_DRIVE_TYPES.includes(type)) continue; // skip virtual drives const used = parseInt(parts[3], 10) || 0; const total = parseInt(parts[2], 10) || 0; drives.push({ filesystem: parts[0], type, total, mount: (parts[6] || parts[0]), utilization: { used, free: parseInt(parts[4], 10) || 0, percentage: total !== 0 ? used / total : 0, }, }); } break; } case 'win32': { const output = await (0, utils_1.execCommand)('powershell -Command "Get-CimInstance -ClassName Win32_LogicalDisk | Select-Object Caption, VolumeName, Size, FreeSpace, FileSystem | ConvertTo-Json"').catch(() => ''); const json = JSON.parse(output); for (const drive of json) { const total = parseInt(drive.Size, 10) || 0; const free = parseInt(drive.FreeSpace, 10) || 0; drives.push({ filesystem: drive.Caption, mount: drive.VolumeName || drive.Caption, type: (drive.FileSystem || 'unknown').toLowerCase(), total, utilization: { free, used: total - free, percentage: total !== 0 ? (total - free) / total : 0, }, }); } break; } } return drives; }