Refactor checkDevChatDependency function in commandsBase.ts

- Imported path module for handling file paths.
- Added pipxBinPath variable to store the pipx environment path.
- Changed error logging level from error to warn.
- Updated the way DevChat command is run and its path is updated.
This commit is contained in:
bobo.yang 2023-07-06 07:42:44 +08:00
parent 1019658bf2
commit 8f846e2d62

View File

@ -3,6 +3,7 @@
import { UiUtilWrapper } from "../util/uiUtil"; import { UiUtilWrapper } from "../util/uiUtil";
import { runCommand } from "../util/commonUtil"; import { runCommand } from "../util/commonUtil";
import { logger } from "../util/logger"; import { logger } from "../util/logger";
import path from "path";
let pipxPathStatus = ''; let pipxPathStatus = '';
let devchatStatus = ''; let devchatStatus = '';
@ -23,8 +24,10 @@ function locateCommand(command): string | undefined {
} }
export function checkDevChatDependency(pythonCommand: string): boolean { export function checkDevChatDependency(pythonCommand: string): boolean {
let pipxBinPath: string | undefined = undefined;
try { try {
const binPath = getPipxEnvironmentPath(pythonCommand); const binPath = getPipxEnvironmentPath(pythonCommand);
pipxBinPath = binPath;
if (binPath) { if (binPath) {
updateEnvironmentPath(binPath); updateEnvironmentPath(binPath);
@ -37,31 +40,32 @@ export function checkDevChatDependency(pythonCommand: string): boolean {
} else { } else {
const error_status = `Failed to obtain the pipx environment path.`; const error_status = `Failed to obtain the pipx environment path.`;
if (pipxPathStatus !== error_status) { if (pipxPathStatus !== error_status) {
logger.channel()?.error(error_status); logger.channel()?.warn(error_status);
logger.channel()?.show(); logger.channel()?.show();
pipxPathStatus = error_status; pipxPathStatus = error_status;
} }
return false;
} }
} catch (error) { } catch (error) {
// DevChat dependency check failed // DevChat dependency check failed
// log out detail error message // log out detail error message
const error_status = `Failed to check DevChat dependency due to error: ${error}`; const error_status = `Failed to check DevChat dependency due to error: ${error}`;
if (pipxPathStatus !== error_status) { if (pipxPathStatus !== error_status) {
logger.channel()?.error(error_status); logger.channel()?.warn(error_status);
logger.channel()?.show(); logger.channel()?.show();
pipxPathStatus = error_status; pipxPathStatus = error_status;
} }
return false;
} }
try { try {
// Check if DevChat is installed // Check if DevChat is installed
runCommand('devchat --help'); const pipxDevChat = path.join(pipxBinPath!, 'devchat');
runCommand(`${pipxDevChat} --help`);
const devchatCommand = locateCommand('devchat');
if (devchatCommand) {
UiUtilWrapper.updateConfiguration('DevChat', 'DevChatPath', devchatCommand);
}
UiUtilWrapper.updateConfiguration('DevChat', 'DevChatPath', pipxDevChat);
const error_status = `DevChat has installed.`; const error_status = `DevChat has installed.`;
if (devchatStatus !== error_status) { if (devchatStatus !== error_status) {
logger.channel()?.info(error_status); logger.channel()?.info(error_status);
@ -72,7 +76,7 @@ export function checkDevChatDependency(pythonCommand: string): boolean {
} catch(error) { } catch(error) {
const error_status = `Failed to check DevChat dependency due to error: ${error}`; const error_status = `Failed to check DevChat dependency due to error: ${error}`;
if (devchatStatus !== error_status) { if (devchatStatus !== error_status) {
logger.channel()?.error(error_status); logger.channel()?.warn(error_status);
logger.channel()?.show(); logger.channel()?.show();
devchatStatus = error_status; devchatStatus = error_status;
} }