Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
59 lines (51 loc) · 1.85 KB
/
Copy pathindex.js
File metadata and controls
59 lines (51 loc) · 1.85 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
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
function loadYaml(filePath) {
if (!fs.existsSync(filePath)) {
console.error(`File not found: ${filePath}`);
return null;
}
const fileContents = fs.readFileSync(filePath, 'utf8');
return yaml.load(fileContents);
}
function resolveRefs(data, basePath) {
if (typeof data === 'object' && data !== null) {
if (Array.isArray(data)) {
return data.map(item => resolveRefs(item, basePath));
} else {
const resolvedData = {};
for (const [key, value] of Object.entries(data)) {
if (key === '$ref' && typeof value === 'string' && value.startsWith('@/')) {
const refPath = path.join(basePath, value.slice(2));
const refData = loadYaml(refPath);
if (refData !== null) {
Object.assign(resolvedData, resolveRefs(refData, basePath));
}
} else {
resolvedData[key] = resolveRefs(value, basePath);
}
}
return resolvedData;
}
}
return data;
}
function main() {
const basePath = path.join(__dirname, 'src');
const specPath = path.join(basePath, 'spec.yaml');
const specData = loadYaml(specPath);
if (specData === null) {
console.error(`Failed to load spec file: ${specPath}`);
return;
}
const resolvedData = resolveRefs(specData, basePath);
const outputDir = path.join(__dirname, 'dist');
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
const outputPath = path.join(outputDir, 'schema.yaml');
fs.writeFileSync(outputPath, yaml.dump(resolvedData), 'utf8');
console.log(`Resolved spec written to ${outputPath}`);
}
main();