Skip to content

Commit

Permalink
parse swagger, write to file, save api
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfred J. Kwak committed Feb 12, 2017
1 parent 394a99b commit 34c770c
Showing 1 changed file with 64 additions and 10 deletions.
74 changes: 64 additions & 10 deletions generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,84 @@ var Generator = require('yeoman-generator');
var chalk = require('chalk');
var yosay = require('yosay');

var fs = require('fs');
var http = require('http');
var https = require('https');
var URL = require('url-parse');
var SwaggerParser = require('swagger-parser');

module.exports = Generator.extend({
prompting: function () {
// Have Yeoman greet the user.
this.log(yosay(
'Welcome to the first-rate ' + chalk.red('generator-restivus') + ' generator!'
'Welcome to use the official ' + chalk.yellow('Restivus API') + ' generator!'
));

var prompts = [{
type: 'confirm',
name: 'someAnswer',
message: 'Would you like to enable this option?',
default: true
type: 'input',
name: 'swaggerURL',
message: 'Please type URL to Swagger spec:'
}];

return this.prompt(prompts).then(function (props) {
// To access props later use this.props.someAnswer;
this.props = props;
}.bind(this));
},

parsing: function () {
// Init variables
var generator = this;
var done = this.async();
var log = this.log;
var swaggerURL = this.props.swaggerURL;
SwaggerParser.validate(swaggerURL, function (err, swaggerApi) {
if (err) {
log(err);
done(err);
} else {
// Bind parsed swaggerApi to Generator instance
var api = generator.api = swaggerApi;
log('API name: %s, Version: %s', api.info.title, api.info.version);
done();
}
});
},
writing: function () {
this.fs.copy(
this.templatePath('dummyfile.txt'),
this.destinationPath('dummyfile.txt')
);
// Init variables
var done = this.async();
var log = this.log;
var swaggerURL = this.props.swaggerURL;
var swaggerFilePath = this.destinationPath('spec/swagger.json');
var swaggerFileStream;

// Make missing folders
fs.mkdir('spec', function (err) {
if (err) {
log('Unable to create folder');
done(err);
} else {
swaggerFileStream = fs.createWriteStream(swaggerFilePath);
// Parse URL
var url = new URL(swaggerURL);
// Copy original spec from URL
if (url.protocol === 'https:') {
https.get(swaggerURL, function (response) {
response.pipe(swaggerFileStream);
});
} else if (url.protocol === 'http:') {
http.get(swaggerURL, function (response) {
response.pipe(swaggerFileStream);
});
} else {
log('Please provide proper URL (http/https).');
}
done();
}
});
},
end: function () {
this.log(yosay(
chalk.yellow('Did all the hard work, phew!\nGoodbye.')
));
}
});

0 comments on commit 34c770c

Please # to comment.