MCPcopy Create free account
hub / github.com/Snapchat/Valdi / detectLinuxDistro

Function detectLinuxDistro

npm_modules/cli/src/utils/linuxDistro.ts:61–171  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

59 * Detects the current Linux distribution and package manager
60 */
61export function detectLinuxDistro(): LinuxDistroInfo {
62 // Try to read /etc/os-release (most modern distributions)
63 if (fs.existsSync('/etc/os-release')) {
64 try {
65 const osRelease = fs.readFileSync('/etc/os-release', 'utf8');
66 const lines = osRelease.split('\n');
67 const info: { [key: string]: string } = {};
68
69 for (const line of lines) {
70 const match = line.match(/^([^=]+)=(.*)$/);
71 if (match && match[1] && match[2]) {
72 // Remove quotes from value
73 info[match[1]] = match[2].replaceAll(/^["']|["']$/g, '');
74 }
75 }
76
77 const id = info['ID']?.toLowerCase() ?? '';
78 const idLike = info['ID_LIKE']?.toLowerCase() ?? '';
79 const name = info['NAME'] ?? 'Linux';
80 const version = info['VERSION_ID'];
81
82 // Detect distribution type based on ID and ID_LIKE
83 if (id.includes('debian') || id.includes('ubuntu') || idLike.includes('debian') || idLike.includes('ubuntu')) {
84 const result: LinuxDistroInfo = {
85 type: LinuxDistroType.DEBIAN,
86 name,
87 packageManager: getPackageManager(LinuxDistroType.DEBIAN),
88 };
89 if (version) result.version = version;
90 return result;
91 } else if (
92 id.includes('fedora') ||
93 id.includes('rhel') ||
94 id.includes('centos') ||
95 id.includes('rocky') ||
96 idLike.includes('fedora') ||
97 idLike.includes('rhel')
98 ) {
99 const result: LinuxDistroInfo = {
100 type: LinuxDistroType.REDHAT,
101 name,
102 packageManager: getPackageManager(LinuxDistroType.REDHAT),
103 };
104 if (version) result.version = version;
105 return result;
106 } else if (id.includes('arch') || id.includes('manjaro') || idLike.includes('arch')) {
107 const result: LinuxDistroInfo = {
108 type: LinuxDistroType.ARCH,
109 name,
110 packageManager: getPackageManager(LinuxDistroType.ARCH),
111 };
112 if (version) result.version = version;
113 return result;
114 } else if (id.includes('suse') || idLike.includes('suse')) {
115 const result: LinuxDistroInfo = {
116 type: LinuxDistroType.SUSE,
117 name,
118 packageManager: getPackageManager(LinuxDistroType.SUSE),

Callers 4

constructorMethod · 0.90
linuxSetupFunction · 0.90
getPackageNameFunction · 0.85
buildInstallCommandFunction · 0.85

Calls 5

checkCommandExistsFunction · 0.90
readFileSyncMethod · 0.80
splitMethod · 0.80
getPackageManagerFunction · 0.70
matchMethod · 0.45

Tested by

no test coverage detected