From 80d1ac713a8e5eaa50d053472cc87abcc3d9796c Mon Sep 17 00:00:00 2001
From: thucpn <thucsh2@gmail.com>
Date: Fri, 29 Dec 2023 13:47:41 +0700
Subject: [PATCH] feat: prepare python dependencies

---
 package.json                                  |  1 +
 templates/index.ts                            | 60 +++++++++++++++++++
 templates/types/simple/fastapi/pyproject.toml |  1 -
 .../types/streaming/fastapi/pyproject.toml    |  1 -
 4 files changed, 61 insertions(+), 2 deletions(-)

diff --git a/package.json b/package.json
index efcde345..4666b500 100644
--- a/package.json
+++ b/package.json
@@ -48,6 +48,7 @@
     "picocolors": "1.0.0",
     "prompts": "2.1.0",
     "rimraf": "^5.0.5",
+    "smol-toml": "^1.1.3",
     "tar": "6.1.15",
     "terminal-link": "^3.0.0",
     "update-check": "1.5.4",
diff --git a/templates/index.ts b/templates/index.ts
index 1165c946..4508b24b 100644
--- a/templates/index.ts
+++ b/templates/index.ts
@@ -1,3 +1,4 @@
+import { parse, stringify } from "smol-toml";
 import { copy } from "../helpers/copy";
 import { callPackageManager } from "../helpers/install";
 
@@ -313,6 +314,62 @@ const installTSTemplate = async ({
   await callPackageManager(packageManager, isOnline);
 };
 
+interface IDependencyItem {
+  name: string;
+  version: string;
+}
+
+const getPythonAddOnDependencies = (vectorDb?: TemplateVectorDB) => {
+  const addOnDependencies: IDependencyItem[] = [];
+
+  switch (vectorDb) {
+    case "mongo": {
+      addOnDependencies.push({
+        name: "pymongo",
+        version: "^4.6.1",
+      });
+      break;
+    }
+  }
+
+  return addOnDependencies;
+};
+
+const preparePythonDependencies = async (
+  root: string,
+  addOnDependencies: IDependencyItem[],
+) => {
+  if (addOnDependencies.length === 0) return;
+
+  const FILENAME = "pyproject.toml";
+  try {
+    // Parse toml file
+    const file = path.join(root, FILENAME);
+    const fileContent = await fs.readFile(file, "utf8");
+    const fileParsed = parse(fileContent);
+
+    // Modify toml dependencies
+    const tool = fileParsed.tool as any;
+    const dependencies = tool.poetry.dependencies as any;
+    for (const dependency of addOnDependencies) {
+      dependencies[dependency.name] = dependency.version;
+    }
+
+    // Write toml file
+    const newFileContent = stringify(fileParsed);
+    await fs.writeFile(file, newFileContent);
+
+    const dependenciesString = addOnDependencies.map((d) => d.name).join(", ");
+    console.log(`\nAdded ${dependenciesString} to ${cyan(FILENAME)}\n`);
+  } catch (error) {
+    console.log(
+      `Error when preparing ${FILENAME} file for Python template\n`,
+      error,
+    );
+    console.log(error);
+  }
+};
+
 const installPythonTemplate = async ({
   root,
   template,
@@ -359,6 +416,9 @@ const installPythonTemplate = async ({
     });
   }
 
+  const addOnDependencies = getPythonAddOnDependencies(vectorDb);
+  await preparePythonDependencies(root, addOnDependencies);
+
   console.log(
     "\nPython project, dependencies won't be installed automatically.\n",
   );
diff --git a/templates/types/simple/fastapi/pyproject.toml b/templates/types/simple/fastapi/pyproject.toml
index a563ffbf..f9bb9605 100644
--- a/templates/types/simple/fastapi/pyproject.toml
+++ b/templates/types/simple/fastapi/pyproject.toml
@@ -12,7 +12,6 @@ uvicorn = { extras = ["standard"], version = "^0.23.2" }
 llama-index = "^0.9.19"
 pypdf = "^3.17.0"
 python-dotenv = "^1.0.0"
-pymongo = "^4.6.1"
 
 
 [build-system]
diff --git a/templates/types/streaming/fastapi/pyproject.toml b/templates/types/streaming/fastapi/pyproject.toml
index a563ffbf..f9bb9605 100644
--- a/templates/types/streaming/fastapi/pyproject.toml
+++ b/templates/types/streaming/fastapi/pyproject.toml
@@ -12,7 +12,6 @@ uvicorn = { extras = ["standard"], version = "^0.23.2" }
 llama-index = "^0.9.19"
 pypdf = "^3.17.0"
 python-dotenv = "^1.0.0"
-pymongo = "^4.6.1"
 
 
 [build-system]
-- 
GitLab