-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
executable file
·61 lines (49 loc) · 1.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env node
'use strict';
const raml2obj = require('raml2obj');
const pjson = require('./package.json');
const nunjucks = require('nunjucks');
/*
The config object can contain the following keys and values:
template: url to the main template (required)
templatesPath: a folder containing the templates (optional, by default it's relative to your working directory)
processOutput: function that takes data, return a promise (optional)
*/
function render(source, config) {
const env = nunjucks.configure(config.templatesPath, { autoescape: false });
config = config || {};
config.raml2MdVersion = pjson.version;
return raml2obj.parse(source).then((ramlObj) => {
ramlObj.config = config;
return new Promise((resolve) => {
const result = env.render(config.template, ramlObj);
if (config.processOutput) {
resolve(config.processOutput(result));
}
return resolve(result);
});
});
}
function getDefaultConfig(mainTemplate, templatesPath) {
if (!mainTemplate) {
mainTemplate = './lib/template.nunjucks';
// When using the default template, make sure that Nunjucks isn't
// using the working directory since that might be anything
templatesPath = __dirname;
}
return {
template: mainTemplate,
templatesPath,
processOutput(data) {
return data.replace(/\n{3,}/g, '\n\n');
},
};
}
if (require.main === module) {
console.log('This script is meant to be used as a library. You probably want to run bin/raml2md if you\'re looking for a CLI.');
process.exit(1);
}
module.exports = {
getDefaultConfig,
render,
};