Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

2.8 KB
Raw Blame
import fs from 'fs';

const { platform } = process;

export const IS_MAC = platform === 'darwin';
export const IS_WIN = platform === 'win32';
export const IS_LINUX = platform === 'linux';

export type LinuxPackageFamily = 'deb' | 'rpm';

// Distro IDs / ID_LIKE families that ship an RPM-based package manager.
const RPM_FAMILY_IDS = new Set([
  'rhel',
  'fedora',
  'centos',
  'rocky',
  'almalinux',
  'ol', // Oracle Linux
  'oracle',
  'amzn', // Amazon Linux
  'mariner',
  'azurelinux',
  'suse',
  'opensuse',
  'opensuse-leap',
  'opensuse-tumbleweed',
  'sles',
]);

// Distro IDs / ID_LIKE families that ship a DEB-based package manager.
const DEB_FAMILY_IDS = new Set([
  'debian',
  'ubuntu',
  'linuxmint',
  'pop',
  'elementary',
  'kali',
  'raspbian',
  'devuan',
]);

// Parse the shell-style key=value pairs of an /etc/os-release file, stripping
// the optional surrounding quotes around values.
function parseOsRelease(content: string): Record<string, string> {
  const fields: Record<string, string> = {};
  for (const rawLine of content.split('\n')) {
    const line = rawLine.trim();
    if (!line || line.startsWith('#')) continue;
    const separator = line.indexOf('=');
    if (separator === -1) continue;
    const key = line.slice(0, separator).trim();
    let value = line.slice(separator + 1).trim();
    if (
      value.length >= 2 &&
      ((value.startsWith('"') && value.endsWith('"')) ||
        (value.startsWith("'") && value.endsWith("'")))
    ) {
      value = value.slice(1, -1);
    }
    if (key) fields[key] = value;
  }
  return fields;
}

// Detect the package family from /etc/os-release. The distro's own ID wins over
// ID_LIKE hints, and an unknown distro falls back to 'deb' to preserve Pake's
// historical default. Accepts content directly so the decision is unit-testable
// without a real /etc/os-release.
export function detectLinuxPackageFamily(
  osReleaseContent?: string,
): LinuxPackageFamily {
  let content = osReleaseContent;
  if (content === undefined) {
    try {
      content = fs.readFileSync('/etc/os-release', 'utf-8');
    } catch {
      return 'deb';
    }
  }

  const fields = parseOsRelease(content);
  const id = (fields.ID ?? '').toLowerCase().trim();
  const idLike = (fields.ID_LIKE ?? '')
    .toLowerCase()
    .split(/\s+/)
    .filter(Boolean);

  for (const token of [id, ...idLike]) {
    if (DEB_FAMILY_IDS.has(token)) return 'deb';
    if (RPM_FAMILY_IDS.has(token)) return 'rpm';
  }
  return 'deb';
}

// Default Linux bundle targets, chosen by the host distro's package family so
// RPM-based distros (Fedora/RHEL/Oracle/Rocky/Alma/openSUSE) get a native .rpm
// instead of a .deb their package manager cannot install. AppImage stays as a
// universal fallback in both cases.
export function getDefaultLinuxTargets(): string {
  return detectLinuxPackageFamily() === 'rpm' ? 'rpm,appimage' : 'deb,appimage';
}