forked from microsoft/fast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert-readme.js
79 lines (66 loc) · 1.88 KB
/
convert-readme.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
/**
* Utility for converting README files to .tsx files.
* Usage: node build/convert-readme.js %path%
*/
const path = require("path");
const glob = require("glob");
const fs = require("fs");
const argv = require("yargs").argv;
const MarkdownIt = require("markdown-it");
const srcDir = argv.src || "./src/**/README.md";
/**
* Start and end file strings
*/
const startFile = `// Generated file from ../../build
import React from "react";
export default class Documentation extends React.Component<{}, {}> {
public render(): JSX.Element {
return (
<React.Fragment>\n `;
const endFile = `\n </React.Fragment>
);
}
}\n`;
const emptyFile = `<p>No documentation provided.</p>`;
const md = new MarkdownIt({
html: true,
linkify: true,
typographer: true,
xhtmlOut: true,
});
/**
* All paths passed to the convert script
*/
const paths = argv._;
/**
* Function to create string exports of a given path
*/
function exportReadme(readmePath) {
const readmePaths = path.resolve(process.cwd(), srcDir);
glob(readmePaths, void 0, function (error, files) {
files.forEach(filePath => {
let documentation = startFile;
const markdown = fs.readFileSync(filePath, "utf8");
const exportPath = filePath.replace(/README\.md/, readmePath);
if (markdown.length !== 0) {
documentation += md.render(markdown);
} else {
documentation += emptyFile;
}
documentation += endFile;
if (!fs.existsSync(exportPath)) {
fs.mkdirSync(exportPath);
}
fs.writeFileSync(
path.resolve(exportPath, "documentation.tsx"),
documentation
);
});
});
}
/**
* Convert all paths
*/
if (Array.isArray(paths)) {
paths.forEach(exportReadme);
}