-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
76 lines (64 loc) · 2.06 KB
/
index.js
File metadata and controls
76 lines (64 loc) · 2.06 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
'use strict';
const fs = require('fs');
const yaml = require('yaml');
const semver = require('semver');
const recurse = require('reftools/lib/recurse.js').recurse;
const jptr = require('reftools/lib/jptr.js').jptr;
const defaultVersion = '0.1.0';
function combine(files) {
let count = 0;
let version = defaultVersion;
let output = {};
output.openapiExtensionFormat = version;
for (let file of files) {
count++;
let s = fs.readFileSync(file,'utf8');
let input = yaml.parse(s);
for (let prop in input) {
if (prop === 'openapiExtensionFormat') {
if (semver.gt(input.openapiExtensionFormat,version)) {
version = input.openapiExtensionFormat;
}
}
else {
if (!output[prop]) output[prop] = {};
output[prop] = Object.assign({},output[prop],input[prop]);
}
}
}
if (count > 0) {
output.openapiExtensionFormat = version;
console.log(yaml.stringify(output));
}
}
function split(filename) {
let s = fs.readFileSync(filename,'utf8');
let input = yaml.parse(s);
if (input.openapiExtensionFormat) {
let version = defaultVersion;
for (let p in input) {
if (p === 'openapiExtensionFormat') {
version = input.openapiExtensionFormat;
}
else {
let output = {};
output.openapiExtensionFormat = version;
output[p] = input[p];
// populate everything required by a $ref
recurse(input[p],{},function(obj,key,state){
if ((key === '$ref') && (typeof obj[key] === 'string')) {
jptr(output,obj[key],jptr(input,obj[key]));
}
});
fs.writeFileSync('./'+p+'.yaml',yaml.stringify(output),'utf8');
}
}
}
else {
console.warn('Not a Semoasa document');
}
}
module.exports = {
combine : combine,
split : split
};