-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocs-layout.mjs
More file actions
119 lines (99 loc) · 3.32 KB
/
Copy pathdocs-layout.mjs
File metadata and controls
119 lines (99 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import fs from "node:fs/promises";
import path from "node:path";
export const docsSubsetEntries = [
{kind: "root", relativePath: "README.md"},
{kind: "oss", relativePath: "introduction.md"},
{kind: "oss", relativePath: "getting-started/quick-start.md"},
{kind: "oss", relativePath: "getting-started/tutorial.md"},
{kind: "oss", relativePath: "getting-started/examples-and-references.md"},
{kind: "oss", relativePath: "getting-started/installation-into-an-existing-rails-app.md"},
{kind: "oss", relativePath: "core-concepts/how-react-on-rails-works.md"},
{kind: "oss", relativePath: "core-concepts/react-server-rendering.md"},
{kind: "oss", relativePath: "api-reference/view-helpers-api.md"},
{kind: "oss", relativePath: "building-features/react-and-redux.md"},
{kind: "oss", relativePath: "deployment/README.md"},
{kind: "oss", relativePath: "upgrading/upgrading-react-on-rails.md"},
{kind: "pro", relativePath: "react-on-rails-pro.md"},
{kind: "pro", relativePath: "home-pro.md"},
{kind: "pro", relativePath: "node-renderer/basics.md"},
{kind: "pro", relativePath: "react-server-components/tutorial.md"},
];
export async function exists(targetPath) {
try {
await fs.access(targetPath);
return true;
} catch {
return false;
}
}
async function hasSplitDocsMarkers(splitRoot) {
const splitMarkers = [
path.join(splitRoot, "introduction.md"),
path.join(splitRoot, "getting-started"),
path.join(splitRoot, "core-concepts"),
];
for (const markerPath of splitMarkers) {
if (await exists(markerPath)) {
return true;
}
}
return false;
}
function assertValidLayout(layout) {
if (layout !== "split" && layout !== "consolidated") {
throw new Error(`Unsupported docs layout: ${layout}`);
}
}
export async function detectDocsLayout(docsRoot) {
const splitRoot = path.join(docsRoot, "oss");
const readmePath = path.join(docsRoot, "README.md");
if ((await exists(splitRoot)) && (await hasSplitDocsMarkers(splitRoot))) {
return "split";
}
if (await exists(readmePath)) {
return "consolidated";
}
throw new Error(`Unable to detect docs layout in ${docsRoot}`);
}
export function subsetPathsForLayout(layout) {
assertValidLayout(layout);
return docsSubsetEntries.map((entry) => {
if (entry.kind === "root") {
return entry.relativePath;
}
if (entry.kind === "pro") {
return path.posix.join("pro", entry.relativePath);
}
if (layout === "split") {
return path.posix.join("oss", entry.relativePath);
}
return entry.relativePath;
});
}
export function docsLayoutPaths(docsRoot, layout) {
assertValidLayout(layout);
if (layout === "split") {
return {
layout,
contentRoot: path.join(docsRoot, "oss"),
proDocsRoot: path.join(docsRoot, "pro"),
imagesRoot: path.join(docsRoot, "images"),
assetsRoot: path.join(docsRoot, "assets"),
readmePath: path.join(docsRoot, "README.md"),
};
}
return {
layout,
contentRoot: docsRoot,
proDocsRoot: path.join(docsRoot, "pro"),
imagesRoot: path.join(docsRoot, "images"),
assetsRoot: path.join(docsRoot, "assets"),
readmePath: path.join(docsRoot, "README.md"),
};
}
export function excludeNamesForRootCopy(layout) {
if (layout === "consolidated") {
return new Set(["pro", "images", "assets"]);
}
return new Set();
}