-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathllm.js
More file actions
158 lines (132 loc) · 4.74 KB
/
Copy pathllm.js
File metadata and controls
158 lines (132 loc) · 4.74 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import * as path from "node:path";
import { exec } from "node:child_process";
import { promisify } from "node:util";
import fs from "node:fs/promises";
const execAsync = promisify(exec);
const bins = await import(
path.join(import.meta.dirname, "../node_modules/rescript/cli/common/bins.js")
);
const rescriptTools = bins["rescript_tools_exe"];
if (!rescriptTools) {
throw new Error("rescript-tools not found");
}
async function getDocJson(filePath) {
try {
const command = `${rescriptTools} doc "${filePath}"`;
const options = { maxBuffer: 10 * 1024 * 1024 };
const { stdout, stderr } = await execAsync(command, options);
if (stderr) {
throw new Error(`Error executing command for ${filePath}: ${stderr}`);
}
return JSON.parse(stdout);
} catch (error) {
throw new Error(`Failed to get documentation JSON for ${filePath}:`, error);
}
}
async function processFile(filePath) {
const json = await getDocJson(filePath);
const relativePath = path.relative(path.join(import.meta.dirname, ".."), filePath);
const moduleName = moduleNameForFile(relativePath);
const types = [];
const functions = [];
function mkType(item) {
let description = "";
if (item.docstrings.length > 0) {
description = "\n Description: " + item.docstrings.join("\n");
}
let fields = "";
if (item.detail && item.detail.kind === "record") {
fields =
"\n Fields:\n" +
item.detail.items
.map((field) => {
let fieldDoc = "";
if (field.docstrings.length > 0) {
fieldDoc = " - " + field.docstrings.join(" ");
}
return ` - ${field.name}: ${field.signature}${fieldDoc}`;
})
.join("\n");
}
return `- ${item.signature}${description}${fields}`;
}
function mkFunction(item) {
let description = "";
if (item.docstrings.length > 0) {
description = "\n Description: " + item.docstrings.join("\n");
}
return `- ${item.signature}${description}`;
}
for (const item of json.items) {
switch (item.kind) {
case "type":
types.push(mkType(item));
break;
case "value":
functions.push(mkFunction(item));
break;
}
}
let typeString = "";
if (types.length > 0) {
typeString = "\n\nTypes:\n\n" + types.join("\n\n");
}
let functionString = "";
if (functions.length > 0) {
functionString = "\n\nFunctions:\n\n" + functions.join("\n\n");
}
return `WebApiFile: ${json.source.filepath}
Module: ${moduleName}${typeString}${functionString}
`;
}
const rootDir = path.join(import.meta.dirname, "..");
const rootConfig = JSON.parse(await fs.readFile(path.join(rootDir, "rescript.json"), "utf-8"));
const publicModulesBySourceDir = new Map(
rootConfig.sources
.filter((source) => source !== null && typeof source === "object")
.filter((source) => source.dir?.startsWith("src/") && Array.isArray(source.public))
.map((source) => [source.dir, new Set(source.public)]),
);
function normalizeRelativePath(filePath) {
return path.relative(rootDir, filePath).split(path.sep).join("/");
}
function sourceDirForRelativePath(relativePath) {
for (const sourceDir of publicModulesBySourceDir.keys()) {
if (relativePath.startsWith(`${sourceDir}/`)) {
return sourceDir;
}
}
}
function isPublicFile(filePath) {
const relativePath = normalizeRelativePath(filePath);
const sourceDir = sourceDirForRelativePath(relativePath);
if (!sourceDir) {
return false;
}
const moduleName = path.basename(relativePath, ".res").replace("$", "");
return publicModulesBySourceDir.get(sourceDir).has(moduleName);
}
function moduleNameForFile(relativePath) {
return `WebAPI.${path.basename(relativePath, ".res")}`;
}
const pattern = "../src/*/**/*.res";
const files = [];
for await (const file of fs.glob(pattern, { recursive: true, cwd: import.meta.dirname })) {
const filePath = path.join(import.meta.dirname, file);
if (isPublicFile(filePath)) {
files.push(filePath);
}
}
files.sort();
const pages = await Promise.all(files.map(processFile));
const packageJson = await fs.readFile(path.join(import.meta.dirname, "../package.json"), "utf-8");
const version = JSON.parse(packageJson).version;
const sha = await execAsync("git rev-parse --short HEAD").then(({ stdout }) => stdout.trim());
const fullVersion = `${version}-alpha-${sha}`;
const header = `Experimental ReScript WebAPI Documentation ${fullVersion}
This is the API documentation for the experimental WebAPI module version ${fullVersion}.
More information can be found on https://rescript-lang.github.io/experimental-rescript-webapi/
`;
const content = pages.join("\n---\n\n");
await fs.writeFile(path.join(import.meta.dirname, "public/llm.txt"), header + content);
console.log("Generated llm.txt");