@egodigital/egoose
Version:
Helper classes and functions for Node.js 10 or later.
79 lines • 2.67 kB
JavaScript
;
/**
* This file is part of the @egodigital/egoose distribution.
* Copyright (c) e.GO Digital GmbH, Aachen, Germany (https://www.e-go-digital.com/)
*
* @egodigital/egoose is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, version 3.
*
* @egodigital/egoose is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDiskSpace = exports.getCpuUsage = void 0;
const diskspace = require('diskspace');
const os = require('os-utils');
const index_1 = require("../index");
/**
* Returns the CPU usage in percentage.
*
* @return {Promise<number>} The promise with the CPU usage.
*/
function getCpuUsage() {
return new Promise((resolve, reject) => {
try {
os.cpuUsage(function (v) {
resolve(v);
});
}
catch (e) {
reject(e);
}
});
}
exports.getCpuUsage = getCpuUsage;
/**
* Returns the diskspace of a mount point.
*
* @param {string} [mountPoint] The custom name / path of the endpoint to check.
*
* @return {Promise<DiskSpaceResult>} The promise with the result.
*/
function getDiskSpace(mountPoint) {
mountPoint = index_1.toStringSafe(mountPoint);
return new Promise((resolve, reject) => {
try {
if (index_1.isEmptyString(mountPoint)) {
if ('win32' === process.platform) {
mountPoint = 'C'; // drive C
}
else {
mountPoint = '/'; // system root
}
}
diskspace.check(mountPoint, function (err, result) {
if (err) {
reject(err);
}
else {
resolve({
free: parseInt(index_1.toStringSafe(result.free).trim()),
total: parseInt(index_1.toStringSafe(result.total).trim()),
used: parseInt(index_1.toStringSafe(result.used).trim()),
});
}
});
}
catch (e) {
reject(e);
}
});
}
exports.getDiskSpace = getDiskSpace;
//# sourceMappingURL=index.js.map