Skip to content

Commit a1af2f9

Browse files
committed
add get package docs
1 parent c5bd6a6 commit a1af2f9

File tree

8 files changed

+58
-6
lines changed

8 files changed

+58
-6
lines changed

src/fs/fixtures/repo-1/packages/no-readme/README.md

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "no-readme"
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
random-extra-files
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "random-extra-files"
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
standard-package
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "standard-package"
3+
}

src/fs/get_content.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { suite } from "uvu";
22
import * as assert from "uvu/assert";
33

4-
import { get_base_documentation } from "./get_content";
4+
import {
5+
get_base_documentation,
6+
get_package_docuementation,
7+
} from "./get_content";
58
import * as path from "path";
69

710
const repo = path.join(__dirname, "fixtures", "repo-1");
@@ -20,4 +23,17 @@ get_docs("gets the api documentation correctly", async () => {
2023
]);
2124
});
2225

26+
get_pkg_docs(
27+
"get documentation for packages ignoring invaliud packages",
28+
async () => {
29+
const content = await get_package_docuementation("packages", repo);
30+
31+
assert.equal(content, [
32+
["random-extra-files", "random-extra-files\n"],
33+
["standard-package", "standard-package\n"],
34+
]);
35+
}
36+
);
37+
2338
get_docs.run();
39+
get_pkg_docs.run();

src/fs/get_content.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ interface BaseDocs {
55
api: [string, string][];
66
}
77

8+
const fs_opts = {
9+
encoding: "utf-8",
10+
} as const;
11+
812
function get_content_and_filename(
913
base: string,
1014
filename: string
1115
): Promise<[string, string]> {
1216
return new Promise(async (rs, rj) => {
1317
try {
14-
const content = await fs.readFile(path.join(base, filename), {
15-
encoding: "utf-8",
16-
});
18+
const content = await fs.readFile(path.join(base, filename), fs_opts);
1719
rs([filename, content]);
1820
} catch (e) {
1921
rj(e);
@@ -39,7 +41,30 @@ export async function get_base_documentation(
3941
};
4042
}
4143

42-
export function get_package_docuementation(
44+
function get_pkg_and_readme(
45+
base: string,
46+
pkg_dir: string
47+
): Promise<[string, string] | false> {
48+
return new Promise(async (rs, rj) => {
49+
try {
50+
const [pkg, docs] = await Promise.all([
51+
fs.readFile(path.join(base, pkg_dir, "package.json"), fs_opts),
52+
fs.readFile(path.join(base, pkg_dir, "README.md"), fs_opts),
53+
]);
54+
rs([JSON.parse(pkg).name, docs]);
55+
} catch (e) {
56+
rs(false);
57+
}
58+
});
59+
}
60+
61+
export async function get_package_docuementation(
4362
pkg_path: string,
4463
working_directory: string = process.cwd()
45-
) {}
64+
) {
65+
const pkg_dir = path.join(working_directory, pkg_path);
66+
const packages = await fs.readdir(pkg_dir);
67+
return (
68+
await Promise.all(packages.map((f) => get_pkg_and_readme(pkg_dir, f)))
69+
).filter(Boolean);
70+
}

0 commit comments

Comments
 (0)