-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
/
Copy pathbuildApi.js
196 lines (163 loc) · 5.39 KB
/
buildApi.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
/* eslint-disable no-console */
import { mkdir, readFileSync, writeFileSync } from 'fs';
import path from 'path';
import kebabCase from 'lodash/kebabCase';
import * as reactDocgen from 'react-docgen';
import generateMarkdown from '../src/modules/utils/generateMarkdown';
import { findPagesMarkdown, findComponents } from '../src/modules/utils/find';
import { getHeaders } from '../src/modules/utils/parseMarkdown';
import createMuiTheme from '../../packages/material-ui/src/styles/createMuiTheme';
import getStylesCreator from '../../packages/material-ui/src/styles/getStylesCreator';
function ensureExists(pat, mask, cb) {
mkdir(pat, mask, err => {
if (err) {
if (err.code === 'EEXIST') {
cb(null); // ignore the error if the folder already exists
} else {
cb(err); // something else went wrong
}
} else {
cb(null); // successfully created folder
}
});
}
// Read the command-line args
const args = process.argv;
// Exit with a message
function exit(error) {
console.log(error, '\n');
process.exit();
}
if (args.length < 4) {
exit('\nERROR: syntax: buildApi source target');
}
const rootDirectory = path.resolve(__dirname, '../../');
const docsApiDirectory = path.resolve(rootDirectory, args[3]);
const theme = createMuiTheme();
const inheritedComponentRegexp = /\/\/ @inheritedComponent (.*)/;
function getInheritance(src) {
const inheritedComponent = src.match(inheritedComponentRegexp);
if (!inheritedComponent) {
return null;
}
const component = inheritedComponent[1];
let pathname;
switch (component) {
case 'Transition':
pathname = 'https://reactcommunity.org/react-transition-group/#Transition';
break;
case 'EventListener':
pathname = 'https://github.com/oliviertassinari/react-event-listener';
break;
default:
pathname = `/api/${kebabCase(component)}`;
break;
}
return {
component,
pathname,
};
}
function buildDocs(options) {
const { component: componentObject, pagesMarkdown } = options;
const src = readFileSync(componentObject.filename, 'utf8');
if (src.match(/@ignore - internal component\./) || src.match(/@ignore - do not document\./)) {
return;
}
// eslint-disable-next-line global-require, import/no-dynamic-require
const component = require(componentObject.filename);
const name = path.parse(componentObject.filename).name;
const styles = {
classes: [],
name: null,
descriptions: {},
};
if (component.styles && component.default.options) {
// Collect the customization points of the `classes` property.
styles.classes = Object.keys(getStylesCreator(component.styles).create(theme)).filter(
className => !className.match(/^(@media|@keyframes)/),
);
styles.name = component.default.options.name;
let styleSrc = src;
// Exception for Select where the classes are imported from NativeSelect
if (name === 'Select') {
styleSrc = readFileSync(
componentObject.filename.replace('Select/Select', 'NativeSelect/NativeSelect'),
'utf8',
);
}
/**
* Collect classes comments from the source
*/
const stylesRegexp = /export const styles.*\n(.*\n)*};\n\n/;
const styleRegexp = /\/\* (.*) \*\/\n\s*(\w*)/g;
// Extract the styles section from the source
const stylesSrc = stylesRegexp.exec(styleSrc);
if (stylesSrc) {
// Extract individual classes and descriptions
stylesSrc[0].replace(styleRegexp, (match, desc, key) => {
styles.descriptions[key] = desc;
});
}
}
let reactAPI;
try {
reactAPI = reactDocgen.parse(src);
} catch (err) {
console.log('Error parsing src for', componentObject.filename);
throw err;
}
reactAPI.name = name;
reactAPI.styles = styles;
reactAPI.pagesMarkdown = pagesMarkdown;
reactAPI.src = src;
// if (reactAPI.name !== 'Snackbar') {
// return;
// }
// Relative location in the file system.
reactAPI.filename = componentObject.filename.replace(rootDirectory, '');
reactAPI.inheritance = getInheritance(src);
let markdown;
try {
markdown = generateMarkdown(reactAPI);
} catch (err) {
console.log('Error generating markdown for', componentObject.filename);
throw err;
}
ensureExists(docsApiDirectory, 0o744, err => {
if (err) {
console.log('Error creating directory', docsApiDirectory);
return;
}
writeFileSync(path.resolve(docsApiDirectory, `${kebabCase(reactAPI.name)}.md`), markdown);
writeFileSync(
path.resolve(docsApiDirectory, `${kebabCase(reactAPI.name)}.js`),
`import React from 'react';
import withRoot from 'docs/src/modules/components/withRoot';
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs';
import markdown from './${kebabCase(reactAPI.name)}.md';
function Page() {
return <MarkdownDocs markdown={markdown} />;
}
export default withRoot(Page);
`,
);
console.log('Built markdown docs for', reactAPI.name);
});
}
function run() {
const pagesMarkdown = findPagesMarkdown()
.map(markdown => {
const markdownSource = readFileSync(markdown.filename, 'utf8');
return {
...markdown,
components: getHeaders(markdownSource).components,
};
})
.filter(markdown => markdown.components.length > 0);
const components = findComponents(path.resolve(rootDirectory, args[2]));
components.forEach(component => {
buildDocs({ component, pagesMarkdown });
});
}
run();