linux-os-release
Version:
Reads Linux operating system identification data
72 lines (70 loc) • 2.36 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// index.ts
var linux_os_release_exports = {};
__export(linux_os_release_exports, {
default: () => linux_os_release_default,
parseOsReleaseText: () => parseOsReleaseText,
readLinuxOSReleaseInfo: () => readLinuxOSReleaseInfo
});
module.exports = __toCommonJS(linux_os_release_exports);
var import_node_fs = require("node:fs");
function parseOsReleaseText(text) {
const fields = {};
for (const line of text.split("\n")) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith("#")) {
continue;
}
const match = trimmed.match(/^([A-Z_][A-Z0-9_]*)=(.*)$/);
if (!match) {
continue;
}
const [, key, rawValue] = match;
let value = rawValue;
if (value.startsWith('"') && value.endsWith('"') || value.startsWith("'") && value.endsWith("'")) {
value = value.slice(1, -1);
if (rawValue.startsWith('"')) {
value = value.replace(/\\([\$"\\`])/g, "$1");
}
}
fields[key] = value;
}
return fields;
}
async function readLinuxOSReleaseInfo() {
const paths = ["/etc/os-release", "/usr/lib/os-release"];
let text;
for (const p of paths) {
try {
text = await import_node_fs.promises.readFile(p, "utf8");
break;
} catch {
}
}
if (!text)
throw new Error("os-release file not found in any standard location");
return parseOsReleaseText(text);
}
var linux_os_release_default = readLinuxOSReleaseInfo;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
parseOsReleaseText,
readLinuxOSReleaseInfo
});