forked from airbnb/visx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildDocs.sh
executable file
·55 lines (46 loc) · 1.56 KB
/
buildDocs.sh
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
#!/usr/bin/env node
const fs = require('fs');
const generateMarkdown = require('./genMarkdown');
const path = require('path');
let json = '';
process.stdin.setEncoding('utf8');
process.stdin.on('readable', function() {
let chunk = process.stdin.read();
if (chunk !== null) {
json += chunk;
}
});
process.stdin.on('end', function() {
buildDocs(JSON.parse(json));
});
function buildDocs(api) {
const dir = path.dirname(Object.keys(api)[0]);
const p = dir === '../src' ? '../../docs' : '../../../docs';
const docPath = path.resolve(`${Object.keys(api)[0]}`, p);
const toc = Object.keys(api)
.map(filepath => {
const name = getComponentName(filepath);
return ` - [${name}](#${name.toLowerCase()}-)`;
})
.join('\n');
const md = Object.keys(api).map(filepath => {
var name = getComponentName(filepath);
return generateMarkdown(name, api[filepath]);
});
const apiDocs = md.join('\n');
const install = fs.readFileSync(`${docPath}/install.md`, { encoding: 'utf-8' });
const description = fs.readFileSync(`${docPath}/description.md`, { encoding: 'utf-8' });
const docs = [description, install, '## Components\n\n', toc, '## API\n\n', apiDocs].join('\n\n');
fs.writeFileSync('api.md', apiDocs);
process.stdout.write(' -> ' + 'api.md\n');
fs.writeFileSync('docs.md', docs);
process.stdout.write(' -> ' + 'docs.md\n');
}
function getComponentName(filepath) {
let name = path.basename(filepath);
let ext;
while ((ext = path.extname(name))) {
name = name.substring(0, name.length - ext.length);
}
return name;
}