forked from carbon-design-system/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
122 lines (106 loc) · 3.84 KB
/
server.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
'use strict';
const globby = require('globby');
const { promisify } = require('bluebird');
const fs = require('fs');
const path = require('path');
const express = require('express');
const Fractal = require('@frctl/fractal');
const webpack = require('webpack');
const webpackDevConfig = require('./tools/webpack.dev.config');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const compiler = webpack(webpackDevConfig);
const readFile = promisify(fs.readFile);
const app = express();
const adaro = require('adaro');
const port = process.env.PORT || 8080;
app.use(webpackDevMiddleware(compiler, { noInfo: true, publicPath: webpackDevConfig.output.publicPath }));
app.use(webpackHotMiddleware(compiler));
const fractal = Fractal.create();
fractal.components.set('path', path.join(__dirname, 'src/components'));
fractal.components.set('ext', '.html');
fractal.docs.set('path', path.join(__dirname, 'docs'));
app.engine('dust', adaro.dust());
app.set('view engine', 'dust');
app.set('views', path.resolve(__dirname, 'demo/views'));
app.use('/demo', express.static('demo'));
app.use(express.static('src'));
app.use(express.static('scripts'));
app.use('/docs/js', express.static('docs/js'));
/**
* @param {string} glob The glob.
* @returns {string} The file contents of files matching the given glob, concatenated.
*/
const getContent = glob =>
globby(glob).then(filePaths => {
if (filePaths.length === 0) {
return undefined;
}
return Promise.all(filePaths.map(filePath => readFile(filePath, { encoding: 'utf8' }))).then(contents =>
contents.reduce((a, b) => a.concat(b))
);
});
/**
* @param {ComponentCollection|Component} item The component data.
* @returns {Promise<ComponentCollection|Component>}
* The component data, with README.md content assigned to `.notes` property for component with variants (`ComponentCollection`).
* Fractal automatically populate `.notes` for component without variants (`Component`).
*/
const ensureComponentItemNotes = item => {
if (!item.isCollection || !item.config.readme) {
return item;
}
return item.config.readme
.getContent()
.then(notes => Object.assign(typeof item.toJSON !== 'function' ? item : item.toJSON(), { notes }));
};
['/', '/demo/:component'].forEach(route => {
app.get(route, (req, res) => {
const name = req.params.component;
if (name && path.relative('src/components', `src/components/${name}`).substr(0, 2) === '..') {
res.status(404).end();
} else {
fractal
.load()
.then(([componentSource, docSource]) =>
Promise.all([Promise.all(componentSource.items().map(ensureComponentItemNotes)), docSource.items()])
)
.then(([componentItems, docItems]) => {
res.render('demo-nav', {
componentItems,
docItems,
});
})
.catch(err => {
console.error(err.stack); // eslint-disable-line no-console
res.status(500).end();
});
}
});
});
['/component/:component', '/component/:component/:variant'].forEach(route => {
app.get(route, (req, res) => {
const glob = `src/components/${req.params.component}/**/${req.params.variant || '*'}.html`;
if (path.relative('src/components', glob).substr(0, 2) === '..') {
res.status(404).end();
} else {
getContent(glob)
.then(html => {
if (typeof html === 'undefined') {
res.status(404).end();
} else {
res.render('demo-live', {
content: html,
});
}
})
.catch(error => {
console.error(error.stack); // eslint-disable-line no-console
res.status(500).end();
});
}
});
});
app.listen(port, () => {
console.log(`Listening on port: ${port}`); // eslint-disable-line no-console
});