-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathutils.js
241 lines (209 loc) · 6.42 KB
/
utils.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
const fs = require('fs');
const path = require('path');
const ejs = require('ejs');
const chalk = require('chalk');
const yaml = require('js-yaml');
const mkdirp = require('mkdirp');
function provisionPaths(file, openapiDir) {
const src = path.resolve(__dirname, '../template/', file);
let target;
if (openapiDir) {
target = path.resolve(openapiDir, file.replace(/^openapi\//, ''));
} else {
target = path.resolve(file);
}
return { src, target };
}
exports.OPENAPI3_METHODS = ['get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace'];
exports.OPENAPI3_COMPONENTS = [
'schemas',
'responses',
'parameters',
'examples',
'headers',
'requestBodies',
'links',
'callbacks',
'securitySchemes'
];
exports.validateDefinitionFileName = fileName => {
if (!fs.existsSync(fileName)) {
return chalk.red(`File ${chalk.blue(fileName)} does not exist`);
}
let file;
try {
file = yaml.safeLoad(fs.readFileSync(fileName, 'utf8'));
} catch (e) {
return chalk.red(e.message);
}
if (file.swagger) {
return chalk.red('OpenAPI 2 is not supported by this tool');
}
if (!file.openapi) {
return chalk.red('File does not conform to the OpenAPI Specification');
}
return true;
};
exports.copy = async (file, openAPIDir) => {
const { src, target } = provisionPaths(file, openAPIDir);
mkdirp.sync(path.dirname(target));
const rd = fs.createReadStream(src);
const wr = fs.createWriteStream(target);
try {
return await new Promise(function(resolve, reject) {
rd.on('error', reject);
wr.on('error', reject);
wr.on('finish', resolve);
rd.pipe(wr);
});
} catch (error) {
rd.destroy();
wr.end();
throw error;
}
};
exports.render = async (file, data, openAPIDir) => {
const { src, target } = provisionPaths(file, openAPIDir);
const res = await ejs.renderFile(src + '.ejs', data);
fs.writeFileSync(target, res);
};
function copyDirSync(srcDir, targetDir) {
const list = fs.readdirSync(srcDir);
list.forEach(file => {
const src = srcDir + '/' + file;
const dst = targetDir + '/' + file;
const stat = fs.statSync(src);
if (stat && stat.isDirectory()) {
try {
fs.mkdirSync(dst);
} catch (e) {
console.log('Directory already exists: ' + dst);
}
copyDirSync(src, dst);
} else {
try {
fs.writeFileSync(dst, fs.readFileSync(src));
} catch (e) {
console.log("Couldn't copy file: " + dst);
}
}
});
}
exports.copyDirSync = (dir, openapiDir) => {
const { src: srcDir, target: targetDir } = provisionPaths(dir, openapiDir);
mkdirp.sync(targetDir);
copyDirSync(srcDir, targetDir);
};
exports.copyDirToSync = (dir, to) => {
const { src: srcDir } = provisionPaths(dir);
mkdirp.sync(to);
copyDirSync(srcDir, to);
};
exports.readYaml = filename => {
return yaml.safeLoad(fs.readFileSync(filename, 'utf-8'), {
filename
});
};
exports.writeYaml = (data, filename) => {
return fs.writeFileSync(filename, yaml.safeDump(data));
};
exports.traverseFolderDeep = function traverseFolderDeep(folder, callback) {
if (!fs.existsSync(folder) || !fs.statSync(folder).isDirectory()) return;
const files = fs.readdirSync(folder);
for (const f of files) {
const filename = path.join(folder, f);
if (fs.statSync(filename).isDirectory()) {
traverseFolderDeep(filename, callback);
} else {
callback(filename);
}
}
};
exports.replace$Refs = function replace$Refs(obj, relativeFrom, componentFiles = {}) {
crawl(obj, node => {
if (node.$ref && typeof node.$ref === 'string' && node.$ref.startsWith('#/components/')) {
replace(node, '$ref');
} else if (
node.discriminator &&
node.discriminator.mapping &&
typeof node.discriminator.mapping === 'object'
) {
for (const name of Object.keys(node.discriminator.mapping)) {
if (
typeof node.discriminator.mapping[name] === 'string' &&
node.discriminator.mapping[name].startsWith('#/components/')
) {
replace(node.discriminator.mapping, name);
}
}
}
});
function replace(node, key) {
const name = node[key].split('/').pop();
const groupName = node[key].split('/')[2];
if (!componentFiles[groupName] || !componentFiles[groupName][name]) {
return;
}
let filename = path.relative(relativeFrom, componentFiles[groupName][name].filename);
if (!filename.startsWith('.')) {
filename = '.' + path.sep + filename;
}
node[key] = filename;
}
};
exports.implicitlyReferenceDiscriminator = function implicitlyReferenceDiscriminator(
obj,
defName,
filename,
schemaFiles
) {
if (!obj.discriminator) return;
const defPtr = `#/components/schemas/${defName}`;
const implicitMapping = {};
for (const [name, { inherits, filename: parentFilename }] of Object.entries(schemaFiles)) {
if (inherits.indexOf(defPtr) > -1) {
const res = path.relative(path.dirname(filename), parentFilename);
implicitMapping[name] = res.startsWith('.') ? res : '.' + path.sep + res;
}
}
if (!Object.keys(implicitMapping).length) return;
const discriminatorPropSchema = obj.properties[obj.discriminator.propertyName];
const discriminatorEnum = discriminatorPropSchema && discriminatorPropSchema.enum;
const mapping = (obj.discriminator.mapping = obj.discriminator.mapping || {});
for (const name of Object.keys(implicitMapping)) {
if (discriminatorEnum && !discriminatorEnum.includes(name)) {
continue;
}
if (mapping[name] && mapping[name] !== implicitMapping[name]) {
console.warn(
chalk.yellow(
`warning: explicit mapping overlaps with local mapping entry ${chalk.red(
name
)} at ${chalk.blue(filename)}, Check manually, please`
)
);
}
mapping[name] = implicitMapping[name];
}
};
function crawl(object, visitor) {
if (typeof object !== 'object' || object == null) {
return;
}
for (const key of Object.keys(object)) {
visitor(object, key);
crawl(object[key], visitor);
}
}
/**
* Convert Windows backslash paths to slash paths: foo\\bar ➔ foo/bar
* Copied form openapi-cli:
* https://github.com/Redocly/openapi-cli/blob/b389661f91202174d5c46766ef2be73bfe9faf0d/packages/core/src/utils.ts#L161
*/
exports.ensureForwardSlashesInPath = (path) => {
const isExtendedLengthPath = /^\\\\\?\\/.test(path)
if (isExtendedLengthPath) {
return path
}
return path.replace(/\\/g, '/');
}