Skip to content

Commit 5038dc9

Browse files
committed
support project repos with only base readme
1 parent c3cdb14 commit 5038dc9

File tree

4 files changed

+142
-78
lines changed

4 files changed

+142
-78
lines changed

dist/main.js

Lines changed: 63 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,21 +1600,38 @@ function get_content_and_filename(
16001600
});
16011601
}
16021602

1603+
async function maybe_read_dir(
1604+
docs_dir
1605+
) {
1606+
try {
1607+
return await fs$1.promises.readdir(path__namespace.join(docs_dir, "docs"));
1608+
} catch (e) {
1609+
return false;
1610+
}
1611+
}
1612+
16031613
async function get_base_documentation(
16041614
docs_path,
16051615
working_directory = process.cwd()
16061616
) {
16071617
const docs_dir = path__namespace.join(working_directory, docs_path);
1618+
let api_content;
1619+
let api = await maybe_read_dir(docs_dir);
1620+
if (api) {
1621+
api_content = await Promise.all(
1622+
api
1623+
.filter((f) => path__namespace.extname(f) === ".md" && !f.startsWith("xx"))
1624+
.map((f) => get_content_and_filename(path__namespace.join(docs_dir, "docs"), f))
1625+
);
1626+
} else {
1627+
const content = await get_pkg_and_readme(process.cwd(), "");
1628+
if (content) api_content = [content];
1629+
}
16081630

1609-
const api = await fs$1.promises.readdir(path__namespace.join(docs_dir, "docs"));
1610-
const api_content = await Promise.all(
1611-
api
1612-
.filter((f) => path__namespace.extname(f) === ".md" || f.startsWith("xx"))
1613-
.map((f) => get_content_and_filename(path__namespace.join(docs_dir, "docs"), f))
1614-
);
1631+
if (!api_content) return false;
16151632

16161633
return {
1617-
api: api_content,
1634+
docs: api_content,
16181635
};
16191636
}
16201637

@@ -1649,12 +1666,17 @@ async function get_package_documentation(
16491666
working_directory = process.cwd(),
16501667
opts = { ignore: [] }
16511668
) {
1669+
const _ignore = opts.ignore.concat(
1670+
opts.ignore.map((pkg) => `@sveltejs/${pkg}`)
1671+
);
16521672
const pkg_dir = path__namespace.join(working_directory, pkg_path);
1653-
const packages = await fs$1.promises.readdir(pkg_dir);
1673+
const packages = await maybe_read_dir(pkg_dir);
1674+
1675+
if (!packages) return false;
16541676

16551677
return (
16561678
await Promise.all(packages.map((f) => get_pkg_and_readme(pkg_dir, f)))
1657-
).filter((contents) => contents && !opts.ignore.includes(contents[0]))
1679+
).filter((contents) => contents && !_ignore.includes(contents[0]))
16581680

16591681

16601682
;
@@ -7960,7 +7982,7 @@ async function get_repo(
79607982
// we only care about the documentation folder and any package readmes + package.jsons
79617983
fs__default['default'].writeFileSync(
79627984
path__default['default'].join(process.cwd(), ".git/info/sparse-checkout"),
7963-
`/${docs_path}/\n/${pkg_path}/*/README.md\n/${pkg_path}/*/package.json`
7985+
`/${docs_path}/\n/${pkg_path}/*/README.md\n/${pkg_path}/*/package.json\n/README.md`
79647986
);
79657987

79667988
fs__default['default'].readdirSync;
@@ -8005,42 +8027,48 @@ async function run() {
80058027
core$1.setFailed("Failed to read documentation files.");
80068028
}
80078029

8008-
// format them
8009-
const formatted_pkg_docs
8030+
if (pkg_docs) {
8031+
const formatted_pkg_docs
80108032

80118033
= pkg_docs.map(([name, content]) => [
8012-
name,
8013-
format_api(name, increment_headings(content), "", name),
8014-
]);
8034+
name,
8035+
format_api(name, increment_headings(content), "", name),
8036+
]);
80158037

8016-
console.log(formatted_pkg_docs, null, 2);
8038+
console.log(formatted_pkg_docs, null, 2);
80178039

8018-
const formatted_base_docs = base_docs.api.map(([name, content]) =>
8019-
format_api(name, content, "docs")
8020-
);
8021-
console.log(JSON.stringify(formatted_base_docs, null, 2));
8040+
const pkgs = formatted_pkg_docs.reduce((acc, [name, _docs]) => {
8041+
const cf_doc = transform_cloudflare([_docs], {
8042+
project: name,
8043+
type: "docs",
8044+
keyby: "slug",
8045+
});
80228046

8023-
// transform to cf format (batch keys)
8047+
return acc.concat(cf_doc);
8048+
}, []);
80248049

8025-
const docs = transform_cloudflare(formatted_base_docs, {
8026-
project: target_repo,
8027-
type: "docs",
8028-
keyby: "slug",
8029-
});
8050+
console.log("\n");
8051+
console.log(pkgs);
8052+
}
8053+
// format them
80308054

8031-
const pkgs = formatted_pkg_docs.reduce((acc, [name, _docs]) => {
8032-
const cf_doc = transform_cloudflare([_docs], {
8033-
project: name,
8055+
if (base_docs) {
8056+
const formatted_base_docs = base_docs.docs.map(([name, content]) =>
8057+
format_api(name, content, "docs")
8058+
);
8059+
console.log(JSON.stringify(formatted_base_docs, null, 2));
8060+
8061+
// transform to cf format (batch keys)
8062+
8063+
const docs = transform_cloudflare(formatted_base_docs, {
8064+
project: target_repo,
80348065
type: "docs",
80358066
keyby: "slug",
80368067
});
80378068

8038-
return acc.concat(cf_doc);
8039-
}, []);
8040-
8041-
console.log(docs);
8042-
console.log("\n");
8043-
console.log(pkgs);
8069+
console.log("\n");
8070+
console.log(docs);
8071+
}
80448072

80458073
// write to cloudflare
80468074
}

src/fs/get_content.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const get_pkg_docs = suite("get_package_documentation");
1515
get_docs("gets the api documentation correctly", async () => {
1616
const content = await get_base_documentation("documentation", repo);
1717

18-
assert.equal(content.api, [
18+
assert.equal(content && content.docs, [
1919
["01-one.md", "file-one\n"],
2020
["02-two.md", "file-two\n"],
2121
["03-three.md", "file-three\n"],

src/fs/get_content.ts

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { promises as fs } from "fs";
22
import * as path from "path";
33

44
export interface BaseDocs {
5-
api: [string, string][];
5+
docs: [string, string][];
66
}
77

88
const fs_opts = {
@@ -25,21 +25,46 @@ function get_content_and_filename(
2525
});
2626
}
2727

28+
async function maybe_read_dir(
29+
docs_dir: string
30+
): Promise<Array<string> | false> {
31+
try {
32+
return await fs.readdir(path.join(docs_dir, "docs"));
33+
} catch (e) {
34+
return false;
35+
}
36+
}
37+
38+
async function maybe_read_file(docs_dir: string): Promise<string | false> {
39+
try {
40+
return (await fs.readFile(path.join(docs_dir, "docs"))).toString();
41+
} catch (e) {
42+
return false;
43+
}
44+
}
45+
2846
export async function get_base_documentation(
2947
docs_path: string,
3048
working_directory: string = process.cwd()
31-
): Promise<BaseDocs> {
49+
): Promise<BaseDocs | false> {
3250
const docs_dir = path.join(working_directory, docs_path);
51+
let api_content;
52+
let api = await maybe_read_dir(docs_dir);
53+
if (api) {
54+
api_content = await Promise.all(
55+
api
56+
.filter((f) => path.extname(f) === ".md" && !f.startsWith("xx"))
57+
.map((f) => get_content_and_filename(path.join(docs_dir, "docs"), f))
58+
);
59+
} else {
60+
const content = await get_pkg_and_readme(process.cwd(), "");
61+
if (content) api_content = [content];
62+
}
3363

34-
const api = await fs.readdir(path.join(docs_dir, "docs"));
35-
const api_content = await Promise.all(
36-
api
37-
.filter((f) => path.extname(f) === ".md" || f.startsWith("xx"))
38-
.map((f) => get_content_and_filename(path.join(docs_dir, "docs"), f))
39-
);
64+
if (!api_content) return false;
4065

4166
return {
42-
api: api_content,
67+
docs: api_content,
4368
};
4469
}
4570

@@ -73,13 +98,18 @@ export async function get_package_documentation(
7398
pkg_path: string,
7499
working_directory: string = process.cwd(),
75100
opts: PackageOptions = { ignore: [] }
76-
): Promise<[string, string][]> {
101+
): Promise<[string, string][] | false> {
102+
const _ignore = opts.ignore.concat(
103+
opts.ignore.map((pkg) => `@sveltejs/${pkg}`)
104+
);
77105
const pkg_dir = path.join(working_directory, pkg_path);
78-
const packages = await fs.readdir(pkg_dir);
106+
const packages = await maybe_read_dir(pkg_dir);
107+
108+
if (!packages) return false;
79109

80110
return (
81111
await Promise.all(packages.map((f) => get_pkg_and_readme(pkg_dir, f)))
82-
).filter((contents) => contents && !opts.ignore.includes(contents[0])) as [
112+
).filter((contents) => contents && !_ignore.includes(contents[0])) as [
83113
string,
84114
string
85115
][];

src/main.ts

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ async function get_repo(
4242
// we only care about the documentation folder and any package readmes + package.jsons
4343
fs.writeFileSync(
4444
path.join(process.cwd(), ".git/info/sparse-checkout"),
45-
`/${docs_path}/\n/${pkg_path}/*/README.md\n/${pkg_path}/*/package.json`
45+
`/${docs_path}/\n/${pkg_path}/*/README.md\n/${pkg_path}/*/package.json\n/README.md`
4646
);
4747

4848
fs.readdirSync;
@@ -74,8 +74,8 @@ async function run() {
7474
}
7575

7676
// read docs in
77-
let base_docs: BaseDocs;
78-
let pkg_docs: [string, string][];
77+
let base_docs: BaseDocs | false;
78+
let pkg_docs: [string, string][] | false;
7979

8080
try {
8181
[base_docs, pkg_docs] = await Promise.all([
@@ -87,42 +87,48 @@ async function run() {
8787
core.setFailed("Failed to read documentation files.");
8888
}
8989

90-
// format them
91-
const formatted_pkg_docs: Array<
92-
[string, FormattedFile]
93-
> = pkg_docs.map(([name, content]) => [
94-
name,
95-
format_api(name, increment_headings(content), "", name),
96-
]);
90+
if (pkg_docs) {
91+
const formatted_pkg_docs: Array<
92+
[string, FormattedFile]
93+
> = pkg_docs.map(([name, content]) => [
94+
name,
95+
format_api(name, increment_headings(content), "", name),
96+
]);
9797

98-
console.log(formatted_pkg_docs, null, 2);
98+
console.log(formatted_pkg_docs, null, 2);
9999

100-
const formatted_base_docs = base_docs.api.map(([name, content]) =>
101-
format_api(name, content, "docs")
102-
);
103-
console.log(JSON.stringify(formatted_base_docs, null, 2));
100+
const pkgs = formatted_pkg_docs.reduce((acc, [name, _docs]) => {
101+
const cf_doc = transform_cloudflare([_docs], {
102+
project: name,
103+
type: "docs",
104+
keyby: "slug",
105+
});
104106

105-
// transform to cf format (batch keys)
107+
return acc.concat(cf_doc);
108+
}, []);
106109

107-
const docs = transform_cloudflare(formatted_base_docs, {
108-
project: target_repo,
109-
type: "docs",
110-
keyby: "slug",
111-
});
110+
console.log("\n");
111+
console.log(pkgs);
112+
}
113+
// format them
112114

113-
const pkgs = formatted_pkg_docs.reduce((acc, [name, _docs]) => {
114-
const cf_doc = transform_cloudflare([_docs], {
115-
project: name,
115+
if (base_docs) {
116+
const formatted_base_docs = base_docs.docs.map(([name, content]) =>
117+
format_api(name, content, "docs")
118+
);
119+
console.log(JSON.stringify(formatted_base_docs, null, 2));
120+
121+
// transform to cf format (batch keys)
122+
123+
const docs = transform_cloudflare(formatted_base_docs, {
124+
project: target_repo,
116125
type: "docs",
117126
keyby: "slug",
118127
});
119128

120-
return acc.concat(cf_doc);
121-
}, []);
122-
123-
console.log(docs);
124-
console.log("\n");
125-
console.log(pkgs);
129+
console.log("\n");
130+
console.log(docs);
131+
}
126132

127133
// write to cloudflare
128134
}

0 commit comments

Comments
 (0)