added support for multifile

This commit is contained in:
Dhroov7
2019-12-04 13:26:41 +05:30
parent 7a2ad3da39
commit 7e963b0ba2
24 changed files with 1957 additions and 75 deletions

View File

@@ -1,7 +1,17 @@
var converter = require('./lib/convert.js'),
validate = require('./lib/validate.js'),
parse = require('./lib/parse.js'),
async = require('async'),
loader = require('speccy/lib/loader'),
_ = require('lodash'),
fs = require('fs');
// options for speccy loader
const loaderOptions = {
resolve: true, // Resolve external references
jsonSchema: true // Treat $ref like JSON Schema and convert to OpenAPI Schema Objects
};
module.exports = {
convert: function(input, options, cb) {
if (input.type === 'string' || input.type === 'json') {
@@ -19,10 +29,82 @@ module.exports = {
return converter.convert(data, options, cb);
});
}
return cb(null, {
result: false,
reason: 'input type:' + input.type + ' is not valid'
});
else if (input.type === 'folder') {
let filesPathArray = input.data,
convertedSpecs = [],
rootFiles = parse.getRootFiles(filesPathArray);
async.each(rootFiles, (rootFile, callback) => {
loader
// will merge all the files in the folder
.loadSpec(rootFile, loaderOptions)
.then((spec) => {
converter.convert(spec, options, (err, result) => {
if (err) {
return callback(null, {
result: false,
reason: err
});
}
// eslint-disable-next-line no-else-return
else {
convertedSpecs.push(result);
return callback(null);
}
});
})
.catch((err) => {
return callback(null, {
result: false,
reason: err
});
});
}, (err) => {
if (err) {
return cb(null, {
result: false,
reason: _.toString(err.reason)
});
}
var conversionResult = false,
convertedCollections = [],
reasonForFail;
_.forEach(convertedSpecs, (convertedSpec) => {
if (convertedSpec.result) {
conversionResult = convertedSpec.result;
convertedCollections.push(convertedSpec.output[0]);
}
else {
conversionResult = convertedSpec.result;
reasonForFail = convertedSpec.reason;
}
});
if (conversionResult) {
return cb(null, {
result: true,
output: convertedCollections
});
}
// eslint-disable-next-line no-else-return
else {
return cb(null, {
result: false,
reason: reasonForFail
});
}
});
}
// eslint-disable-next-line no-else-return
else {
return cb(null, {
result: false,
reason: 'input type:' + input.type + ' is not valid'
});
}
},
validate: function(input) {