Skip to content
Snippets Groups Projects
Commit d8fe65a2 authored by Marcus Schiesser's avatar Marcus Schiesser
Browse files

refactor: improved var naming

parent ba1cb996
No related branches found
No related tags found
No related merge requests found
...@@ -5,17 +5,17 @@ import { parse, stringify } from "smol-toml"; ...@@ -5,17 +5,17 @@ import { parse, stringify } from "smol-toml";
import { copy } from "../helpers/copy"; import { copy } from "../helpers/copy";
import { InstallTemplateArgs, TemplateVectorDB } from "./types"; import { InstallTemplateArgs, TemplateVectorDB } from "./types";
interface IDependencyItem { interface Dependency {
name: string; name: string;
version: string; version: string;
} }
const getPythonAddOnDependencies = (vectorDb?: TemplateVectorDB) => { const getAdditionalDependencies = (vectorDb?: TemplateVectorDB) => {
const addOnDependencies: IDependencyItem[] = []; const dependencies: Dependency[] = [];
switch (vectorDb) { switch (vectorDb) {
case "mongo": { case "mongo": {
addOnDependencies.push({ dependencies.push({
name: "pymongo", name: "pymongo",
version: "^4.6.1", version: "^4.6.1",
}); });
...@@ -23,25 +23,26 @@ const getPythonAddOnDependencies = (vectorDb?: TemplateVectorDB) => { ...@@ -23,25 +23,26 @@ const getPythonAddOnDependencies = (vectorDb?: TemplateVectorDB) => {
} }
} }
return addOnDependencies; return dependencies;
}; };
const preparePythonDependencies = async (
root: string, const addDependencies = async (
addOnDependencies: IDependencyItem[], projectDir: string,
dependencies: Dependency[],
) => { ) => {
if (addOnDependencies.length === 0) return; if (dependencies.length === 0) return;
const FILENAME = "pyproject.toml"; const FILENAME = "pyproject.toml";
try { try {
// Parse toml file // Parse toml file
const file = path.join(root, FILENAME); const file = path.join(projectDir, FILENAME);
const fileContent = await fs.readFile(file, "utf8"); const fileContent = await fs.readFile(file, "utf8");
const fileParsed = parse(fileContent); const fileParsed = parse(fileContent);
// Modify toml dependencies // Modify toml dependencies
const tool = fileParsed.tool as any; const tool = fileParsed.tool as any;
const dependencies = tool.poetry.dependencies as any; const dependencies = tool.poetry.dependencies as any;
for (const dependency of addOnDependencies) { for (const dependency of dependencies) {
dependencies[dependency.name] = dependency.version; dependencies[dependency.name] = dependency.version;
} }
...@@ -49,16 +50,19 @@ const preparePythonDependencies = async ( ...@@ -49,16 +50,19 @@ const preparePythonDependencies = async (
const newFileContent = stringify(fileParsed); const newFileContent = stringify(fileParsed);
await fs.writeFile(file, newFileContent); await fs.writeFile(file, newFileContent);
const dependenciesString = addOnDependencies.map((d) => d.name).join(", "); const dependenciesString = dependencies
.map((d: Dependency) => d.name)
.join(", ");
console.log(`\nAdded ${dependenciesString} to ${cyan(FILENAME)}\n`); console.log(`\nAdded ${dependenciesString} to ${cyan(FILENAME)}\n`);
} catch (error) { } catch (error) {
console.log( console.log(
`Error when preparing ${FILENAME} file for Python template\n`, `Error while updating dependencies for Poetry project file ${FILENAME}\n`,
error, error,
); );
console.log(error); console.log(error);
} }
}; };
export const installPythonTemplate = async ({ export const installPythonTemplate = async ({
root, root,
template, template,
...@@ -105,8 +109,8 @@ export const installPythonTemplate = async ({ ...@@ -105,8 +109,8 @@ export const installPythonTemplate = async ({
}); });
} }
const addOnDependencies = getPythonAddOnDependencies(vectorDb); const addOnDependencies = getAdditionalDependencies(vectorDb);
await preparePythonDependencies(root, addOnDependencies); await addDependencies(root, addOnDependencies);
console.log( console.log(
"\nPython project, dependencies won't be installed automatically.\n", "\nPython project, dependencies won't be installed automatically.\n",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment