Skip to content

Commit

Permalink
Switch to swagger-inline for finding Swagger files
Browse files Browse the repository at this point in the history
  • Loading branch information
gkoberger committed Oct 2, 2016
1 parent 078b03f commit 5bc4ec6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
4 changes: 3 additions & 1 deletion api.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ exports.api = function(args, opts) {
return;
}

if(!swagger) {
if(!file) {
console.log("We couldn't find a Swagger file. Let's set one up!");
// TODO: Help them set it up
return; // TODO: This is wrong
Expand Down Expand Up @@ -75,6 +75,8 @@ exports.api = function(args, opts) {
}
}

utils.removeMetadata(swagger);

info.swagger = swagger;
actionObj.run(config, info);
});
Expand Down
35 changes: 15 additions & 20 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var _ = require('lodash');
var git = require('git-utils');
var swagger = require('swagger-parser');
var yaml = require('yamljs');
var swaggerInline = require('swagger-inline');

exports.config = function(env) {
var config = require('./config/' + (env || 'config'));
Expand All @@ -19,28 +20,22 @@ exports.config = function(env) {
exports.findSwagger = function(cb, opts) {
opts = opts || {};

var dir = opts.dir || process.cwd();
/*
// TODO: Maybe use the git root?
if(!dir) {
var repository = git.open(__dirname)
dir = repository.getWorkingDirectory();
}
*/

var files = fs.readdirSync(dir).filter((file) => {
if(!file.match(/\.(json|yaml)$/)) return false;
return exports.isSwagger(path.join(dir, file));
swaggerInline(['**/*.js'], { // TODO! Don't use just .js (Also... ignore node_modules, .git, etc?)
format: '.json',
metadata: true,
}).then((generatedSwagger) => {
generatedSwagger = JSON.parse(generatedSwagger);
cb(undefined, generatedSwagger, generatedSwagger['x-si-base']); // TODO! We need to fix the file!
});

// TODO: Give people an option if there's more than one Swagger file
if(files[0]) {
var file = path.join(dir, files[0]);
swagger.bundle(file, function(err, api) {
cb(err, api, file);
});
} else {
cb();
};

exports.removeMetadata = function(obj) {
for(prop in obj) {
if (prop.substr(0, 5) === 'x-si-')
delete obj[prop];
else if (typeof obj[prop] === 'object')
exports.removeMetadata(obj[prop]);
}
};

Expand Down

0 comments on commit 5bc4ec6

Please sign in to comment.