forked from yuristrelets/react-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildReadme.js
More file actions
executable file
·89 lines (71 loc) · 2.17 KB
/
buildReadme.js
File metadata and controls
executable file
·89 lines (71 loc) · 2.17 KB
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
import Fs from 'fs';
import GenerateMarkdown from './generateMarkdown';
import Glob from 'glob';
import Path from 'path';
import { parse as ParseComponent } from 'react-docgen';
import Promise from 'bluebird';
const getComponentName = (filepath) => {
let name = Path.basename(filepath);
let ext;
while ((ext = Path.extname(name))) {
name = name.substring(0, name.length - ext.length);
}
return name;
};
const componentGlob = Path.join(process.argv[2], '/*.js');
new Promise((resolve, reject) => {
Glob(componentGlob, (err, files) => {
if (err) {
return reject(err);
}
return resolve(files);
});
}).then(filePaths => {
const files = {};
filePaths.forEach(path => {
files[getComponentName(path)] = new Promise((resolveRead, rejectRead) => {
Fs.readFile(path, 'utf8', (err, data) => {
if (err) {
return rejectRead(err);
}
return resolveRead(data);
});
});
});
return Promise.props(files);
}).then(fulfillments => {
for (const componentName in fulfillments) {
fulfillments[componentName] = ParseComponent(fulfillments[componentName]);
}
return fulfillments;
}).then(fulfillments => {
let markdown = '';
for (const componentName in fulfillments) {
const reactAPI = fulfillments[componentName];
markdown = markdown
.concat(GenerateMarkdown(componentName, reactAPI))
.concat('\n\n------------------------------------------------------------------\n\n');
}
return markdown;
}).then(markdown => {
return new Promise((resolve, reject) => {
Fs.readFile(Path.join(process.argv[2], '/readme.md'), 'utf8', (err, contents) => {
if (err) {
return reject(err);
}
const contentLines = contents.split('\n');
const introLines = [];
contentLines.some(line => {
introLines.push(line);
if (line === '<!--component-docgen-start-->') {
introLines.push('\n');
return true;
}
});
const newMarkdown = introLines.join('\n').concat(markdown);
return resolve(newMarkdown);
});
});
}).then(newMarkdown => {
Fs.writeFileSync(Path.join(process.argv[2], '/readme.md'), newMarkdown);
});