|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import cp from 'node:child_process'; |
| 3 | +import path from 'node:path'; |
| 4 | +import fs from 'node:fs'; |
| 5 | + |
| 6 | +import type { InstalledAppInfo, InstallablePackage } from './types'; |
| 7 | +import { execute } from '../execute'; |
| 8 | +/** |
| 9 | + * Call dnf to get the package name |
| 10 | + */ |
| 11 | +function getPackageName(filepath: string) { |
| 12 | + const { status, stdout, stderr } = cp.spawnSync( |
| 13 | + 'rpm', |
| 14 | + ['--query', '--queryformat', '%{NAME}', '--package', filepath], |
| 15 | + { encoding: 'utf8' } |
| 16 | + ); |
| 17 | + assert.equal( |
| 18 | + status, |
| 19 | + 0, |
| 20 | + `Expected a clean exit, got status ${status || 'null'}: ${stderr}` |
| 21 | + ); |
| 22 | + return stdout.trim(); |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Check if a package is installed (by name) |
| 27 | + */ |
| 28 | +export function isInstalled(packageName: string) { |
| 29 | + const result = cp.spawnSync( |
| 30 | + 'sudo', |
| 31 | + ['dnf', 'list', 'installed', packageName], |
| 32 | + { |
| 33 | + stdio: 'inherit', |
| 34 | + } |
| 35 | + ); |
| 36 | + return result.status === 0; |
| 37 | +} |
| 38 | + |
| 39 | +export function installLinuxRpm({ |
| 40 | + appName, |
| 41 | + filepath, |
| 42 | +}: InstallablePackage): InstalledAppInfo { |
| 43 | + const packageName = getPackageName(filepath); |
| 44 | + const installPath = `/usr/lib/${packageName}`; |
| 45 | + const appPath = path.resolve(installPath, appName); |
| 46 | + |
| 47 | + function uninstall() { |
| 48 | + execute('sudo', ['dnf', 'remove', '-y', packageName]); |
| 49 | + } |
| 50 | + |
| 51 | + if (isInstalled(packageName)) { |
| 52 | + console.warn( |
| 53 | + 'Found an existing install directory (likely from a previous run): Uninstalling first' |
| 54 | + ); |
| 55 | + uninstall(); |
| 56 | + } |
| 57 | + |
| 58 | + console.warn( |
| 59 | + "Installing globally, since we haven't discovered a way to specify an install path" |
| 60 | + ); |
| 61 | + assert(!isInstalled(packageName), 'Expected the package to not be installed'); |
| 62 | + assert( |
| 63 | + !fs.existsSync(installPath), |
| 64 | + `Expected no install directory to exist: ${installPath}` |
| 65 | + ); |
| 66 | + execute('sudo', ['dnf', 'install', '-y', filepath]); |
| 67 | + |
| 68 | + assert(isInstalled(packageName), 'Expected the package to be installed'); |
| 69 | + assert( |
| 70 | + fs.existsSync(installPath), |
| 71 | + `Expected an install directory to exist: ${installPath}` |
| 72 | + ); |
| 73 | + |
| 74 | + // Check that the executable will run without being quarantined or similar |
| 75 | + execute('xvfb-run', [appPath, '--version']); |
| 76 | + |
| 77 | + return { |
| 78 | + appPath: installPath, |
| 79 | + uninstall, |
| 80 | + }; |
| 81 | +} |
0 commit comments