extensions-repo/inst-configs-repo/kylin-code/config/os_classify_script-0.0.1.js

142 lines
4.0 KiB
JavaScript
Raw Normal View History

2024-01-30 10:57:45 +08:00
const os = require('os');
const fs = require('fs');
var os_arch; //arch x86_64
var os_org; //Kylin
var os_type; // Desktop Server
var os_release;// eg:2203
var os_version_id; // V10
var os_sp;//sp
var os_custom; //hwe
var pretty_name;
//检查系统架构
function getArch() {
const arch = os.arch();
let res = '';
switch (arch) {
case 'x64':
case 'x86':
case "x86_64": res = 'x86'; break;
case 'arm':
case 'arm64':
case 'aarch64': res = 'arm'; break;
case 'loongarch': res = 'loongarch'; break;
default: res = "other";
}
return res;
}
function returnosinfo() {
if (os_arch == 'x86' && os_org == 'openkylin' && os_type == 'desktop') {
return "x86_openkylin_desktop";
}
return "undefined"
}
function getosinfo() {
const kyinfoPath = '/etc/.kyinfo';
const osReleasePath = '/etc/os-release';
//1、架构
//2、是否是麒麟系统
//3、是桌面系统还是服务器系统
//4、是否是V10
//5、是否带sp是sp几
//6、小版本号
//7、定制版字符
var dist_id;
var os_info
os_arch = getArch();
/*
1.存在.kyinfo的系统 取值dist_id 架构之前的信息
2.不在.kyinfo的系统但是存在os-release 的kylin系统
3.不是kylin的系统但是存在os-release
目前可以先做第一和第二种分类
*/
let data;
let releasedata;
if (fs.existsSync(kyinfoPath) && fs.existsSync(osReleasePath)) {
os_org = "kylin";
try {
let milestone;
let dist_id;
let tmpstr;
let version_id;
data = fs.readFileSync(kyinfoPath, 'utf8');
releasedata = fs.readFileSync(osReleasePath, 'utf8');
// 使用 split 方法将文件内容分割成行数组
const lines = data.split('\n');
// 遍历行数组 获取系统架构
lines.forEach((line) => {
if (line.startsWith("milestone=")) {
milestone = line.slice(10).toLocaleLowerCase();
} else if (line.startsWith("dist_id=")) {
dist_id = line.slice(8).toLocaleLowerCase();
}
});//end lines
const releasedatalines = releasedata.split('\n');
releasedatalines.forEach((line) => {
if (line.startsWith("PRETTY_NAME=")) {
pretty_name = line.slice(12).toLocaleLowerCase().replace(/"/g, '');
} else if (line.startsWith("VERSION_ID=")) {
version_id = line.slice(11).toLocaleLowerCase().replace(/"/g, '');
}
});
if (version_id) {
os_version_id = version_id;
}
os_type = dist_id.includes('desktop') ? "desktop" : "server";
if (milestone.indexOf('release')) {
os_release = milestone.slice(milestone.indexOf('release') + 8);
}
if (dist_id.includes('edu')) {
os_custom = 'edu';
} else {
os_custom = 'gen';
}
if (dist_id.includes('hwe')) {
os_custom += 'hwe';
}
if (dist_id.includes('gfb')) {
l
os_custom += 'gfb'
}
} catch (err) {
os_info = "";
}
}else if(fs.existsSync(osReleasePath)){
try {
let milestone;
let dist_id;
let tmpstr;
let version_id;
releasedata = fs.readFileSync(osReleasePath, 'utf8');
const releasedatalines = releasedata.split('\n');
releasedatalines.forEach((line) => {
if (line.startsWith("PRETTY_NAME=")) {
pretty_name = line.slice(12).toLocaleLowerCase().replace(/"/g, '');
} else if (line.startsWith("VERSION_ID=")) {
version_id = line.slice(11).toLocaleLowerCase().replace(/"/g, '');
}else if (line.startsWith("ID=")) {
os_org = line.slice(3).toLocaleLowerCase().replace(/"/g, '');
}else if (line.startsWith("VERSION_CODENAME=")) {
os_custom = line.slice(17).toLocaleLowerCase().replace(/"/g, '');
}
});
if (version_id) {
os_version_id = version_id;
}
if(pretty_name.includes('openkylin'))
os_type ="desktop";
} catch (err) {
os_info = "";
}
}
os_info = returnosinfo();
return os_info;
}
module.exports = { getosinfo }