add os release

This commit is contained in:
Cheney 2025-03-11 17:29:48 +08:00
parent 8149846f16
commit 97a76195c2

View File

@ -2,14 +2,24 @@ const os = require('os');
const diskusage = require('diskusage-ng'); const diskusage = require('diskusage-ng');
const { physicalCpuCount } = require('physical-cpu-count'); const { physicalCpuCount } = require('physical-cpu-count');
const { formatBytes } = require("./util/convutil") const { formatBytes } = require("./util/convutil")
const { exec } = require('child_process');
const { promisify } = require('util');
const fs = require('fs');
const path = require('path');
// 将 exec 函数转换为返回 Promise 的函数
const execPromise = promisify(exec);
// 将 readFile 函数转换为返回 Promise 的函数
const readFilePromise = promisify(fs.readFile);
// 获取操作系统信息 // 获取操作系统信息
function getOSInfo() { async function getOSInfo() {
return { return {
type: os.type(), type: os.type(),
platform: os.platform(), platform: os.platform(),
release: os.release(), release: os.release(),
arch: os.arch() arch: os.arch(),
os: await getOSDescription()
}; };
} }
@ -65,7 +75,7 @@ async function getDiskInfo() {
// 主函数,调用上述函数获取所有信息 // 主函数,调用上述函数获取所有信息
async function getAllDeviceInfo() { async function getAllDeviceInfo() {
const osInfo = getOSInfo(); const osInfo = await getOSInfo();
const cpuInfo = getCPUInfo(); const cpuInfo = getCPUInfo();
const memoryInfo = getMemoryInfo(); const memoryInfo = getMemoryInfo();
const diskInfo = await getDiskInfo(); const diskInfo = await getDiskInfo();
@ -78,6 +88,45 @@ async function getAllDeviceInfo() {
}; };
} }
async function getOSDescription() {
if (os.platform() === 'win32') {
return undefined;
}
try {
// 尝试执行 lsb_release 命令
const { stdout } = await execPromise('lsb_release -d');
// 提取描述信息
const distroDescription = stdout.split(':')[1].trim();
return distroDescription;
} catch (lsbError) {
try {
// lsb_release 命令不可用,尝试读取 /etc/os-release 文件
const osReleasePath = path.join('/', 'etc', 'os-release');
const data = await readFilePromise(osReleasePath, 'utf8');
// 解析文件内容
const lines = data.split('\n');
const info = {};
lines.forEach(line => {
const [key, value] = line.split('=');
if (key && value) {
info[key] = value.replace(/^"|"$/g, '');
}
});
// 获取 PRETTY_NAME
const prettyName = info['PRETTY_NAME'];
if (prettyName) {
return prettyName;
}
} catch (fileError) {
console.error('读取 /etc/os-release 文件时出错:', fileError);
}
}
return undefined;
}
module.exports = { module.exports = {
getOSInfo, getOSInfo,
getCPUInfo, getCPUInfo,