forked from HospitalRun/components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildDocs.sh
49 lines (43 loc) · 1.29 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
#!/usr/bin/env node
/**
* This example script expects a JSON blob generated by react-docgen as input,
* e.g. react-docgen components/* | buildDocs.sh
*/
var fs = require('fs');
var generateMarkdown = require('./generateMarkdown');
var path = require('path');
var json = '';
process.stdin.setEncoding('utf8');
process.stdin.on('readable', function() {
var chunk = process.stdin.read();
if (chunk !== null) {
json += chunk;
}
});
process.stdin.on('end', function() {
buildDocs(JSON.parse(json));
});
function buildDocs(api) {
// api is an object keyed by filepath. We use the file name as component name.
for (var filepath in api) {
var name = getComponentName(filepath);
var markdown = generateMarkdown(name, api[filepath]);
const path = process.cwd() + '/docs/';
fs.mkdirSync(path, { recursive: true });
fs.writeFileSync(path + name + '.md', markdown);
process.stdout.write(filepath + ' -> ' + path + name + '.md\n');
}
}
function getComponentName(filepath) {
var name = path.basename(filepath);
// check for index.js
if (name === 'index.js') {
const dirs = path.dirname(filepath).split('/');
name = dirs[dirs.length - 1];
}
var ext;
while ((ext = path.extname(name))) {
name = name.substring(0, name.length - ext.length);
}
return name;
}