-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
verbfile.js
205 lines (178 loc) · 5.39 KB
/
verbfile.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
'use strict';
const fs = require('fs');
const path = require('path');
const link = require('markdown-link');
const through = require('through2');
const cache = {};
/**
* TODO: externalize to `verb-generate-toc`
*/
module.exports = function(verb, base, env) {
verb.on('error', console.log);
verb.use(require('verb-generate-readme'));
verb.helpers(require('./')());
helpers(verb);
verb.preRender(/\.md$/, function(file, next) {
file.options.stripEmpty = false;
next();
});
verb.task('toc', function(cb) {
let total = { categories: 0, helpers: 0 };
let sections = [];
let summary = [];
let methods = {};
let toc = [];
return verb.src('lib/helpers/*.js')
.pipe(through.obj(function(file, enc, next) {
if (file.stem === 'index') {
next();
return;
}
try {
file.base = verb.cwd;
file.code = require(file.path);
let heading = listItem(file);
let newFile = {
methods: require(file.path),
test: {
path: path.join('test', file.basename),
code: {}
},
code: {},
path: file.relative,
base: file.base,
cwd: file.cwd,
relative: file.relative,
stem: file.stem || path.basename(file.path, path.extname(filepath)),
data: {
methods: {}
}
};
let testLine = matchTest(newFile.test.path);
let codeLine = matchCode(newFile.path);
for (let key in newFile.methods) {
total.helpers++;
if (newFile.methods.hasOwnProperty(key)) {
newFile.data.methods[key] = {
method: newFile.stem,
stem: key,
code: {
path: newFile.relative,
line: codeLine(key)
},
test: {
path: newFile.test.path,
line: testLine(key)
}
};
}
}
methods[newFile.stem] = newFile;
total.categories++;
next();
} catch (err) {
next(err);
}
}, function(next) {
verb.data({ total });
verb.data({ methods });
next();
}));
});
verb.task('sectionToc', function() {
let toc = '';
return verb.src('lib/helpers/*.js')
.pipe(through.obj(function(file, enc, next) {
if (file.stem !== 'index') {
file.base = verb.cwd;
toc += '- ' + link(file.stem, '#' + file.stem) + ' ';
toc += '(code '+ link(file.stem, file.relative) + ')\n';
}
next();
}, function(next) {
// create an `include` from the toc
verb.include('toc.md', { contents: Buffer.from(toc) });
next();
}))
.pipe(verb.dest('.'));
});
verb.task('fix-headings', function() {
return verb.src('README.md')
.pipe(through.obj(function(file, enc, next) {
let str = file.contents.toString();
str = str.replace(/\n### index/, '');
str = str.replace(/^(#{2,}\s*\[)\./gm, '$1');
file.contents = Buffer.from(str);
next(null, file);
}))
.pipe(verb.dest('.'));
});
verb.task('default', ['toc', 'readme', 'fix-headings']);
};
function section(name) {
return `## ${name}\n{%= apidocs("lib/helpers/${name}.js") %}`;
}
function matchTest(fp) {
if (!fp || !fs.existsSync(fp)) return function() {};
let str = fs.readFileSync(fp, 'utf8');
let lines = str.split('\n');
return function(method) {
let re = new RegExp('\\s*(describe|it)\\([\'"]\\.?(' + method + ')');
let len = lines.length, i = -1;
while (++i < len) {
let line = lines[i];
if (re.test(line)) {
return i + 1;
}
}
return;
};
}
function matchCode(fp) {
if (!fp || !fs.existsSync(fp)) return function() {};
let str = fs.readFileSync(fp, 'utf8');
let lines = str.split('\n');
return method => {
let re = new RegExp(`^exports\\.(${method})`, 'gm');
let len = lines.length, i = -1;
while (++i < len) {
let line = lines[i];
if (re.test(line)) {
return i + 1;
}
}
};
}
function helpers(app) {
app.helper('bullet', function(file) {
return '- **' + link(file.stem, '#' + file.stem) + '**';
});
app.helper('issue', function(name) {
let repo = app.cache.data.repository;
return '[create issue](https://github.com/' + repo + '/issues/new?title=' + name + '%20helper)';
});
app.helper('sectionIssue', function(section) {
let repo = app.cache.data.repository;
let url = 'https://github.com/' + repo + '/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+';
return '[issues](' + url + section + '+helpers)';
});
app.helper('anchor', function(file) {
return link(file.stem, '#' + file.stem);
});
app.helper('codeLink', function(file) {
return codeLink('code', file.code.path, file.code.line);
});
app.helper('testLink', function(file) {
let line = file.test.line;
return line ? codeLink('unit tests', file.test.path, line) : '[no tests]';
});
app.helper('link', function(name, filepath) {
return link(name, filepath);
});
}
function listItem(file) {
return '- **' + link(file.stem, '#' + file.stem) + '** (code ' + link(file.stem, file.relative) + ')';
}
function codeLink(title, path, start) {
return link(title, path + '#L' + start);
}