doccupine 0.0.143 → 0.0.145
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/cli.d.ts +6 -0
- package/dist/cli.js +130 -22
- package/dist/generator/api-reference-generator.d.ts +22 -0
- package/dist/generator/api-reference-generator.js +96 -0
- package/dist/generator/app-scaffolder.d.ts +18 -0
- package/dist/generator/app-scaffolder.js +49 -0
- package/dist/generator/generated-route-manager.d.ts +18 -0
- package/dist/generator/generated-route-manager.js +114 -0
- package/dist/generator/page-catalog.d.ts +21 -0
- package/dist/generator/page-catalog.js +75 -0
- package/dist/generator/page-renderer.d.ts +31 -0
- package/dist/generator/page-renderer.js +248 -0
- package/dist/generator/project-config-repository.d.ts +33 -0
- package/dist/generator/project-config-repository.js +191 -0
- package/dist/generator/public-asset-manager.d.ts +20 -0
- package/dist/generator/public-asset-manager.js +225 -0
- package/dist/generator/section-resolver.d.ts +13 -0
- package/dist/generator/section-resolver.js +150 -0
- package/dist/generator/secure-source-fs.d.ts +31 -0
- package/dist/generator/secure-source-fs.js +470 -0
- package/dist/generator/site-artifacts.d.ts +23 -0
- package/dist/generator/site-artifacts.js +156 -0
- package/dist/generator/watch-coordinator.d.ts +58 -0
- package/dist/generator/watch-coordinator.js +456 -0
- package/dist/lib/config-manager.d.ts +1 -0
- package/dist/lib/config-manager.js +194 -12
- package/dist/lib/generated-artifacts.d.ts +11 -2
- package/dist/lib/generated-artifacts.js +52 -7
- package/dist/lib/layout.d.ts +1 -17
- package/dist/lib/layout.js +5 -4
- package/dist/lib/openapi.d.ts +4 -0
- package/dist/lib/openapi.js +36 -4
- package/dist/lib/project-config.d.ts +3 -0
- package/dist/lib/project-config.js +110 -0
- package/dist/lib/types.d.ts +14 -1
- package/dist/mdx-to-nextjs-generator.d.ts +50 -34
- package/dist/mdx-to-nextjs-generator.js +760 -1740
- package/dist/templates/app/api/rag/route.d.ts +1 -1
- package/dist/templates/app/api/rag/route.js +2 -1
- package/dist/templates/components/Chat.d.ts +1 -1
- package/dist/templates/components/Chat.js +2 -1
- package/dist/templates/components/layout/Accordion.d.ts +1 -1
- package/dist/templates/components/layout/Accordion.js +2 -3
- package/dist/templates/mdx/api-playground.mdx.d.ts +1 -1
- package/dist/templates/mdx/api-playground.mdx.js +11 -2
- package/dist/templates/mdx/commands.mdx.d.ts +1 -1
- package/dist/templates/mdx/commands.mdx.js +26 -1
- package/dist/templates/mdx/media-and-assets.mdx.d.ts +1 -1
- package/dist/templates/mdx/media-and-assets.mdx.js +5 -0
- package/dist/templates/mdx/model-context-protocol.mdx.d.ts +1 -1
- package/dist/templates/mdx/model-context-protocol.mdx.js +2 -1
- package/dist/templates/mdx/platform/api-playground.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/api-playground.mdx.js +2 -2
- package/dist/templates/mdx/platform/publishing.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/publishing.mdx.js +3 -3
- package/dist/templates/mdx/space.mdx.d.ts +1 -1
- package/dist/templates/mdx/space.mdx.js +1 -5
- package/dist/templates/mdx/what-is-doccupine.mdx.d.ts +1 -1
- package/dist/templates/mdx/what-is-doccupine.mdx.js +8 -3
- package/dist/templates/next.config.js +2 -2
- package/dist/templates/package.js +3 -3
- package/dist/templates/proxy.js +1 -1
- package/dist/templates/services/mcp/server.d.ts +1 -1
- package/dist/templates/services/mcp/server.js +2 -12
- package/package.json +3 -3
|
@@ -0,0 +1,470 @@
|
|
|
1
|
+
import fs from "fs-extra";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { constants } from "node:fs";
|
|
4
|
+
import { open } from "node:fs/promises";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { isPathInside, resolveWithin } from "../lib/output-safety.js";
|
|
7
|
+
function errorCode(error) {
|
|
8
|
+
return error && typeof error === "object" && "code" in error
|
|
9
|
+
? String(error.code)
|
|
10
|
+
: undefined;
|
|
11
|
+
}
|
|
12
|
+
function sameFileIdentity(left, right) {
|
|
13
|
+
return left.dev === right.dev && left.ino === right.ino;
|
|
14
|
+
}
|
|
15
|
+
export class SecureSourceFs {
|
|
16
|
+
documentationRoot;
|
|
17
|
+
projectRoot;
|
|
18
|
+
constructor(documentationRoot, projectRoot) {
|
|
19
|
+
this.documentationRoot = path.resolve(documentationRoot);
|
|
20
|
+
this.projectRoot = path.resolve(projectRoot);
|
|
21
|
+
}
|
|
22
|
+
sourcePathError(label, sourcePath, detail) {
|
|
23
|
+
return new Error(`Refusing to use ${label} at ${sourcePath}: ${detail}. Replace it with a real file or directory inside the source root.`);
|
|
24
|
+
}
|
|
25
|
+
async realSourceRoot(root, label, rejectRootSymlink) {
|
|
26
|
+
let rootStat;
|
|
27
|
+
try {
|
|
28
|
+
rootStat = await fs.lstat(root);
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
if (errorCode(error) === "ENOENT") {
|
|
32
|
+
throw this.sourcePathError(label, root, "the source root does not exist");
|
|
33
|
+
}
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
if (rootStat.isSymbolicLink() && rejectRootSymlink) {
|
|
37
|
+
throw this.sourcePathError(label, root, "the source root is a symbolic link");
|
|
38
|
+
}
|
|
39
|
+
const realRoot = await fs.realpath(root);
|
|
40
|
+
if (!(await fs.stat(realRoot)).isDirectory()) {
|
|
41
|
+
throw this.sourcePathError(label, root, "the source root is not a directory");
|
|
42
|
+
}
|
|
43
|
+
return realRoot;
|
|
44
|
+
}
|
|
45
|
+
async readSafeSourceFile(root, sourcePath, label, rejectRootSymlink) {
|
|
46
|
+
const resolvedRoot = path.resolve(root);
|
|
47
|
+
const resolvedSource = path.resolve(sourcePath);
|
|
48
|
+
if (!isPathInside(resolvedRoot, resolvedSource)) {
|
|
49
|
+
throw this.sourcePathError(label, resolvedSource, `the path is outside ${resolvedRoot}`);
|
|
50
|
+
}
|
|
51
|
+
const realRoot = await this.realSourceRoot(resolvedRoot, label, rejectRootSymlink);
|
|
52
|
+
const relativePath = path.relative(resolvedRoot, resolvedSource);
|
|
53
|
+
const components = relativePath.split(path.sep).filter(Boolean);
|
|
54
|
+
let currentPath = resolvedRoot;
|
|
55
|
+
for (const [index, component] of components.entries()) {
|
|
56
|
+
currentPath = path.join(currentPath, component);
|
|
57
|
+
let stat;
|
|
58
|
+
try {
|
|
59
|
+
stat = await fs.lstat(currentPath);
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
if (errorCode(error) === "ENOENT") {
|
|
63
|
+
throw this.sourcePathError(label, currentPath, "the path does not exist");
|
|
64
|
+
}
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
if (stat.isSymbolicLink()) {
|
|
68
|
+
throw this.sourcePathError(label, currentPath, "the path is a symbolic link");
|
|
69
|
+
}
|
|
70
|
+
if (index < components.length - 1 && !stat.isDirectory()) {
|
|
71
|
+
throw this.sourcePathError(label, currentPath, "a path component is not a directory");
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const sourceStat = await fs.lstat(resolvedSource);
|
|
75
|
+
if (!sourceStat.isFile()) {
|
|
76
|
+
throw this.sourcePathError(label, resolvedSource, "expected a regular file");
|
|
77
|
+
}
|
|
78
|
+
const realSource = await fs.realpath(resolvedSource);
|
|
79
|
+
if (!isPathInside(realRoot, realSource)) {
|
|
80
|
+
throw this.sourcePathError(label, resolvedSource, `the real path ${realSource} is outside ${realRoot}`);
|
|
81
|
+
}
|
|
82
|
+
const noFollow = typeof constants.O_NOFOLLOW === "number" ? constants.O_NOFOLLOW : 0;
|
|
83
|
+
let handle;
|
|
84
|
+
try {
|
|
85
|
+
handle = await open(resolvedSource, constants.O_RDONLY | noFollow);
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
if (errorCode(error) === "ELOOP") {
|
|
89
|
+
throw this.sourcePathError(label, resolvedSource, "the final path became a symbolic link while it was being opened");
|
|
90
|
+
}
|
|
91
|
+
throw error;
|
|
92
|
+
}
|
|
93
|
+
try {
|
|
94
|
+
const openedStat = await handle.stat();
|
|
95
|
+
if (!openedStat.isFile()) {
|
|
96
|
+
throw this.sourcePathError(label, resolvedSource, "the opened source is not a regular file");
|
|
97
|
+
}
|
|
98
|
+
let currentStat;
|
|
99
|
+
let currentRealSource;
|
|
100
|
+
let currentRealStat;
|
|
101
|
+
try {
|
|
102
|
+
currentStat = await fs.lstat(resolvedSource);
|
|
103
|
+
currentRealSource = await fs.realpath(resolvedSource);
|
|
104
|
+
currentRealStat = await fs.lstat(currentRealSource);
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
throw this.sourcePathError(label, resolvedSource, "the path changed while it was being opened");
|
|
108
|
+
}
|
|
109
|
+
if (!isPathInside(realRoot, currentRealSource)) {
|
|
110
|
+
throw this.sourcePathError(label, resolvedSource, `the real path ${currentRealSource} is outside ${realRoot}`);
|
|
111
|
+
}
|
|
112
|
+
if (currentStat.isSymbolicLink() ||
|
|
113
|
+
!currentStat.isFile() ||
|
|
114
|
+
!currentRealStat.isFile() ||
|
|
115
|
+
!sameFileIdentity(sourceStat, openedStat) ||
|
|
116
|
+
!sameFileIdentity(openedStat, currentStat) ||
|
|
117
|
+
!sameFileIdentity(openedStat, currentRealStat)) {
|
|
118
|
+
throw this.sourcePathError(label, resolvedSource, "the source identity changed while it was being opened");
|
|
119
|
+
}
|
|
120
|
+
return { data: await handle.readFile(), stat: openedStat };
|
|
121
|
+
}
|
|
122
|
+
finally {
|
|
123
|
+
await handle.close();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
async readMdxSourceFile(filePath) {
|
|
127
|
+
if (!filePath.toLowerCase().endsWith(".mdx")) {
|
|
128
|
+
throw this.sourcePathError("documentation source", filePath, "expected an .mdx file");
|
|
129
|
+
}
|
|
130
|
+
const { data, stat } = await this.readSafeSourceFile(this.documentationRoot, path.resolve(this.documentationRoot, filePath), "documentation source", false);
|
|
131
|
+
return { content: data.toString("utf8"), stat };
|
|
132
|
+
}
|
|
133
|
+
async readProjectSourceFile(sourcePath, label) {
|
|
134
|
+
return this.readSafeSourceFile(this.projectRoot, sourcePath, label, false);
|
|
135
|
+
}
|
|
136
|
+
async readPublicSourceFile(sourcePath) {
|
|
137
|
+
return this.readSafeSourceFile(path.join(this.projectRoot, "public"), sourcePath, "public source", true);
|
|
138
|
+
}
|
|
139
|
+
async assertPinnedDocumentationRoot(pinned) {
|
|
140
|
+
try {
|
|
141
|
+
const currentRootStat = await fs.lstat(pinned.resolvedRoot);
|
|
142
|
+
const currentRealRoot = await fs.realpath(pinned.resolvedRoot);
|
|
143
|
+
const currentRealRootStat = await fs.stat(currentRealRoot);
|
|
144
|
+
if (currentRealRoot !== pinned.realRoot ||
|
|
145
|
+
!currentRealRootStat.isDirectory() ||
|
|
146
|
+
!sameFileIdentity(pinned.rootStat, currentRootStat) ||
|
|
147
|
+
!sameFileIdentity(pinned.realRootStat, currentRealRootStat)) {
|
|
148
|
+
throw new Error("changed");
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
throw this.sourcePathError("documentation source", pinned.resolvedRoot, "the source root changed while starter documents were being created");
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
async pinDocumentationRoot() {
|
|
156
|
+
const resolvedRoot = path.resolve(this.documentationRoot);
|
|
157
|
+
const rootStat = await fs.lstat(resolvedRoot);
|
|
158
|
+
const realRoot = await this.realSourceRoot(resolvedRoot, "documentation source", false);
|
|
159
|
+
const realRootStat = await fs.stat(realRoot);
|
|
160
|
+
const pinned = { resolvedRoot, rootStat, realRoot, realRootStat };
|
|
161
|
+
await this.assertPinnedDocumentationRoot(pinned);
|
|
162
|
+
return pinned;
|
|
163
|
+
}
|
|
164
|
+
async removeCreatedStarterFile(targetPath, openedStat) {
|
|
165
|
+
try {
|
|
166
|
+
const currentStat = await fs.lstat(targetPath);
|
|
167
|
+
if (!currentStat.isSymbolicLink() &&
|
|
168
|
+
currentStat.isFile() &&
|
|
169
|
+
sameFileIdentity(openedStat, currentStat)) {
|
|
170
|
+
await fs.unlink(targetPath);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
catch (error) {
|
|
174
|
+
if (errorCode(error) !== "ENOENT")
|
|
175
|
+
throw error;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
async writeStarterFile(relativePath, content, pinned) {
|
|
179
|
+
await this.assertPinnedDocumentationRoot(pinned);
|
|
180
|
+
const { resolvedRoot, realRoot } = pinned;
|
|
181
|
+
const targetPath = path.resolve(resolvedRoot, relativePath);
|
|
182
|
+
if (!isPathInside(resolvedRoot, targetPath)) {
|
|
183
|
+
throw this.sourcePathError("documentation source", targetPath, `the starter path is outside ${resolvedRoot}`);
|
|
184
|
+
}
|
|
185
|
+
const parentRelativePath = path.relative(resolvedRoot, path.dirname(targetPath));
|
|
186
|
+
let currentPath = resolvedRoot;
|
|
187
|
+
for (const component of parentRelativePath
|
|
188
|
+
.split(path.sep)
|
|
189
|
+
.filter(Boolean)) {
|
|
190
|
+
currentPath = path.join(currentPath, component);
|
|
191
|
+
try {
|
|
192
|
+
await fs.mkdir(currentPath);
|
|
193
|
+
}
|
|
194
|
+
catch (error) {
|
|
195
|
+
if (errorCode(error) !== "EEXIST")
|
|
196
|
+
throw error;
|
|
197
|
+
}
|
|
198
|
+
const stat = await fs.lstat(currentPath);
|
|
199
|
+
if (stat.isSymbolicLink() || !stat.isDirectory()) {
|
|
200
|
+
throw this.sourcePathError("documentation source", currentPath, "a starter directory component is not a real directory");
|
|
201
|
+
}
|
|
202
|
+
const realPath = await fs.realpath(currentPath);
|
|
203
|
+
if (!isPathInside(realRoot, realPath)) {
|
|
204
|
+
throw this.sourcePathError("documentation source", currentPath, `the real path ${realPath} is outside ${realRoot}`);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
const parentPath = path.dirname(targetPath);
|
|
208
|
+
const parentStat = await fs.lstat(parentPath);
|
|
209
|
+
const realParent = await fs.realpath(parentPath);
|
|
210
|
+
try {
|
|
211
|
+
const stat = await fs.lstat(targetPath);
|
|
212
|
+
const detail = stat.isSymbolicLink()
|
|
213
|
+
? "the starter file is a symbolic link"
|
|
214
|
+
: "the starter file appeared after the empty-source check";
|
|
215
|
+
throw this.sourcePathError("documentation source", targetPath, detail);
|
|
216
|
+
}
|
|
217
|
+
catch (error) {
|
|
218
|
+
if (errorCode(error) !== "ENOENT")
|
|
219
|
+
throw error;
|
|
220
|
+
}
|
|
221
|
+
const noFollow = typeof constants.O_NOFOLLOW === "number" ? constants.O_NOFOLLOW : 0;
|
|
222
|
+
let handle;
|
|
223
|
+
try {
|
|
224
|
+
handle = await open(targetPath, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | noFollow, 0o666);
|
|
225
|
+
}
|
|
226
|
+
catch (error) {
|
|
227
|
+
if (errorCode(error) === "EEXIST" || errorCode(error) === "ELOOP") {
|
|
228
|
+
throw this.sourcePathError("documentation source", targetPath, "the starter path changed before it could be created");
|
|
229
|
+
}
|
|
230
|
+
throw error;
|
|
231
|
+
}
|
|
232
|
+
let openedStat;
|
|
233
|
+
try {
|
|
234
|
+
openedStat = await handle.stat();
|
|
235
|
+
let currentParentStat;
|
|
236
|
+
let currentRealParent;
|
|
237
|
+
let currentTargetStat;
|
|
238
|
+
try {
|
|
239
|
+
currentParentStat = await fs.lstat(parentPath);
|
|
240
|
+
currentRealParent = await fs.realpath(parentPath);
|
|
241
|
+
currentTargetStat = await fs.lstat(targetPath);
|
|
242
|
+
}
|
|
243
|
+
catch {
|
|
244
|
+
throw this.sourcePathError("documentation source", targetPath, "the starter path changed while it was being created");
|
|
245
|
+
}
|
|
246
|
+
const stableRootSymlink = parentPath === resolvedRoot &&
|
|
247
|
+
parentStat.isSymbolicLink() &&
|
|
248
|
+
currentParentStat.isSymbolicLink() &&
|
|
249
|
+
sameFileIdentity(parentStat, currentParentStat);
|
|
250
|
+
if ((!currentParentStat.isDirectory() && !stableRootSymlink) ||
|
|
251
|
+
currentRealParent !== realParent ||
|
|
252
|
+
!isPathInside(realRoot, currentRealParent) ||
|
|
253
|
+
!sameFileIdentity(parentStat, currentParentStat) ||
|
|
254
|
+
currentTargetStat.isSymbolicLink() ||
|
|
255
|
+
!currentTargetStat.isFile() ||
|
|
256
|
+
!sameFileIdentity(openedStat, currentTargetStat)) {
|
|
257
|
+
throw this.sourcePathError("documentation source", targetPath, "the starter path changed while it was being created");
|
|
258
|
+
}
|
|
259
|
+
await handle.writeFile(content);
|
|
260
|
+
}
|
|
261
|
+
catch (error) {
|
|
262
|
+
try {
|
|
263
|
+
await handle.close();
|
|
264
|
+
}
|
|
265
|
+
catch {
|
|
266
|
+
// Preserve the creation failure; cleanup below still verifies identity.
|
|
267
|
+
}
|
|
268
|
+
if (openedStat) {
|
|
269
|
+
try {
|
|
270
|
+
await this.removeCreatedStarterFile(targetPath, openedStat);
|
|
271
|
+
}
|
|
272
|
+
catch {
|
|
273
|
+
// Preserve the original failure rather than masking its cause.
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
throw error;
|
|
277
|
+
}
|
|
278
|
+
await handle.close();
|
|
279
|
+
}
|
|
280
|
+
async writeStarterFilesIfEmpty(files) {
|
|
281
|
+
const pinned = await this.pinDocumentationRoot();
|
|
282
|
+
const existingFiles = await this.getAllMdxFiles();
|
|
283
|
+
await this.assertPinnedDocumentationRoot(pinned);
|
|
284
|
+
if (existingFiles.length > 0)
|
|
285
|
+
return;
|
|
286
|
+
for await (const [relativePath, content] of files) {
|
|
287
|
+
await this.writeStarterFile(relativePath, content, pinned);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
async pathState(filePath, hashContents = false) {
|
|
291
|
+
try {
|
|
292
|
+
const stat = await fs.lstat(filePath);
|
|
293
|
+
const kind = stat.isDirectory()
|
|
294
|
+
? "directory"
|
|
295
|
+
: stat.isFile()
|
|
296
|
+
? "file"
|
|
297
|
+
: stat.isSymbolicLink()
|
|
298
|
+
? "symlink"
|
|
299
|
+
: "other";
|
|
300
|
+
const hash = hashContents && stat.isFile()
|
|
301
|
+
? createHash("sha256")
|
|
302
|
+
.update(await fs.readFile(filePath))
|
|
303
|
+
.digest("hex")
|
|
304
|
+
: "";
|
|
305
|
+
return `${kind}:${stat.size}:${stat.mtimeMs}:${stat.dev}:${stat.ino}:${hash}`;
|
|
306
|
+
}
|
|
307
|
+
catch (error) {
|
|
308
|
+
if (errorCode(error) === "ENOENT")
|
|
309
|
+
return "missing";
|
|
310
|
+
throw error;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
async treeState(root, includeFile, hashContents = false) {
|
|
314
|
+
if (!(await fs.pathExists(root)))
|
|
315
|
+
return "missing";
|
|
316
|
+
const entries = [];
|
|
317
|
+
const scan = async (directory, relativePath = "") => {
|
|
318
|
+
const children = await fs.readdir(directory, { withFileTypes: true });
|
|
319
|
+
children.sort((left, right) => left.name.localeCompare(right.name));
|
|
320
|
+
for (const child of children) {
|
|
321
|
+
const childRelativePath = path.join(relativePath, child.name);
|
|
322
|
+
const childPath = path.join(directory, child.name);
|
|
323
|
+
const state = await this.pathState(childPath, hashContents);
|
|
324
|
+
if (state === "missing")
|
|
325
|
+
continue;
|
|
326
|
+
if (child.isDirectory()) {
|
|
327
|
+
entries.push(`${childRelativePath.replace(/\\/g, "/")}:${state}`);
|
|
328
|
+
await scan(childPath, childRelativePath);
|
|
329
|
+
}
|
|
330
|
+
else if (includeFile(childRelativePath)) {
|
|
331
|
+
entries.push(`${childRelativePath.replace(/\\/g, "/")}:${state}`);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
await scan(root);
|
|
336
|
+
return entries.join("\n");
|
|
337
|
+
}
|
|
338
|
+
async scanPublicFiles() {
|
|
339
|
+
const publicDir = path.join(this.projectRoot, "public");
|
|
340
|
+
let publicStat;
|
|
341
|
+
try {
|
|
342
|
+
publicStat = await fs.lstat(publicDir);
|
|
343
|
+
}
|
|
344
|
+
catch (error) {
|
|
345
|
+
if (errorCode(error) === "ENOENT")
|
|
346
|
+
return null;
|
|
347
|
+
throw error;
|
|
348
|
+
}
|
|
349
|
+
if (publicStat.isSymbolicLink() || !publicStat.isDirectory()) {
|
|
350
|
+
throw this.sourcePathError("public source", publicDir, "the public source root must be a real directory");
|
|
351
|
+
}
|
|
352
|
+
const realPublicDir = await this.realSourceRoot(publicDir, "public source", true);
|
|
353
|
+
const files = [];
|
|
354
|
+
const scanDir = async (directory, relativePath = "") => {
|
|
355
|
+
const entries = await fs.readdir(directory, { withFileTypes: true });
|
|
356
|
+
for (const entry of entries) {
|
|
357
|
+
const sourcePath = path.join(directory, entry.name);
|
|
358
|
+
const entryRelativePath = path.join(relativePath, entry.name);
|
|
359
|
+
const stat = await fs.lstat(sourcePath);
|
|
360
|
+
if (stat.isSymbolicLink()) {
|
|
361
|
+
throw this.sourcePathError("public source", sourcePath, "the path is a symbolic link");
|
|
362
|
+
}
|
|
363
|
+
const realPath = await fs.realpath(sourcePath);
|
|
364
|
+
if (!isPathInside(realPublicDir, realPath)) {
|
|
365
|
+
throw this.sourcePathError("public source", sourcePath, `the real path ${realPath} is outside ${realPublicDir}`);
|
|
366
|
+
}
|
|
367
|
+
if (stat.isDirectory()) {
|
|
368
|
+
await scanDir(sourcePath, entryRelativePath);
|
|
369
|
+
}
|
|
370
|
+
else if (stat.isFile()) {
|
|
371
|
+
files.push(entryRelativePath);
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
throw this.sourcePathError("public source", sourcePath, "expected a regular file or directory");
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
await scanDir(publicDir);
|
|
379
|
+
return files;
|
|
380
|
+
}
|
|
381
|
+
async getAllMdxFiles() {
|
|
382
|
+
const files = [];
|
|
383
|
+
const realWatchDir = await this.realSourceRoot(this.documentationRoot, "documentation source", false);
|
|
384
|
+
const scanDir = async (dir, relativePath = "") => {
|
|
385
|
+
const entries = await fs.readdir(dir, { withFileTypes: true });
|
|
386
|
+
for (const entry of entries) {
|
|
387
|
+
const fullPath = path.join(dir, entry.name);
|
|
388
|
+
const relPath = path.join(relativePath, entry.name);
|
|
389
|
+
const stat = await fs.lstat(fullPath);
|
|
390
|
+
if (stat.isSymbolicLink()) {
|
|
391
|
+
let linksToDirectory = false;
|
|
392
|
+
try {
|
|
393
|
+
linksToDirectory = (await fs.stat(fullPath)).isDirectory();
|
|
394
|
+
}
|
|
395
|
+
catch (error) {
|
|
396
|
+
if (errorCode(error) !== "ENOENT")
|
|
397
|
+
throw error;
|
|
398
|
+
}
|
|
399
|
+
if (entry.name.endsWith(".mdx") || linksToDirectory) {
|
|
400
|
+
throw this.sourcePathError("documentation source", fullPath, "the path is a symbolic link");
|
|
401
|
+
}
|
|
402
|
+
continue;
|
|
403
|
+
}
|
|
404
|
+
const realPath = await fs.realpath(fullPath);
|
|
405
|
+
if (!isPathInside(realWatchDir, realPath)) {
|
|
406
|
+
throw this.sourcePathError("documentation source", fullPath, `the real path ${realPath} is outside ${realWatchDir}`);
|
|
407
|
+
}
|
|
408
|
+
if (stat.isDirectory()) {
|
|
409
|
+
await scanDir(fullPath, relPath);
|
|
410
|
+
}
|
|
411
|
+
else if (stat.isFile() && entry.name.endsWith(".mdx")) {
|
|
412
|
+
files.push(relPath);
|
|
413
|
+
}
|
|
414
|
+
else if (!stat.isFile() && entry.name.endsWith(".mdx")) {
|
|
415
|
+
throw this.sourcePathError("documentation source", fullPath, "expected a regular .mdx file");
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
await scanDir(this.documentationRoot);
|
|
420
|
+
return files;
|
|
421
|
+
}
|
|
422
|
+
async findPublicAsset(relativePath) {
|
|
423
|
+
const sourcePublicDir = path.join(this.projectRoot, "public");
|
|
424
|
+
const normalized = relativePath.replace(/\\/g, "/");
|
|
425
|
+
resolveWithin(sourcePublicDir, normalized);
|
|
426
|
+
let rootStat;
|
|
427
|
+
try {
|
|
428
|
+
rootStat = await fs.lstat(sourcePublicDir);
|
|
429
|
+
}
|
|
430
|
+
catch (error) {
|
|
431
|
+
if (errorCode(error) === "ENOENT")
|
|
432
|
+
return null;
|
|
433
|
+
throw error;
|
|
434
|
+
}
|
|
435
|
+
if (rootStat.isSymbolicLink() || !rootStat.isDirectory()) {
|
|
436
|
+
throw this.sourcePathError("public source", sourcePublicDir, "the public source root must be a real directory");
|
|
437
|
+
}
|
|
438
|
+
const realPublicDir = await this.realSourceRoot(sourcePublicDir, "public source", true);
|
|
439
|
+
let currentPath = sourcePublicDir;
|
|
440
|
+
const parts = normalized.split("/").filter(Boolean);
|
|
441
|
+
for (const [index, part] of parts.entries()) {
|
|
442
|
+
let entries;
|
|
443
|
+
try {
|
|
444
|
+
entries = await fs.readdir(currentPath);
|
|
445
|
+
}
|
|
446
|
+
catch {
|
|
447
|
+
return null;
|
|
448
|
+
}
|
|
449
|
+
const actualName = entries.find((entry) => entry === part) ??
|
|
450
|
+
entries.find((entry) => entry.toLowerCase() === part.toLowerCase());
|
|
451
|
+
if (!actualName)
|
|
452
|
+
return null;
|
|
453
|
+
currentPath = path.join(currentPath, actualName);
|
|
454
|
+
const stat = await fs.lstat(currentPath);
|
|
455
|
+
if (stat.isSymbolicLink()) {
|
|
456
|
+
throw this.sourcePathError("public source", currentPath, "the path is a symbolic link");
|
|
457
|
+
}
|
|
458
|
+
if (index < parts.length - 1 && !stat.isDirectory())
|
|
459
|
+
return null;
|
|
460
|
+
}
|
|
461
|
+
const stat = await fs.lstat(currentPath);
|
|
462
|
+
if (!stat.isFile())
|
|
463
|
+
return null;
|
|
464
|
+
const realPath = await fs.realpath(currentPath);
|
|
465
|
+
if (!isPathInside(realPublicDir, realPath)) {
|
|
466
|
+
throw this.sourcePathError("public source", currentPath, `the real path ${realPath} is outside ${realPublicDir}`);
|
|
467
|
+
}
|
|
468
|
+
return currentPath;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { PageMeta, SectionConfig } from "../lib/types.js";
|
|
2
|
+
import { type SitemapEntry } from "../templates/app/sitemap.js";
|
|
3
|
+
import { type PageWithBody } from "../templates/llms/llmsFull.js";
|
|
4
|
+
import type { PublicAssetManager } from "./public-asset-manager.js";
|
|
5
|
+
export interface SiteMetadata {
|
|
6
|
+
url: string | null;
|
|
7
|
+
name: string;
|
|
8
|
+
description: string;
|
|
9
|
+
}
|
|
10
|
+
type ResolvePages = () => Promise<PageMeta[]>;
|
|
11
|
+
type ReadMdxSource = (filePath: string) => Promise<{
|
|
12
|
+
content: string;
|
|
13
|
+
}>;
|
|
14
|
+
type ReadOpenApiBody = (slug: string) => string | undefined;
|
|
15
|
+
type ReadConfigSource = () => Promise<string | null>;
|
|
16
|
+
export declare function loadSiteUrl(readConfigSource: ReadConfigSource): Promise<string | null>;
|
|
17
|
+
export declare function loadSiteMetadata(readConfigSource: ReadConfigSource): Promise<SiteMetadata>;
|
|
18
|
+
export declare function buildSitemapEntries(pages: PageMeta[], sectionsConfig: SectionConfig[] | null): SitemapEntry[];
|
|
19
|
+
export declare function writeSitemap(outputDir: string, sectionsConfig: SectionConfig[] | null, resolvePages: ResolvePages, resolveSiteUrl: () => Promise<string | null>): Promise<void>;
|
|
20
|
+
export declare function writeRobots(outputDir: string, resolveSiteUrl: () => Promise<string | null>): Promise<void>;
|
|
21
|
+
export declare function collectPageBodies(pages: PageMeta[], readMdxSource: ReadMdxSource, readOpenApiBody: ReadOpenApiBody): Promise<PageWithBody[]>;
|
|
22
|
+
export declare function writeLlmsFiles(outputDir: string, sectionsConfig: SectionConfig[] | null, resolvePages: ResolvePages, readMdxSource: ReadMdxSource, readOpenApiBody: ReadOpenApiBody, resolveSiteMetadata: () => Promise<SiteMetadata>, publicAssetManager: PublicAssetManager): Promise<void>;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import chalk from "chalk";
|
|
2
|
+
import fs from "fs-extra";
|
|
3
|
+
import { resolveOutputPath } from "../lib/output-safety.js";
|
|
4
|
+
import { safeMatter, writeFileAtomic } from "../lib/utils.js";
|
|
5
|
+
import { robotsTemplate } from "../templates/app/robots.js";
|
|
6
|
+
import { sitemapTemplate, } from "../templates/app/sitemap.js";
|
|
7
|
+
import { llmsFullTemplate, } from "../templates/llms/llmsFull.js";
|
|
8
|
+
import { llmsIndexTemplate } from "../templates/llms/llmsIndex.js";
|
|
9
|
+
import { skillMdTemplate } from "../templates/llms/skillMd.js";
|
|
10
|
+
export async function loadSiteUrl(readConfigSource) {
|
|
11
|
+
const fromEnv = process.env.NEXT_PUBLIC_SITE_URL?.trim();
|
|
12
|
+
if (fromEnv)
|
|
13
|
+
return fromEnv.replace(/\/$/, "");
|
|
14
|
+
try {
|
|
15
|
+
const content = await readConfigSource();
|
|
16
|
+
if (content !== null) {
|
|
17
|
+
const parsed = JSON.parse(content);
|
|
18
|
+
if (typeof parsed.url === "string" && parsed.url.trim() !== "") {
|
|
19
|
+
return parsed.url.trim().replace(/\/$/, "");
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
console.warn(chalk.yellow("⚠️ Error reading config.json"), error);
|
|
25
|
+
}
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
export async function loadSiteMetadata(readConfigSource) {
|
|
29
|
+
let url = null;
|
|
30
|
+
let name = "Documentation";
|
|
31
|
+
let description = "";
|
|
32
|
+
try {
|
|
33
|
+
const fromEnv = process.env.NEXT_PUBLIC_SITE_URL?.trim();
|
|
34
|
+
if (fromEnv)
|
|
35
|
+
url = fromEnv.replace(/\/$/, "");
|
|
36
|
+
const content = await readConfigSource();
|
|
37
|
+
if (content !== null) {
|
|
38
|
+
const parsed = JSON.parse(content);
|
|
39
|
+
if (!url && typeof parsed.url === "string" && parsed.url.trim() !== "") {
|
|
40
|
+
url = parsed.url.trim().replace(/\/$/, "");
|
|
41
|
+
}
|
|
42
|
+
if (typeof parsed.name === "string" && parsed.name.trim() !== "") {
|
|
43
|
+
name = parsed.name.trim();
|
|
44
|
+
}
|
|
45
|
+
else if (typeof parsed.title === "string" &&
|
|
46
|
+
parsed.title.trim() !== "") {
|
|
47
|
+
name = parsed.title.trim();
|
|
48
|
+
}
|
|
49
|
+
if (typeof parsed.description === "string" &&
|
|
50
|
+
parsed.description.trim() !== "") {
|
|
51
|
+
description = parsed.description.trim();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
console.warn(chalk.yellow("⚠️ Error reading config.json for llms metadata"), error);
|
|
57
|
+
}
|
|
58
|
+
return { url, name, description };
|
|
59
|
+
}
|
|
60
|
+
export function buildSitemapEntries(pages, sectionsConfig) {
|
|
61
|
+
const sectionSlugs = new Set((sectionsConfig || [])
|
|
62
|
+
.map((section) => section.slug)
|
|
63
|
+
.filter((slug) => typeof slug === "string" && slug !== ""));
|
|
64
|
+
const entries = pages.map((page) => {
|
|
65
|
+
let priority = 0.5;
|
|
66
|
+
if (page.slug === "") {
|
|
67
|
+
priority = 1.0;
|
|
68
|
+
}
|
|
69
|
+
else if (sectionSlugs.has(page.slug)) {
|
|
70
|
+
priority = 0.8;
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
slug: page.slug,
|
|
74
|
+
lastModified: page.lastModified,
|
|
75
|
+
changeFrequency: "weekly",
|
|
76
|
+
priority,
|
|
77
|
+
};
|
|
78
|
+
});
|
|
79
|
+
if (!entries.some((entry) => entry.slug === "")) {
|
|
80
|
+
entries.unshift({
|
|
81
|
+
slug: "",
|
|
82
|
+
changeFrequency: "weekly",
|
|
83
|
+
priority: 1.0,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
return entries;
|
|
87
|
+
}
|
|
88
|
+
export async function writeSitemap(outputDir, sectionsConfig, resolvePages, resolveSiteUrl) {
|
|
89
|
+
const siteUrl = await resolveSiteUrl();
|
|
90
|
+
const entries = buildSitemapEntries(await resolvePages(), sectionsConfig);
|
|
91
|
+
await writeFileAtomic(resolveOutputPath(outputDir, "app", "sitemap.ts"), sitemapTemplate(entries));
|
|
92
|
+
console.log(chalk.green(`🗺️ Generated sitemap.ts with ${entries.length} page(s)${siteUrl ? ` using ${siteUrl}` : " (waiting for a deployment URL)"}`));
|
|
93
|
+
}
|
|
94
|
+
export async function writeRobots(outputDir, resolveSiteUrl) {
|
|
95
|
+
const siteUrl = await resolveSiteUrl();
|
|
96
|
+
await writeFileAtomic(resolveOutputPath(outputDir, "app", "robots.ts"), robotsTemplate);
|
|
97
|
+
console.log(chalk.green(siteUrl
|
|
98
|
+
? `🤖 Regenerated robots.ts with sitemap link`
|
|
99
|
+
: `🤖 Regenerated robots.ts (no sitemap link)`));
|
|
100
|
+
}
|
|
101
|
+
export async function collectPageBodies(pages, readMdxSource, readOpenApiBody) {
|
|
102
|
+
return Promise.all(pages.map(async (page) => {
|
|
103
|
+
if (!page.path.endsWith(".mdx")) {
|
|
104
|
+
return { ...page, body: readOpenApiBody(page.slug) ?? "" };
|
|
105
|
+
}
|
|
106
|
+
const { content: raw } = await readMdxSource(page.path);
|
|
107
|
+
const { content: body } = safeMatter(raw, page.path);
|
|
108
|
+
return { ...page, body };
|
|
109
|
+
}));
|
|
110
|
+
}
|
|
111
|
+
export async function writeLlmsFiles(outputDir, sectionsConfig, resolvePages, readMdxSource, readOpenApiBody, resolveSiteMetadata, publicAssetManager) {
|
|
112
|
+
await fs.ensureDir(resolveOutputPath(outputDir, "public"));
|
|
113
|
+
const { url: baseUrl, name, description } = await resolveSiteMetadata();
|
|
114
|
+
const resolvedPages = await resolvePages();
|
|
115
|
+
const pagesWithBodies = await collectPageBodies(resolvedPages, readMdxSource, readOpenApiBody);
|
|
116
|
+
const docsContent = pagesWithBodies.map((page) => {
|
|
117
|
+
const route = page.slug.replace(/^\/+|\/+$/g, "");
|
|
118
|
+
const pagePath = route
|
|
119
|
+
? `app/(site)/${route}/page.tsx`
|
|
120
|
+
: "app/(site)/page.tsx";
|
|
121
|
+
return {
|
|
122
|
+
uri: `docs://${route || "/"}`,
|
|
123
|
+
name: page.title,
|
|
124
|
+
path: pagePath,
|
|
125
|
+
content: page.body,
|
|
126
|
+
};
|
|
127
|
+
});
|
|
128
|
+
await writeFileAtomic(resolveOutputPath(outputDir, "services", "mcp", "docs-content.json"), JSON.stringify(docsContent, null, 2) + "\n");
|
|
129
|
+
const indexContent = llmsIndexTemplate({
|
|
130
|
+
siteName: name,
|
|
131
|
+
siteDescription: description,
|
|
132
|
+
baseUrl,
|
|
133
|
+
pages: resolvedPages,
|
|
134
|
+
sectionsConfig,
|
|
135
|
+
});
|
|
136
|
+
const fullContent = llmsFullTemplate({
|
|
137
|
+
siteName: name,
|
|
138
|
+
siteDescription: description,
|
|
139
|
+
baseUrl,
|
|
140
|
+
pages: pagesWithBodies,
|
|
141
|
+
sectionsConfig,
|
|
142
|
+
});
|
|
143
|
+
await publicAssetManager.writePublicAggregate("llms.txt", indexContent);
|
|
144
|
+
await publicAssetManager.writePublicAggregate("llms-full.txt", fullContent);
|
|
145
|
+
const skillContent = skillMdTemplate({
|
|
146
|
+
siteName: name,
|
|
147
|
+
siteDescription: description,
|
|
148
|
+
baseUrl,
|
|
149
|
+
pages: resolvedPages,
|
|
150
|
+
sectionsConfig,
|
|
151
|
+
});
|
|
152
|
+
await publicAssetManager.writePublicAggregate("skill.md", skillContent);
|
|
153
|
+
await publicAssetManager.syncMcpManifest(baseUrl, name);
|
|
154
|
+
await publicAssetManager.syncLlmsPageFiles(pagesWithBodies, baseUrl);
|
|
155
|
+
console.log(chalk.green(`🤖 Generated llms.txt and llms-full.txt with ${resolvedPages.length} page(s)${baseUrl ? ` using ${baseUrl}` : " (relative URLs)"}`));
|
|
156
|
+
}
|