Skip to content

Commit b8e7481

Browse files
committed
Improve code
1 parent 0533d4b commit b8e7481

File tree

6 files changed

+39
-27
lines changed

6 files changed

+39
-27
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "python-dependencies-vscode",
33
"displayName": "Python Dependencies",
44
"description": "Utilities for managing Python dependencies",
5-
"version": "0.0.15",
5+
"version": "0.0.16",
66
"publisher": "patrick91",
77
"engines": {
88
"vscode": "^1.53.0"

src/core/get-installed-versions.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as execa from "execa";
2+
import { workspace } from "vscode";
3+
import { normalize } from "./normalize";
4+
5+
export const getInstalledVersions = async (path: string) => {
6+
let command = "pip list";
7+
8+
const { pythonPath } = workspace.getConfiguration("python");
9+
10+
if (pythonPath) {
11+
command = path + "/" + pythonPath.replace(/python$/, "") + command;
12+
}
13+
14+
const { stdout } = await execa.command(command, {
15+
cwd: path,
16+
});
17+
18+
const lines = stdout.split("\n");
19+
20+
const installedVersions: { [key: string]: string } = {};
21+
22+
lines.forEach((line) => {
23+
const [name, version] = line.split(/\s+/).map((x) => x.trim());
24+
25+
installedVersions[normalize(name)] = version;
26+
});
27+
28+
return installedVersions;
29+
};

src/core/listener.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { TextEditor, window, TextEditorDecorationType } from "vscode";
22
import { Dependency } from "../types";
33
import { findDependencies } from "../utils/parsing/toml";
4-
import { getInstalledVersions } from "./poetry";
54
import { getInfo } from "./api";
65
import { getDecoration } from "./decorations";
76
import { dirname } from "path";
87
import { findDependenciesFromRequirementsTxt } from "../utils/parsing/requirements-txt";
8+
import { getInstalledVersions } from "./get-installed-versions";
99

1010
let decorationType: TextEditorDecorationType | null = null;
1111

@@ -17,7 +17,7 @@ const updateVersions = (
1717
deps.map(async (dep) => {
1818
const info = await getInfo(dep.name);
1919

20-
// dep.version.installed = installedVersions[dep.name];
20+
dep.version.installed = installedVersions[dep.name];
2121
dep.version.latest = info.version;
2222
dep.summary = info.summary;
2323
}),
@@ -29,10 +29,9 @@ const decorateEditor = async (deps: Dependency[], editor: TextEditor) => {
2929
decorationType.dispose();
3030
}
3131

32-
const installedVersions = {}
33-
// await getInstalledVersions(
34-
// dirname(editor.document.fileName),
35-
// );
32+
const installedVersions = await getInstalledVersions(
33+
dirname(editor.document.fileName),
34+
);
3635

3736
await updateVersions(deps, installedVersions);
3837

src/core/normalize.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const normalize = (dependencyName: string) =>
2+
dependencyName.toLowerCase();

src/core/poetry.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/utils/parsing/toml.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { normalize } from "../../core/normalize";
12
import { Dependency } from "../../types";
23
import { splitFirst } from "../strings";
34

@@ -26,7 +27,7 @@ const parseDependency = (line: string, lineNumber: number) => {
2627
: JSON.parse(info);
2728

2829
return {
29-
name,
30+
name: normalize(name),
3031
version: {
3132
toml: version,
3233
},

0 commit comments

Comments
 (0)