This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
collect.js
42 lines (35 loc) · 1.17 KB
/
collect.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
var path = require('path');
var fs = require('fs');
/**
* Takes a source file, and does some replacements to
* make the code examples be more useful
*
* @param {string} contents Original file contents
* @return {string} Modified file contents
*/
function transformFile(contents) {
// Show prettier import paths
contents = contents.replace('../../../src/components/Modal', 'tg-modal');
return contents;
}
/**
* Collect all files from examples/components/examples and
* return them as {fName: "Modified source", ...}.
*
* This is used to automatically generate `view code` values
* when rendering the examples page.
*
* @return {Object} Example files as {fName: "Modified source", ...}.
*/
function collectExampleSource() {
var baseDir = path.join(__dirname, 'examples', 'components', 'examples');
var files = fs.readdirSync(baseDir);
var result = {};
files.forEach(function (fileName) {
if (/\.jsx?$/.test(fileName)) {
result[fileName.replace(/\.jsx?$/, '')] = transformFile(fs.readFileSync(path.join(baseDir, fileName), {encoding: 'utf-8'}));
}
});
return result;
}
module.exports = collectExampleSource;