Skip to content

Instantly share code, notes, and snippets.

@Bernardoow
Created January 18, 2019 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bernardoow/b69782807d521ba8d74ed1c90ad102cb to your computer and use it in GitHub Desktop.
Save Bernardoow/b69782807d521ba8d74ed1c90ad102cb to your computer and use it in GitHub Desktop.
TASK GULP raml2html
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.getConfigForTheme();
}
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.path, 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment