forked from langchain-ai/langchainjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-entrypoints.js
102 lines (91 loc) · 2.58 KB
/
create-entrypoints.js
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
import path from "path";
import url from "url";
import fs from "fs";
const entrypoints = {
agents: "agents/index",
base_language: "base_language/index",
tools: "agents/tools/index",
chains: "chains/index",
embeddings: "embeddings/index",
llms: "llms/index",
prompts: "prompts/index",
vectorstores: "vectorstores/index",
text_splitter: "text_splitter",
memory: "memory/index",
document: "document",
docstore: "docstore/index",
document_loaders: "document_loaders/index",
chat_models: "chat_models/index",
schema: "schema/index",
sql_db: "sql_db",
callbacks: "callbacks/index",
output_parsers: "output_parsers/index",
retrievers: "retrievers/index",
};
const updateJsonFile = (relativePath, updateFunction) => {
const filePath = path.resolve(
path.dirname(url.fileURLToPath(import.meta.url)),
relativePath
);
const contents = fs.readFileSync(filePath).toString();
const res = updateFunction(JSON.parse(contents));
fs.writeFileSync(filePath, JSON.stringify(res, null, 2));
};
const generateFiles = () => {
const files = [...Object.entries(entrypoints), ["index", "index"]].flatMap(
([key, value]) => {
const compiledPath = `./dist/${value}.js`;
return [
[`${key}.js`, `export * from '${compiledPath}'`],
[`${key}.d.ts`, `export * from '${compiledPath}'`],
];
}
);
return Object.fromEntries(files);
};
const updateConfig = () => {
updateJsonFile("./tsconfig.json", (json) => ({
...json,
typedocOptions: {
...json.typedocOptions,
entryPoints: [...Object.values(entrypoints), "index"].map(
(value) => `src/${value}.ts`
),
},
}));
const generatedFiles = generateFiles();
const filenames = Object.keys(generatedFiles);
updateJsonFile("./package.json", (json) => ({
...json,
exports: Object.fromEntries(
["index", ...Object.keys(entrypoints)].map((key) => {
const entryPoint = {
types: `./${key}.d.ts`,
import: `./${key}.js`,
};
return [key === "index" ? "." : `./${key}`, entryPoint];
})
),
files: ["dist/", ...filenames],
}));
Object.entries(generatedFiles).forEach(([filename, content]) => {
fs.writeFileSync(filename, content);
});
fs.writeFileSync("./.gitignore", filenames.join("\n"));
};
const cleanGenerated = () => {
const filenames = Object.keys(generateFiles());
filenames.forEach((fname) => {
try {
fs.unlinkSync(fname);
} catch {
// ignore error
}
});
};
const command = process.argv[2];
if (command === "clean") {
cleanGenerated();
} else {
updateConfig();
}