Skip to content

Instantly share code, notes, and snippets.

@AnatolyRugalev
Created July 4, 2016 11:21
Show Gist options
  • Save AnatolyRugalev/bba1062c8258e719ad668b041ba24974 to your computer and use it in GitHub Desktop.
Save AnatolyRugalev/bba1062c8258e719ad668b041ba24974 to your computer and use it in GitHub Desktop.
var gulp = require('gulp');
var gutil = require('gulp-util');
var gaglio = require('gulp-aglio');
var aglio = require('aglio');
var fs = require('fs');
var request = require('request');
var apiaryApiKey = 'api-key';
var apiaryApiName = 'api-name';
gulp.task('default', ['html'], function () {
});
gulp.task('compile', function (callback) {
aglio.compileFile('api/main.apib', 'apiary.apib', function () {
console.log("Compiled apiary.apib");
callback();
});
});
gulp.task('html', ['compile'], function () {
gulp.src('apiary.apib')
.pipe(gaglio({template: 'default'}))
.on('error', gutil.log)
.pipe(gulp.dest('html'));
console.log("Rendered to html/apiary.html");
});
gulp.task('watch', ['html'], function () {
gulp.watch('api/**/*.apib', ['html']);
});
gulp.task('publish', ['compile'], function (callback) {
fs.readFile('apiary.apib', 'utf8', function (err, code) {
var body = {
code: code,
messageToSave: "Publish from gulp"
};
request({
method: 'POST',
url: 'http://api.apiary.io/blueprint/publish/' + apiaryApiName,
headers: {
'Accept': 'text/html',
'Content-Type': 'application/json',
'Authentication': 'Token ' + apiaryApiKey
},
body: JSON.stringify(body)
}, function (error, response, body) {
if (response.statusCode == 200 || response.statusCode == 201) {
console.log("Successfully published to apiary.io!");
console.log("http://docs." + apiaryApiName + ".apiary.io/");
callback();
} else {
console.warn("Publish error");
console.log('Status:', response.statusCode);
console.log('Headers:', JSON.stringify(response.headers));
console.log('Response:', body);
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment