All files / util os-checker.ts

100% Statements 33/33
100% Branches 16/16
100% Functions 7/7
100% Lines 32/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 1132x   2x                           2x 2x   2x   2x 2x   2x           2x                         2x 2x       2x   2x 2x   2x           2x                           2x 5x   5x 5x 5x   5x 5x   5x           5x           2x 11x   11x   5x   2x   2x   2x            
import {release, platform} from 'os';
 
import {execPromise} from './helpers';
 
/**
 * Executes `lsb_release -a` command in terminal.
 *
 * ```
 * Distributor ID: Ubuntu
 * Description:    Ubuntu 22.04.4 LTS
 * Release:        22.04
 * Codename:       jammy
 * ```
 *
 * Parses os and os-version from the response.
 */
const getLinuxInfo = async () => {
  const {stdout} = await execPromise('lsb_release -a');
 
  const parseLinuxVersion = (lsbReleaseResponse: string) => {
    const regex =
      /Distributor ID: ([^\n]+)\nDescription: +([^ ]+) +([^ ]+) +(.+)\n/;
    const match = lsbReleaseResponse.match(regex);
 
    return {
      os: match ? match[1] : platform(),
      'os-version': match ? `${match[3]} ${match[4]}` : release(),
    };
  };
 
  return parseLinuxVersion(stdout);
};
 
/**
 * Executes in CMD `systeminfo | findstr /B /C:"OS Name" /B /C:"OS Version"` command.
 *
 * ```
 * OS Name:                   Microsoft Windows 11 Enterprise
 * OS Version:                10.0.22631 N/A Build 22631
 * ```
 *
 * Parses os and os-version from the response.
 */
const getWindowsInfo = async () => {
  const {stdout} = await execPromise(
    'systeminfo | findstr /B /C:"OS Name" /B /C:"OS Version"'
  );
 
  const parseWindowsInfo = (systemInfoResponse: string) => {
    const regex =
      /OS Name:\s+([^\n]+)\nOS Version:\s+([\d.]+)\s+(N\/A\s+Build\s+(\d+))/;
    const match = systemInfoResponse.match(regex);
 
    return {
      os: match ? match[1] : platform(),
      'os-version': match ? `${match[2]} ${match[3]}` : release(),
    };
  };
 
  return parseWindowsInfo(stdout);
};
 
/**
 * Executes `sw_vers` command in terminal.
 *
 * ```
 * ProductName:      macOS
 * ProductVersion:   14.3.1
 * BuildVersion:     23D60
 * ```
 *
 * Parses os and os version from the response.
 */
const getMacVersion = async () => {
  const {stdout} = await execPromise('sw_vers');
 
  const parseMacInfo = (swVersResponse: string) => {
    const productNameRegex = /ProductName:\s*(.+)/;
    const productVersionRegex = /ProductVersion:\s*(.+)/;
 
    const nameMatch = swVersResponse.match(productNameRegex);
    const versionMatch = swVersResponse.match(productVersionRegex);
 
    return {
      os: nameMatch ? nameMatch[1].trim() : platform(),
      'os-version': versionMatch ? versionMatch[1].trim() : release(),
    };
  };
 
  return parseMacInfo(stdout);
};
 
/**
 * Finds operating system information like name and version.
 */
export const osInfo = async () => {
  const osKind = platform();
 
  switch (osKind) {
    case 'darwin':
      return getMacVersion();
    case 'linux':
      return getLinuxInfo();
    case 'win32':
      return getWindowsInfo();
    default:
      return {
        os: osKind,
        'os-version': release(),
      };
  }
};