From 00a4517d551ae9e560e4240208cd1db45b88544a Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 25 Jan 2026 21:42:00 +0800 Subject: [PATCH] handle remove venv --- python-ts/main.ts | 62 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/python-ts/main.ts b/python-ts/main.ts index 5e3a5fc..c56f560 100644 --- a/python-ts/main.ts +++ b/python-ts/main.ts @@ -6,6 +6,7 @@ import { existsPath, joinPath, log, + ProcessBar, readFileToString, resolveFilename, } from "https://global.hatter.ink/script/get/@16/deno-commons-mod.ts"; @@ -128,10 +129,11 @@ function handleHelp(_args: string[]) { help.push( `python.ts - Python version and virtual environment management tool -python.ts python - management python version [alias: py] -python.ts add-python - add python version [alias: py] -python.ts venv - management python virtual environment -python.ts add-venv - add python virtual environment`, +python.ts python - management python version [alias: py] +python.ts add-python - add python version [alias: py] +python.ts venv - management python virtual environment +python.ts add-venv - add python virtual environment +python.ts remove-venv - remove python virtual environment`, ); // source <(cat ~/.venv-python-3.13.5/bin/activate) // source <(python.ts venv test1) @@ -327,6 +329,54 @@ python.ts add-venv --version 3.10 --venv test-env`); await addVirtualEnv(flags.version, flags.venv); } +async function handleRemoveVenv(args: string[]) { + const flags = parseArgs(args, { + boolean: ["help"], + string: ["venv"], + }); + if (args.length === 0 || flags.help) { + console.log(`Help massage for remove-venv + +python.ts remove-venv --venv test-env`); + return; + } + if (!flags.venv) { + log.error("Venv is missing"); + return; + } + const pythonConfig = await loadPythonConfig(); + const pythonVenvProfile = pythonConfig.profiles && + pythonConfig.profiles[flags.venv]; + if (!pythonVenvProfile) { + throw `Python venv not exists: ${flags.venv}`; + } + + console.log( + `Pending remove virtual environment [PLEASE CONFIRM]: `, + pythonVenvProfile, + ); + + const yesOrNo = prompt("Please confirm (yes/no):"); + if (yesOrNo === "yes" || yesOrNo === "y") { + // DO CONFIRM YES + // TODO + const path = pythonVenvProfile.path; + log.info(`Remove path: ${path}`); + + await new ProcessBar(`Removing path ${path}`).call(async () => { + await Deno.remove(path, { recursive: true }); + }); + + delete pythonConfig.profiles[flags.venv]; + await savePythonConfig(pythonConfig); + log.success(`Remove virtual environment: ${flags.venv}`); + } else { + console.log( + `Your input '${yesOrNo}', skip remove the virtual environment`, + ); + } +} + async function main() { const args = Deno.args; @@ -356,6 +406,10 @@ async function main() { case "add-venv": await handleAddVenv(remainingArgs); break; + case "rm-venv": + case "remove-venv": + await handleRemoveVenv(remainingArgs); + break; default: log.error(`Unknown subcommand: ${subcommand}`); break;