import os from "node:os";
import fs from "node:fs";
interface ILinuxDistro {
name: string;
version: string;
}
export const getLinuxDistroInfo = (): ILinuxDistro => {
if (os.type() !== "Linux") {
throw new Error(
`${getLinuxDistroInfo.name}() 函数仅支持 GNU/Linux 操作系统。`
);
}
const linuxDistroInfo: ILinuxDistro = {
name: "",
version: ""
};
try {
const data = fs.readFileSync("/etc/os-release", "utf-8");
const lines = data.split("\n");
for (const line of lines) {
if (line.startsWith("NAME=")) {
linuxDistroInfo.name = line.substring(5).replace(/"/g, "");
} else if (line.startsWith("VERSION_ID=")) {
linuxDistroInfo.version = line.substring(11).replace(/"/g, "");
}
}
} catch (error: any) {
throw new Error(`读取 /etc/os-release 失败: ${error.message}`);
}
return linuxDistroInfo;
};