UNPKG

@2501-ai/cli

Version:

[![npm version](https://img.shields.io/npm/v/@2501-ai/cli.svg)](https://www.npmjs.com/package/@2501-ai/cli) [![HumanEval Score](https://img.shields.io/badge/HumanEval-96.95%25-brightgreen.svg)](https://www.2501.ai/research/full-humaneval-benchmark) [![Lic

217 lines (216 loc) 7.78 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSystemInfo = void 0; const child_process_1 = require("child_process"); const os_1 = __importDefault(require("os")); const node_util_1 = require("node:util"); const logger_1 = __importDefault(require("./logger")); const execAsync = (0, node_util_1.promisify)(child_process_1.exec); const LINUX_PACKAGE_MANAGERS = [ { cmd: 'apt', listCmd: (exclusionPattern) => `apt-mark showmanual 2>/dev/null | grep -vE '${exclusionPattern}'`, }, { cmd: 'dnf', listCmd: (exclusionPattern) => `dnf repoquery --userinstalled --queryformat "%{name}" 2>/dev/null | grep -vE '${exclusionPattern}'`, }, { cmd: 'yum', listCmd: (exclusionPattern) => `yum history list all 2>/dev/null | grep "U" | awk '{print $4}' | grep -vE '${exclusionPattern}'`, }, { cmd: 'pacman', listCmd: (exclusionPattern) => `pacman -Qe | grep -vE '${exclusionPattern}' | awk '{print $1}'`, }, { cmd: 'zypper', listCmd: (exclusionPattern) => `zypper search -i -t package | tail -n +5 | grep -vE '${exclusionPattern}' | awk '{print $3}'`, }, ]; const MACOS_PACKAGE_MANAGERS = [ { cmd: 'brew', listCmd: (exclusionPattern) => `brew leaves | grep -vE '${exclusionPattern}'`, }, { cmd: 'port', listCmd: (exclusionPattern) => `port echo requested | grep -vE '${exclusionPattern}' | awk '{print $1}'`, }, ]; function getGlobalNpmPackages() { return __awaiter(this, void 0, void 0, function* () { try { const command = "npm list -g --depth=0 | awk '{print $2}' | grep '@'"; const output = yield execAsync(command, { encoding: 'utf8' }); return output.stdout.split('\n').filter((line) => line.trim() !== ''); } catch (error) { return []; } }); } function getOSInfo() { return __awaiter(this, void 0, void 0, function* () { try { const { stdout } = yield execAsync('uname -a'); return stdout.trim(); } catch (error) { return (os_1.default.type() + ' ' + os_1.default.release() + ' ' + os_1.default.arch() + ' ' + os_1.default.platform()); } }); } function getPythonVersion() { return __awaiter(this, void 0, void 0, function* () { try { const { stdout } = yield execAsync('python --version'); return stdout.trim(); } catch (error) { return '(not installed)'; } }); } function getPython3Version() { return __awaiter(this, void 0, void 0, function* () { const { stdout } = yield execAsync('python3 --version'); return stdout.trim(); }); } function getPhpVersion() { return __awaiter(this, void 0, void 0, function* () { try { const { stdout } = yield execAsync('php --version'); return stdout.trim(); } catch (error) { return '(not installed)'; } }); } function getSystemInfo() { return __awaiter(this, void 0, void 0, function* () { const packageManagers = yield detectPackageManagers(); const installedPackages = yield getInstalledPackages(packageManagers); const osInfo = yield getOSInfo(); const pythonVersion = yield getPython3Version().catch(() => getPythonVersion()); const phpVersion = yield getPhpVersion(); const sysInfo = { platform: os_1.default.platform(), os_info: osInfo, installed_packages: installedPackages, }; return { sysInfo, nodeInfo: { version: process.version, global_packages: yield getGlobalNpmPackages(), }, pythonInfo: { version: pythonVersion, }, phpInfo: { version: phpVersion, }, }; }); } exports.getSystemInfo = getSystemInfo; function getPackageManagersForPlatform(exclusionPattern) { const platform = os_1.default.platform(); switch (platform) { case 'linux': return LINUX_PACKAGE_MANAGERS.map((pm) => (Object.assign(Object.assign({}, pm), { listCmd: pm.listCmd(exclusionPattern) }))); case 'darwin': return MACOS_PACKAGE_MANAGERS.map((pm) => (Object.assign(Object.assign({}, pm), { listCmd: pm.listCmd(exclusionPattern) }))); default: return []; } } const DEFAULT_PACKAGE_EXCLUSIONS = [ '^lib', '^python[0-9]?[@]?', '^gcc-', '^perl-', '^php[0-9]?-', '^ruby-', '^tex-', '^fonts-', '^xserver-', '^kernel-', '^linux-', '^openssh-', '^mime-', '^bind[0-9]?-', '^grub-', '^systemd-', '^udev-', '^desktop-', '^glib[0-9]?-', '^gtk[0-9]?-', ]; function detectPackageManagers(options = {}) { return __awaiter(this, void 0, void 0, function* () { const exclusions = [ ...DEFAULT_PACKAGE_EXCLUSIONS, ...(options.additionalExclusions || []), ]; const exclusionPattern = exclusions.join('|'); const packageManagers = getPackageManagersForPlatform(exclusionPattern); const pmChecks = yield Promise.all(packageManagers.map((pm) => __awaiter(this, void 0, void 0, function* () { try { yield execAsync(`which ${pm.cmd}`); return pm; } catch (_a) { return null; } }))); return pmChecks.filter(Boolean); }); } function getInstalledPackages(pms) { return __awaiter(this, void 0, void 0, function* () { if (!pms.length) { return {}; } try { const allPackages = yield Promise.all(pms.map((pm) => __awaiter(this, void 0, void 0, function* () { try { const { stdout } = yield execAsync(pm.listCmd); const packages = stdout .split('\n') .filter(Boolean) .sort(); if (packages.length > 0) { return { [`packages installed via ${pm.cmd}`]: packages.join(',') }; } logger_1.default.debug(`No packages found for ${pm.cmd}`); return { [pm.cmd]: '' }; } catch (error) { logger_1.default.error(`Error executing ${pm.cmd}:`, error.message); return { [pm.cmd]: '' }; } }))); return allPackages.reduce((acc, curr) => (Object.assign(Object.assign({}, acc), curr)), {}); } catch (error) { logger_1.default.error('Error getting installed packages:', error.message); return {}; } }); }