-
Notifications
You must be signed in to change notification settings - Fork 65
/
index.js
executable file
·59 lines (48 loc) · 1.63 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
#!/usr/bin/env node
"use strict";
var fs = require('fs'),
ejs = require('ejs'),
_ = require('underscore');
var argv = require('optimist')
.usage('Generate documentation from apidoc data.\nUsage: apidoc-markdown -p [path] -o [output file]')
.demand(['path', 'output'])
.alias({
'path': 'p',
'output': 'o',
'template': 't'
})
.describe({
'path': 'Path to generated apidoc output. Where api_data.json & api_project.json resides.',
'output': 'Output file to write.',
'template': 'Path to EJS template file, if not specified default template will be used.',
'prepend': 'Prepend file after TOC.'
}).argv;
ejs.filters.undef = function (obj) {
return obj ? obj : '';
};
ejs.filters.mlink = function (obj) {
return (obj || '').toLowerCase().replace(/\s/g, '-');
};
var tmplFile = argv.template ? argv.template : __dirname + '/templates/default.md',
apiData = JSON.parse(fs.readFileSync(argv.path + '/api_data.json')),
projData = JSON.parse(fs.readFileSync(argv.path + '/api_project.json')),
template = ejs.compile(fs.readFileSync(tmplFile).toString());
apiData = _.filter(apiData, function (entry) {
return entry.type;
});
var apiByGroup = _.groupBy(apiData, function (entry) {
return entry.group;
});
var apiByGroupAndName = {};
Object.keys(apiByGroup).forEach(function (key) {
apiByGroupAndName[key] = _.groupBy(apiByGroup[key], function (entry) {
return entry.name;
});
});
var data = {
project: projData,
data: apiByGroupAndName
};
data.prepend = argv.prepend ? fs.readFileSync(argv.prepend).toString() : null;
fs.writeFileSync(argv.output, template(data));
console.log('Wrote apidoc-markdown template output to: ' + argv.output);