node-drivelist
Version:
Drivelist with usage and mountpoint
47 lines (46 loc) • 1.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parse = exports.execDriveList = void 0;
var child_process_1 = require("child_process");
var execDriveList = function (cb) {
(0, child_process_1.execFile)("df", ["-P", "-k"], function (err, stdout) {
if (err) {
return err;
}
var lines = stdout.split("\n").filter(function (line) { return line.length; });
lines.shift();
var drives = lines.map(function (line) { return (0, exports.parse)(line); });
try {
cb(null, drives);
}
catch (e) {
cb(e);
}
});
};
exports.execDriveList = execDriveList;
var parse = function (driveLine) {
var matches = driveLine.match(/^.+\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+%)\s+([\dA-Za-z\/]*)/);
if (!matches || matches.length !== 6) {
throw new Error("Unexpected df output: [" + driveLine + "]");
}
var total = Number(matches[1]);
var used = Number(matches[2]);
var available = Number(matches[3]);
var percentageUsed = Number(matches[4].replace("%", ""));
var mountpoint = matches[5];
var name = mountpoint.split("/").pop();
return {
total: total * 1024,
used: used * 1024,
available: available * 1024,
percentageUsed: percentageUsed,
mountpoint: mountpoint,
name: name,
};
};
exports.parse = parse;
module.exports = {
execDriveList: exports.execDriveList,
parse: exports.parse,
};