Skip to content

Commit 52dd251

Browse files
committed
make plugin configurable, improve component path resolving
1 parent 2b32447 commit 52dd251

File tree

4 files changed

+93
-50
lines changed

4 files changed

+93
-50
lines changed

README.md

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# SourceJS auto React doc builder middleware.
1+
# SourceJS Auto React Doc Builder Middleware
22

3-
[react-docgen](https://github.com/reactjs/react-docgen) integration plugin, renders React components information into [SourceJS](http://sourcejs.com) Spec page.
3+
[react-docgen](https://github.com/reactjs/react-docgen) integration plugin, renders React components information into [SourceJS](http://sourcejs.com) Spec page.
44

55
Compatible with [SourceJS](http://sourcejs.com) 0.6.0+.
66

@@ -16,16 +16,16 @@ After restarting your app, middleware will be loaded automatically. To disable i
1616

1717
## Usage
1818

19-
After installing the middleware, during spec load, plugin will try to find `<specPath>/index.jsx` or `<specPath>/src/index.jsx` file, analyze it and expose raw and rendered data objects. Data will be then available within [EJS Spec pre-rendering](http://sourcejs.com/docs/spec-helpers/#native-templating) (enabled by default).
19+
After installing the middleware, during SourceJS Spec load plugin will try to find first `<specPath>/*.jsx` file, analyze it and expose raw and rendered into HTML data objects. Data will be then available within [EJS Spec pre-rendering](http://sourcejs.com/docs/spec-helpers/#native-templating).
2020

21-
Insert these code snippets anywhere you want in your Spec file
21+
Insert these code snippets anywhere you want in your Spec file:
2222

2323
```html
2424
<h1>My Spec</h1>
2525

2626
<section class="source_section">
2727
<h2>Default Example</h2>
28-
28+
2929
<p><%- info.__docGenRaw.description %></p>
3030

3131
<%- info.__docGenHTML %>
@@ -34,13 +34,30 @@ Insert these code snippets anywhere you want in your Spec file
3434
</section>
3535
```
3636

37-
Other custom Spec file syntax with [sourcejs-react](https://github.com/szarouski/sourcejs-react) and [sourcejs-md-react](https://github.com/mik01aj/sourcejs-md-react) plugins is also supported.
37+
# My Spec
38+
39+
## Default Example
40+
41+
<%- info.__docGenRaw.description %>
42+
43+
<%- info.__docGenHTML %>
44+
45+
```example
46+
code
47+
```
3848

39-
Check usage examples in [react-styleguide-example](https://github.com/sourcejs/react-styleguide-example).
49+
Other custom Spec file syntax options like [sourcejs-react](https://github.com/szarouski/sourcejs-react) and [sourcejs-md-react](https://github.com/mik01aj/sourcejs-md-react) plugins are also supported.
4050

41-
### Define custom path to JSX file
51+
Check usage examples in [react-styleguide-example](https://github.com/sourcejs/react-styleguide-example) and [react-styleguidist-example](https://github.com/sourcejs/react-styleguidist-example).
4252

43-
Using `info.json`, it's possible to define custom path to React component, apart from default `src/index.jsx`:
53+
### EJS exposed data
54+
55+
* **info.__docGenRaw** - raw JSON from react-docgen
56+
* **info.__docGenHTML** - rendered table with properties
57+
58+
## Configuration
59+
60+
Using Spec's `info.json` file, it's possible to define custom path to React component:
4461

4562
```
4663
{
@@ -49,14 +66,40 @@ Using `info.json`, it's possible to define custom path to React component, apart
4966
}
5067
```
5168

52-
### Exposed Data
69+
Or overriding global plugin configuration:
5370

54-
* **info.__docGenRaw** - raw JSON from react-docgen
55-
* **info.__docGenHTML** - rendered table with properties
71+
```javascript
72+
module.exports = {
73+
plugins: {
74+
reactDocgen: {
75+
componentPath: 'custom/path/index.jsx',
76+
}
77+
}
78+
};
79+
```
80+
81+
See other configuration options below.
82+
83+
### enabled
84+
85+
Default: true
86+
Type: _boolean_
87+
88+
89+
Set `false` to disable middleware.
90+
91+
### componentPath
92+
93+
Default: '*.jsx'
94+
Type: _string_
95+
96+
Define custom path to component entry file. Accepts [glob](https://github.com/isaacs/node-glob) string, which will be resolved relatively to spec path (takes only first found file).
97+
98+
## TODO:
5699

57-
## TODO
100+
* Add auto-append option
58101

59-
* Add more configuration options
102+
Pull request highly welcome!
60103

61104
## Other SourceJS Middlewares
62105

core/middleware/index.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@ var reactDocgen = require('react-docgen');
22
var path = require('path');
33
var fs = require('fs');
44
var ejs = require('ejs');
5+
var glob = require("glob");
56
var specUtils = require(path.join(global.pathToApp,'core/lib/specUtils'));
67
var currentDir = path.dirname(__filename);
8+
var sourceJSUtils = require(path.join(global.pathToApp, 'core/lib/utils'));
79

810
// Module configuration
911
var globalConfig = global.opts.plugins && global.opts.plugins.reactDocgen ? global.opts.plugins.reactDocgen : {};
1012
var config = {
1113
enabled: true,
14+
componentPath: '*.jsx',
1215

1316
// Public object is exposed to Front-end via options API.
1417
public: {}
1518
};
19+
sourceJSUtils.extendOptions(config, globalConfig);
1620

1721
/*
1822
* @param {object} req - Request object
@@ -27,41 +31,36 @@ var processRequest = function (req, res, next) {
2731

2832
// Check if request is targeting Spec
2933
if (req.specData && req.specData.renderedHtml) {
30-
var componentInfo = {};
34+
var componentDocs = {};
3135
var error = false;
3236
var specPath = specUtils.getFullPathToSpec(req.path);
33-
var componentPath = path.join(specPath, 'src/index.jsx');
34-
var componentPathAlt = path.join(specPath, 'index.jsx');
35-
var componentPathExists = fs.existsSync(componentPath);
36-
var componentPathAltExists = fs.existsSync(componentPathAlt);
37-
var componentPathToUse;
37+
var componentPathToUse = req.specData.info.main || config.componentPath;
3838

39-
if (!(componentPathExists || componentPathAltExists)) {
39+
var componentFilePath = glob.sync(componentPathToUse, {
40+
cwd: specPath,
41+
realpath: true
42+
})[0];
43+
44+
if (!fs.existsSync(componentFilePath)) {
4045
next();
4146
return;
4247
}
4348

44-
if (req.specData.info.main && fs.existsSync(path.join(specPath, req.specData.info.main))) {
45-
componentPathToUse = path.join(specPath, req.specData.info.main);
46-
} else {
47-
componentPathToUse = fs.existsSync(componentPath) ? componentPath : componentPathAlt;
48-
}
49-
50-
var componentContent = fs.readFileSync(componentPathToUse, 'utf-8');
49+
var componentContent = fs.readFileSync(componentFilePath, 'utf-8');
5150
var propsTpl = fs.readFileSync(path.join(currentDir, '../templates/props.ejs'), 'utf-8');
5251

5352
try {
54-
componentInfo = reactDocgen.parse(componentContent);
53+
componentDocs = reactDocgen.parse(componentContent);
5554
} catch(e) {
5655
error = true;
5756
console.warn('sourcejs-react-docgen: error generating component doc', e);
5857
}
5958

60-
req.specData.info.__docGenRaw = componentInfo;
59+
req.specData.info.__docGenRaw = componentDocs;
6160

6261
if (!error) {
6362
try {
64-
req.specData.info.__docGenHTML = ejs.render(propsTpl, componentInfo);
63+
req.specData.info.__docGenHTML = ejs.render(propsTpl, componentDocs);
6564
} catch(e) {
6665
console.warn('sourcejs-react-docgen: error rendering docgen props', e);
6766
}

core/templates/props.ejs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
<% if (typeof props !== 'undefined') { %>
2-
<table>
2+
<table>
3+
<tr>
4+
<td>Property name</td>
5+
<td>Type</td>
6+
<td>Required</td>
7+
<td>Default value</td>
8+
<td>Description</td>
9+
</tr>
10+
<% for(var key in props) { %>
11+
<% var val = props[key] %>
312
<tr>
4-
<td>Property name</td>
5-
<td>Type</td>
6-
<td>Required</td>
7-
<td>Default value</td>
8-
<td>Description</td>
13+
<td><%- key %></td>
14+
<td><%- val.type ? val.type.name : '-' %></td>
15+
<td><%- val.required %></td>
16+
<td><%- val.defaultValue ? val.defaultValue.value.replace(/'/g, '').replace(/"/g, '') : '-' %></td>
17+
<td><%- val.description %></td>
918
</tr>
10-
<% for(var key in props) { %>
11-
<% var val = props[key] %>
12-
<tr>
13-
<td><%- key %></td>
14-
<td><%- val.type.name %></td>
15-
<td><%- val.required %></td>
16-
<td><%- val.defaultValue.value %></td>
17-
<td><%- val.description %></td>
18-
</tr>
19-
<% } %>
20-
</table>
19+
<% } %>
20+
</table>
2121
<% } else { %>
2222
No props defined in component.
2323
<% } %>

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sourcejs-react-docgen",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"description": "SourceJS auto React doc builder middleware.",
55
"repository": {
66
"type": "git",
@@ -9,6 +9,7 @@
99
"homepage": "https://github.com/sourcejs/sourcejs-react-docgen",
1010
"dependencies": {
1111
"ejs": "^2.3.4",
12-
"react-docgen": "^2.2.0"
12+
"glob": "^6.0.1",
13+
"react-docgen": "^2.4.0"
1314
}
1415
}

0 commit comments

Comments
 (0)