From f94582b68d783f4b01d2fe243a3b24ae5c48ae07 Mon Sep 17 00:00:00 2001 From: whyour Date: Wed, 21 May 2025 01:25:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9F=A5=E8=AF=A2=20python?= =?UTF-8?q?=20=E4=BE=9D=E8=B5=96=E5=AD=98=E5=9C=A8=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/config/util.ts | 14 ++++++++++++++ back/data/dependence.ts | 6 ------ back/services/dependence.ts | 12 +++--------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/back/config/util.ts b/back/config/util.ts index da73d0e2..96070009 100644 --- a/back/config/util.ts +++ b/back/config/util.ts @@ -514,6 +514,20 @@ export async function setSystemTimezone(timezone: string): Promise { } } +export function getGetCommand(type: DependenceTypes, name: string): string { + const baseCommands = { + [DependenceTypes.nodejs]: `pnpm ls -g | grep "${name}" | head -1`, + [DependenceTypes.python3]: `python3 -c "import importlib;pkg=importlib.import_module('${name}');print(getattr(pkg, '__version__', 'version not found'))"`, + [DependenceTypes.linux]: `apk info -es ${name}`, + }; + + let command = baseCommands[type]; + + return type === DependenceTypes.python3 + ? command + : `${command} ${name.trim()}`; +} + export function getInstallCommand(type: DependenceTypes, name: string): string { const baseCommands = { [DependenceTypes.nodejs]: 'pnpm add -g', diff --git a/back/data/dependence.ts b/back/data/dependence.ts index 49dfe167..601f461c 100644 --- a/back/data/dependence.ts +++ b/back/data/dependence.ts @@ -41,12 +41,6 @@ export enum DependenceTypes { 'linux', } -export enum GetDependenceCommandTypes { - 'pnpm ls -g ', - 'pip3 show --disable-pip-version-check', - 'apk info -es', -} - export enum versionDependenceCommandTypes { '@', '==', diff --git a/back/services/dependence.ts b/back/services/dependence.ts index 09434288..d93f56d1 100644 --- a/back/services/dependence.ts +++ b/back/services/dependence.ts @@ -6,7 +6,6 @@ import { DependenceStatus, DependenceTypes, DependenceModel, - GetDependenceCommandTypes, versionDependenceCommandTypes, } from '../data/dependence'; import { spawn } from 'cross-spawn'; @@ -19,6 +18,7 @@ import { promiseExecSuccess, getInstallCommand, getUninstallCommand, + getGetCommand, } from '../config/util'; import dayjs from 'dayjs'; import taskLimit from '../shared/pLimit'; @@ -252,7 +252,7 @@ export default class DependenceService { // 判断是否已经安装过依赖 if (isInstall && !force) { - const getCommandPrefix = GetDependenceCommandTypes[dependency.type]; + const getCommand = getGetCommand(dependency.type, depName); const depVersionStr = versionDependenceCommandTypes[dependency.type]; let depVersion = ''; if (depName.includes(depVersionStr)) { @@ -269,13 +269,7 @@ export default class DependenceService { const isLinuxDependence = dependency.type === DependenceTypes.linux; const isPythonDependence = dependency.type === DependenceTypes.python3; - const depInfo = ( - await promiseExecSuccess( - isNodeDependence - ? `${getCommandPrefix} | grep "${depName}" | head -1` - : `${getCommandPrefix} ${depName}`, - ) - ) + const depInfo = (await promiseExecSuccess(getCommand)) .replace(/\s{2,}/, ' ') .replace(/\s+$/, '');