-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
101 lines (99 loc) · 2.3 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
'use strict';
const getValueInfo = require('./lib/getValueInfo');
const _ = require('lodash');
const htmlTableify = require('html-tableify');
const markdownTableify = require('markdown-tableify');
const cliTableify = require('cli-table2');
const DEFAULT_MARKDOWN_CONFIG = {
type: 'markdown',
afterParse: () => {},
beforeGenerate: () => {},
headers: [{
name: 'name',
align: ':---'
}, {
name: 'description',
align: ':-----'
}, {
name: 'type',
align: ':---'
}, {
name: 'required',
align: ':---:'
}, {
name: 'defaultValue',
align: ':---:',
title: 'Default Value'
}]
};
const DEFAULT_HTML_CONFIG = {
type: 'html',
tidy: true,
afterParse: () => {},
beforeGenerate: () => {},
headers: [{
name: 'name',
align: 'left'
}, {
name: 'description',
align: 'left'
}, {
name: 'type',
align: 'center'
}, {
name: 'required',
align: 'center'
}, {
name: 'defaultValue',
align: 'center',
title: 'Default Value'
}]
};
const DEFAULT_CLI_CONFIG = {
type: 'cli',
headers: [{
name: 'name',
width: 20,
align: 'left'
}, {
name: 'description',
width: 30,
align: 'left'
}, {
name: 'type',
width: 20,
align: 'center'
}, {
name: 'required',
width: 10,
align: 'center'
}, {
name: 'defaultValue',
width: 20,
title: 'Default Value',
align: 'center'
}],
afterParse: () => {},
beforeGenerate: () => {}
};
module.exports = {
html: (source, config) => {
config = _.defaults(config || {}, DEFAULT_HTML_CONFIG);
return htmlTableify(getValueInfo(source, config), config);
},
markdown: (source, config) => {
config = _.defaults(config || {}, DEFAULT_MARKDOWN_CONFIG);
return markdownTableify(getValueInfo(source, config), config);
},
cli: (source, config) => {
config = _.defaults(config || {}, DEFAULT_CLI_CONFIG);
let table = new cliTableify({
head: config.headers.map(header => header.title || _.capitalize(header.name)),
colWidths: config.headers.map(header => header.width || 200),
colAligns: config.headers.map(header => header.align || 'left'),
wordWrap: true
});
getValueInfo(source, config).forEach(row => table.push(config.headers.map(header => row[header.name] || '')));
return table.toString();
}
};