@@ -20,6 +20,9 @@ const OSRegex = {
2020 id_like : / ^ i d _ l i k e \s * = \s * " ? ( [ \w \s ] * ) " ? $ / im,
2121} ;
2222
23+ /** Helper Static so that a consistent UNKNOWN value is used */
24+ export const UNKNOWN = 'unknown' ;
25+
2326export interface OtherOS {
2427 os : 'aix' | 'android' | 'darwin' | 'freebsd' | 'openbsd' | 'sunos' | 'win32' | 'cygwin' | string ;
2528}
@@ -74,53 +77,67 @@ async function getLinuxInformation(): Promise<LinuxOS> {
7477
7578 const upstreamLSB = await tryReleaseFile ( '/etc/upstream-release/lsb-release' , parseLSB ) ;
7679
77- if ( ! isNullOrUndefined ( upstreamLSB ) ) {
80+ if ( isValidOs ( upstreamLSB ) ) {
7881 log ( 'getLinuxInformation: Using UpstreamLSB' ) ;
7982
8083 return upstreamLSB ;
8184 }
8285
8386 const etcOsRelease = await tryReleaseFile ( '/etc/os-release' , parseOS ) ;
8487
85- if ( ! isNullOrUndefined ( etcOsRelease ) ) {
88+ if ( isValidOs ( etcOsRelease ) ) {
8689 log ( 'getLinuxInformation: Using etcOsRelease' ) ;
8790
8891 return etcOsRelease ;
8992 }
9093
9194 const usrOsRelease = await tryReleaseFile ( '/usr/lib/os-release' , parseOS ) ;
9295
93- if ( ! isNullOrUndefined ( usrOsRelease ) ) {
96+ if ( isValidOs ( usrOsRelease ) ) {
9497 log ( 'getLinuxInformation: Using usrOsRelease' ) ;
9598
9699 return usrOsRelease ;
97100 }
98101
99102 const etcLSBRelease = await tryReleaseFile ( '/etc/lsb-release' , parseLSB ) ;
100103
101- if ( ! isNullOrUndefined ( etcLSBRelease ) ) {
104+ if ( isValidOs ( etcLSBRelease ) ) {
102105 log ( 'getLinuxInformation: Using etcLSBRelease' ) ;
103106
104107 return etcLSBRelease ;
105108 }
106109
107- console . warn ( 'Could not find any Release File, using fallback binary ' ) ;
110+ console . warn ( 'Could not find any valid Release File, using fallback information ' ) ;
108111
109112 // if none has worked, return unknown
110113 return {
111114 os : 'linux' ,
112- dist : 'unknown' ,
115+ dist : UNKNOWN ,
113116 release : '' ,
114117 } ;
115118}
116119
120+ /**
121+ * Helper function to check if the input os is valid
122+ * @param os The OS information to check
123+ * @returns `true` if not undefined AND not UNKNOWN
124+ */
125+ export function isValidOs ( os : LinuxOS | undefined ) : os is LinuxOS {
126+ // helper for debugging
127+ if ( os && os . dist === UNKNOWN ) {
128+ log ( 'isValidOS: found defined os, but was unknown:' , os ) ;
129+ }
130+
131+ return ! isNullOrUndefined ( os ) && os . dist !== UNKNOWN ;
132+ }
133+
117134/**
118135 * Parse LSB-like output (either command or file)
119136 */
120137export function parseLSB ( input : string ) : LinuxOS {
121138 return {
122139 os : 'linux' ,
123- dist : input . match ( LSBRegex . name ) ?. [ 1 ] . toLocaleLowerCase ( ) ?? 'unknown' ,
140+ dist : input . match ( LSBRegex . name ) ?. [ 1 ] . toLocaleLowerCase ( ) ?? UNKNOWN ,
124141 codename : input . match ( LSBRegex . codename ) ?. [ 1 ] . toLocaleLowerCase ( ) ,
125142 release : input . match ( LSBRegex . release ) ?. [ 1 ] . toLocaleLowerCase ( ) ?? '' ,
126143 } ;
@@ -132,7 +149,7 @@ export function parseLSB(input: string): LinuxOS {
132149export function parseOS ( input : string ) : LinuxOS {
133150 return {
134151 os : 'linux' ,
135- dist : input . match ( OSRegex . name ) ?. [ 1 ] . toLocaleLowerCase ( ) ?? 'unknown' ,
152+ dist : input . match ( OSRegex . name ) ?. [ 1 ] . toLocaleLowerCase ( ) ?? UNKNOWN ,
136153 codename : input . match ( OSRegex . codename ) ?. [ 1 ] . toLocaleLowerCase ( ) ,
137154 release : input . match ( OSRegex . release ) ?. [ 1 ] . toLocaleLowerCase ( ) ?? '' ,
138155 id_like : input . match ( OSRegex . id_like ) ?. [ 1 ] . toLocaleLowerCase ( ) . split ( ' ' ) ,
0 commit comments