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+$/, '');