-
Notifications
You must be signed in to change notification settings - Fork 26
/
gen-test-docs.js
63 lines (54 loc) · 1.72 KB
/
gen-test-docs.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
#!/usr/bin/env node
/*
Usage:
1. Add source files for docs generation to `filesForGen` array. The order matters.
2. Set output file relative path in `outputFile`
3. Execute "node gen-test-docs.js"
*/
// Settings
const filesForGen = [
'test/ERC20Farmable.js',
'test/behaviors/ERC20Farmable.behavior.js',
'test/FarmingPool.js',
];
const outputMdFile = 'TESTS.md';
const includeCode = false;
const listOnly = false;
// Script
const acquitMd = require('acquit')();
const acquitJson = require('acquit')();
const fs = require('fs');
require('acquit-markdown')(acquitMd, { code: includeCode, it: true });
const legend = {};
let content;
let markdown = '';
let legendMd = '';
filesForGen.forEach((file) => {
content = fs.readFileSync(file).toString();
legend.blocks = acquitJson.parse(content);
legend.contents = file;
legendMd += buildLegend(legend, 1, listOnly);
markdown += acquitMd.parse(content).toString();
markdown += '\n';
});
content = listOnly ? legendMd : legendMd + markdown;
fs.writeFileSync(outputMdFile, content);
console.log('done');
function buildLegend (block, depth, listOnly) {
// console.log(depth, block.contents);
const url = (block.contents == null)
? ''
: block.contents.toLowerCase().trim()
.split(' ').join('-')
.split(/,|\+|\/|:|\(|\)/).join('')
.replace('--', '-');
let legend = listOnly
? Array(depth).join(' ') + '* ' + block.contents + '\n'
: Array(depth).join(' ') + '* [' + block.contents + '](#' + url + ')\n';
if (block.blocks) {
legend += block.blocks.map(function (child) {
return buildLegend(child, depth + 1, listOnly);
}).join('');
}
return legend;
}