Refactor: Move installSystemPackage function to a separate module for better organization
This commit is contained in:
+1
-26
@@ -1,13 +1,13 @@
|
||||
import * as path from 'path';
|
||||
import * as fs from 'fs';
|
||||
import * as os from 'os';
|
||||
import { spawnSync } from 'child_process';
|
||||
import { Command } from 'commander';
|
||||
import { getPlatformInfo } from './core/platform';
|
||||
import { getMatchingAsset } from './core/matcher';
|
||||
import { findBinary } from './core/finder';
|
||||
import { fetchLatestRelease, fetchLatestReleaseRaw, downloadAsset } from './core/downloader';
|
||||
import { extractAsset } from './core/extractor';
|
||||
import { installSystemPackage } from './core/installer';
|
||||
|
||||
interface CliOptions {
|
||||
appName?: string;
|
||||
@@ -58,31 +58,6 @@ function getInstallDir(installPath?: string): string {
|
||||
return '/usr/local/bin';
|
||||
}
|
||||
|
||||
function installSystemPackage(downloadPath: string): void {
|
||||
const fileName = path.basename(downloadPath).toLowerCase();
|
||||
|
||||
const command: { binary: string; args: string[] } | undefined = fileName.endsWith('.deb')
|
||||
? { binary: 'dpkg', args: ['-i', downloadPath] }
|
||||
: fileName.endsWith('.pkg')
|
||||
? { binary: 'installer', args: ['-pkg', downloadPath, '-target', '/'] }
|
||||
: fileName.endsWith('.rpm')
|
||||
? { binary: 'rpm', args: ['-i', downloadPath] }
|
||||
: undefined;
|
||||
|
||||
if (!command) {
|
||||
throw new Error(`Unsupported package type: ${fileName}`);
|
||||
}
|
||||
|
||||
const isRoot = process.getuid && process.getuid() === 0;
|
||||
const commandToRun = isRoot ? command.binary : 'sudo';
|
||||
const argsToRun = isRoot ? command.args : [command.binary, ...command.args];
|
||||
|
||||
const result = spawnSync(commandToRun, argsToRun, { stdio: 'inherit' });
|
||||
if (result.status !== 0) {
|
||||
throw new Error(`Failed to install package using ${commandToRun} ${argsToRun.join(' ')}.`);
|
||||
}
|
||||
}
|
||||
|
||||
async function run() {
|
||||
let tempDir: string | undefined;
|
||||
const program = new Command();
|
||||
|
||||
Reference in New Issue
Block a user