diff --git a/kit/src/platform.js b/kit/src/platform.js index 5581908..af0b62b 100644 --- a/kit/src/platform.js +++ b/kit/src/platform.js @@ -2,14 +2,24 @@ const os = require('os'); const diskusage = require('diskusage-ng'); const { physicalCpuCount } = require('physical-cpu-count'); 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 { type: os.type(), platform: os.platform(), release: os.release(), - arch: os.arch() + arch: os.arch(), + os: await getOSDescription() }; } @@ -65,7 +75,7 @@ async function getDiskInfo() { // 主函数,调用上述函数获取所有信息 async function getAllDeviceInfo() { - const osInfo = getOSInfo(); + const osInfo = await getOSInfo(); const cpuInfo = getCPUInfo(); const memoryInfo = getMemoryInfo(); 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 = { getOSInfo, getCPUInfo,