import * as fs from 'fs' ; import * as os from 'os' ; export class LinuxDistribution { constructor ( public name , public version ) { } /**
     * There is no standard way on Linux to find the distribution name and version.
     * Recently, systemd has pushed to standardize the os-release file. This has
     * seen adoption in "recent" versions of all major distributions.
     * https://www.freedesktop.org/software/systemd/man/os-release.html
     */ public static GetDistroInformation ( ) { const linuxDistro = LinuxDistribution . getDistroInformationFromFile ( '/etc/os-release' ) . catch ( ( ) => LinuxDistribution . getDistroInformationFromFile ( '/usr/lib/os-release' ) ) . catch ( ( ) => Promise . resolve ( new LinuxDistribution ( 'unknown' , 'unknown' ) ) ) ; return linuxDistro ; } private static getDistroInformationFromFile ( path ) { return new Promise < LinuxDistribution > ( ( resolve , reject ) => { fs . readFile ( path , 'utf8' , ( error , data ) => { if ( error ) { reject ( error ) ; return ; } resolve ( LinuxDistribution . getDistroInformation ( data ) ) ; } ) ; } ) ; } public static getDistroInformation ( data ) { const idKey = 'ID' ; const versionKey = 'VERSION_ID' ; let distroName = 'unknown' ; let distroVersion = 'unknown' ; const keyValues = data . split ( os . EOL ) ; for ( let i = 0 ; i < keyValues . length ; i ++ ) { const keyValue = keyValues [ i ] . split ( '=' ) ; if ( keyValue . length === 2 ) { if ( keyValue [ 0 ] === idKey ) { distroName = keyValue [ 1 ] ; } else if ( keyValue [ 0 ] === versionKey ) { distroVersion = keyValue [ 1 ] ; } } } return new LinuxDistribution ( distroName , distroVersion ) ; } }