Skip to content

Instantly share code, notes, and snippets.

@alfredoem
Created January 5, 2017 23:26
Show Gist options
  • Save alfredoem/db6df471bb03c215fe68749859ccab4c to your computer and use it in GitHub Desktop.
Save alfredoem/db6df471bb03c215fe68749859ccab4c to your computer and use it in GitHub Desktop.
'use strict';
var gulp = require('gulp');
var path = require('path');
var livereload = require('gulp-livereload');
var API_HTML = 'index.html';
var API_DEST = './public/project/reference/v1';
function raml2html(options) {
var gutil = require('gulp-util');
var through = require('through2');
var raml2html = require('raml2html');
var simplifyMark = function(mark) {
if (mark) mark.buffer = mark.buffer.split('\n', mark.line + 1)[mark.line].trim();
}
options = options || {};
switch (options.type) {
case 'json':
var Q = require('q');
options.config = {processRamlObj: function(raml) { return Q.fcall(function() {
return JSON.stringify(raml, options.replacer, 'indent' in options ? options.indent : 2);
})}};
break;
case 'yaml':
var Q = require('q');
var yaml = require('js-yaml');
options.config = {processRamlObj: function(raml) { return Q.fcall(function() {
return yaml.safeDump(raml, {skipInvalid: true, indent: options.indent, flowLevel: options.flowLevel});
})}};
break;
default:
options.type = 'html';
options.config = options.config || raml2html.getDefaultConfig(options.template, options.templatePath);
}
var stream = through.obj(function(file, enc, done) {
var fail = function(message) {
done(new gutil.PluginError('raml2html', message));
};
if (file.isBuffer()) {
var cwd = process.cwd();
process.chdir(path.resolve(path.dirname(file.path)));
raml2html.render(file.contents, options.config).then(
function(output) {
process.chdir(cwd);
stream.push(new gutil.File({
base: file.base,
cwd: file.cwd,
path: gutil.replaceExtension(file.path, options.extension || '.' + options.type),
contents: new Buffer(output)
}));
done();
},
function(error) {
process.chdir(cwd);
simplifyMark(error.context_mark);
simplifyMark(error.problem_mark);
process.nextTick(function() {
fail(JSON.stringify(error, null, 2));
});
});
}
else if (file.isStream()) fail('Streams are not supported: ' + file.inspect());
else if (file.isNull()) fail('Input file is null: ' + file.inspect());
});
return stream;
}
function logErrorAndQuit(err) {
console.error(err.toString());
this.emit('end');
}
var documentation = function() {
var rename = require('gulp-rename');
return gulp.src('./resources/raml/api-aff.raml')
.pipe(raml2html())
.on('error', logErrorAndQuit)
.pipe(rename(API_HTML))
.pipe(gulp.dest(API_DEST));
};
gulp.task('api', function() {
return documentation();
});
// Gulp watch syntax
gulp.task('watch', function(){
livereload.listen();
documentation();
return gulp.watch(
['./resources/raml/*.*','./resources/raml/v1/**/*.*', './resources/raml/v1/**/**/*.*', './resources/raml/v1/**/**/**/*.*'],
['api', livereload.reload]
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment