Skip to content

Commit

Permalink
add a function to list pip package directories
Browse files Browse the repository at this point in the history
  • Loading branch information
threeal committed Feb 2, 2023
1 parent 6f411b2 commit 52de9a4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
19 changes: 19 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,25 @@ class PackageInfo {
}
return absFiles;
}
directories() {
const dirs = [];
for (const file of this.files) {
const strs = file.split(path.sep);
if (strs.length < 1)
continue;
const dir = strs[0];
if (dir in dirs)
continue;
dirs.push(dir);
}
const absDirs = [];
for (const dir of dirs) {
const absDir = path.join(this.location, dir);
if (fs.existsSync(absDir))
absDirs.push(absDir);
}
return absDirs;
}
}
exports.PackageInfo = PackageInfo;
async function showPackageInfo(packageName) {
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/deps/pip/info.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ describe("pip module", () => {
expect(pkgInfo.name).toBe("pip");
expect(pkgInfo.version).toMatch(/^(\d+\.)?(\d+\.)?(\*|\d+)$/);
expectPathExist(pkgInfo.location);
for (const file of pkgInfo.absoluteFiles()) {
expectPathExist(file);
const dirs = pkgInfo.directories();
expect(dirs.length).toBeGreaterThan(0);
for (const dir of dirs) {
expectPathExist(dir);
}
});
test("show invalid package info return null", async () => {
Expand Down
17 changes: 17 additions & 0 deletions src/deps/pip/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ export class PackageInfo {
}
return absFiles;
}

directories(): string[] {
const dirs: string[] = [];
for (const file of this.files) {
const strs = file.split(path.sep);
if (strs.length < 1) continue;
const dir = strs[0];
if (dir in dirs) continue;
dirs.push(dir);
}
const absDirs: string[] = [];
for (const dir of dirs) {
const absDir = path.join(this.location, dir);
if (fs.existsSync(absDir)) absDirs.push(absDir);
}
return absDirs;
}
}

export async function showPackageInfo(
Expand Down

0 comments on commit 52de9a4

Please sign in to comment.