-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
142 lines (126 loc) · 3.67 KB
/
Copy pathmain.js
File metadata and controls
142 lines (126 loc) · 3.67 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
#!/usr/bin/env node
const path = require("path");
const fs = require("fs");
const axios = require("axios");
const { generateEndpointSchema, validateSpec } = require("./anthropicProxy");
if (!process.env.BASEL_API_KEY && !process.env.ANTHROPIC_API_KEY) {
console.log("Please provide either a BASEL_API_KEY or an ANTHROPIC_API_KEY");
process.exit(1);
}
if (process.env.BASEL_API_KEY && process.env.ANTHROPIC_API_KEY) {
console.log(
"Please provide either a BASEL_API_KEY or an ANTHROPIC_API_KEY, not both"
);
process.exit(1);
}
let USE_BASEL_API = false;
const BASEL_API_URL = process.env.BASEL_API_URL || "http://localhost:8000";
if (process.env.BASEL_API_KEY) {
USE_BASEL_API = true;
console.log("Using Basel API");
console.log(`Using Basel API URL: ${BASEL_API_URL}`);
}
let USE_CLIENTSIDE_API = false;
if (process.env.ANTHROPIC_API_KEY) {
USE_CLIENTSIDE_API = true;
console.log("Using client side API");
}
async function main() {
const args = process.argv.slice(2);
const roots = [];
const includes = [];
const excludes = [];
let i = 0;
while (i < args.length) {
if (args[i] === "--root") {
roots.push(args[i + 1]);
i += 2;
} else if (args[i] === "--include") {
includes.push(args[i + 1]);
i += 2;
} else if (args[i] === "--exclude") {
excludes.push(args[i + 1]);
i += 2;
} else {
i++;
}
}
const defaultRoots = ["./"];
const defaultIncludes = ["api"];
const defaultExcludes = ["node_modules"];
const rootsToUse = roots.length > 0 ? roots : defaultRoots;
const includesToUse = includes.length > 0 ? includes : defaultIncludes;
const excludesToUse = excludes.length > 0 ? excludes : defaultExcludes;
const files = getFiles({
roots: rootsToUse,
includes: includesToUse,
excludes: excludesToUse,
});
console.log("List of files to generate OpenAPI spec for:");
console.log(files);
let yamlDraft = "";
for (let endpoint of files) {
let endpointSchema;
if (USE_BASEL_API) {
const response = await axios.post(
`${BASEL_API_URL}/generate-endpoint-schema`,
{ prompt: endpoint, filepath: endpoint }
);
endpointSchema = response.data.schema;
} else if (USE_CLIENTSIDE_API) {
endpointSchema = await generateEndpointSchema(endpoint, endpoint);
}
console.log(endpointSchema);
yamlDraft += endpointSchema;
}
fs.writeFileSync("draft_spec.yaml", yamlDraft);
let validSpec;
if (USE_BASEL_API) {
const response = await axios.post(`${BASEL_API_URL}/validate-spec`, {
spec: yamlDraft,
});
validSpec = response.data.validYaml;
} else if (USE_CLIENTSIDE_API) {
validSpec = await validateSpec(yamlDraft);
}
writeSpecToFile(validSpec);
}
function writeSpecToFile(validSpec) {
if (validSpec) {
fs.writeFileSync("spec.yaml", validSpec);
}
}
function getFiles(options) {
const {
roots = ["./"],
includes = [],
excludes = ["/node_modules/"],
extensions = ["js", "ts", "jsx", "tsx"],
} = options;
let files = [];
for (const root of roots) {
files = files.concat(walk(root));
}
return files
.filter((file) => {
if (includes.length > 0) {
return includes.some((include) => file.includes(include));
}
return true;
})
.filter((file) => !excludes.some((exclude) => file.includes(exclude)))
.filter((file) => extensions.includes(path.extname(file).slice(1)));
}
function walk(dir) {
let files = [];
for (const file of fs.readdirSync(dir)) {
const filepath = path.join(dir, file);
if (fs.statSync(filepath).isDirectory()) {
files = files.concat(walk(filepath));
} else {
files.push(filepath);
}
}
return files;
}
main();