-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathsplit-definition.js
163 lines (145 loc) · 4.7 KB
/
split-definition.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
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
159
160
161
162
163
const path = require('path');
const fs = require('fs');
const mkdirp = require('mkdirp');
const chalk = require('chalk');
const isEqual = require('lodash.isequal');
const {
writeYaml,
readYaml,
implicitlyReferenceDiscriminator,
replace$Refs,
traverseFolderDeep,
ensureForwardSlashesInPath,
OPENAPI3_COMPONENTS,
OPENAPI3_METHODS
} = require('./utils');
function langToExt(lang) {
return (
{
php: '.php',
'c#': '.cs',
shell: '.sh',
curl: '.sh',
bash: '.sh',
javascript: '.js',
js: '.js',
python: '.py'
}[lang.toLowerCase()] || '.txt'
);
}
module.exports = function(openapi, openapiDir) {
mkdirp.sync(openapiDir);
const pathsDir = path.join(openapiDir, 'paths');
mkdirp.sync(pathsDir);
if (openapi.paths) {
for (const oasPath of Object.keys(openapi.paths)) {
const pathFile = path.join(pathsDir, pathToFilename(oasPath)) + '.yaml';
const pathData = openapi.paths[oasPath];
for (const method of OPENAPI3_METHODS) {
const methodData = pathData[method];
if (
!methodData ||
!methodData['x-code-samples'] ||
!Array.isArray(methodData['x-code-samples'])
) {
continue;
}
for (const sample of methodData['x-code-samples']) {
if (sample.source && sample.source.$ref) continue;
const sampleFileName = path.join(
openapiDir,
'code_samples',
sample.lang,
pathToFilename(oasPath),
method + langToExt(sample.lang)
);
mkdirp.sync(path.dirname(sampleFileName));
fs.writeFileSync(sampleFileName, sample.source);
sample.source = {
$ref: path.relative(pathsDir, sampleFileName)
};
}
}
writeYaml(pathData, pathFile);
openapi.paths[oasPath] = {
$ref: ensureForwardSlashesInPath(path.relative(openapiDir, pathFile))
};
}
}
const componentsDir = path.join(openapiDir, 'components');
mkdirp.sync(componentsDir);
const componentsFiles = {};
if (openapi.components) {
for (const componentType of OPENAPI3_COMPONENTS) {
const compDir = path.join(componentsDir, componentType);
if (openapi.components[componentType]) {
mkdirp.sync(compDir);
for (const componentName of Object.keys(openapi.components[componentType])) {
const filename = path.join(compDir, componentName) + '.yaml';
const componentData = openapi.components[componentType][componentName];
if (fs.existsSync(filename) && isEqual(readYaml(filename), componentData)) {
console.warn(
chalk.yellow(
`warning: conflict for ${componentName} - file already exists with different content: ${chalk.blue(
filename
)} ... Skip.`
)
);
continue;
}
writeYaml(componentData, filename);
let inherits = [];
if (componentType === 'schemas') {
inherits = (componentData.allOf || []).map(s => s.$ref).filter(Boolean);
}
componentsFiles[componentType] = componentsFiles[componentType] || {};
componentsFiles[componentType][componentName] = {
inherits,
filename
};
if (componentType !== 'securitySchemes') {
// security schemas must referenced from components
delete openapi.components[componentType][componentName];
}
}
if (Object.keys(openapi.components[componentType]).length === 0) {
delete openapi.components[componentType];
}
}
}
if (Object.keys(openapi.components).length === 0) {
delete openapi.components;
}
}
traverseFolderDeep(pathsDir, filename => {
if (!filename.endsWith('.yaml') && !filename.endsWith('.yml')) {
return;
}
const pathData = readYaml(filename);
replace$Refs(pathData, pathsDir, componentsFiles);
writeYaml(pathData, filename);
});
traverseFolderDeep(componentsDir, filename => {
if (!filename.endsWith('.yaml') && !filename.endsWith('.yml')) {
return;
}
const compData = readYaml(filename);
replace$Refs(compData, path.dirname(filename), componentsFiles);
implicitlyReferenceDiscriminator(
compData,
path.basename(filename, path.extname(filename)),
filename,
componentsFiles.schemas || {}
);
writeYaml(compData, filename);
});
replace$Refs(openapi, openapiDir, componentsFiles);
writeYaml(openapi, path.join(openapiDir, 'openapi.yaml'));
};
function pathToFilename(path) {
return path
.replace(/~1/g, '/')
.replace(/~0/g, '~')
.substring(1)
.replace(/\//g, '@');
}