1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | const { each, pairs } = require('ferrum');
|
13 | const stringify = require('remark-stringify');
|
14 | const unified = require('unified');
|
15 | const gfm = require('remark-gfm');
|
16 | const path = require('path');
|
17 | const fs = require('fs-extra');
|
18 | const yaml = require('js-yaml');
|
19 |
|
20 | function writemarkdown({
|
21 | out, error, meta,
|
22 | }) {
|
23 | const processor = unified()
|
24 | .use(gfm)
|
25 | .use(stringify);
|
26 |
|
27 | fs.mkdirpSync(out);
|
28 |
|
29 | return (schemas) => {
|
30 | each(pairs(schemas), ([name, markdown]) => {
|
31 | const fileName = path.resolve(out, `${name}.md`);
|
32 |
|
33 |
|
34 | const output = (!meta ? '' : '---\n')
|
35 | + (!meta ? '' : yaml.dump(meta))
|
36 | + (!meta ? '' : '---\n\n')
|
37 |
|
38 | + processor.stringify(markdown);
|
39 |
|
40 | fs.writeFile(fileName, output, (err) => {
|
41 | if (err) {
|
42 | error(err);
|
43 | }
|
44 |
|
45 | });
|
46 | });
|
47 |
|
48 | return schemas;
|
49 | };
|
50 | }
|
51 |
|
52 | function writereadme({
|
53 | out, error, info, meta, readme,
|
54 | }) {
|
55 | const processor = unified()
|
56 | .use(gfm)
|
57 | .use(stringify);
|
58 |
|
59 | if (readme) {
|
60 | fs.mkdirpSync(out);
|
61 |
|
62 | return (readmeast) => {
|
63 | const fileName = path.resolve(out, 'README.md');
|
64 |
|
65 | const output = (!meta ? '' : '---\n')
|
66 | + (!meta ? '' : yaml.dump(meta))
|
67 | + (!meta ? '' : '---\n\n')
|
68 |
|
69 | + processor.stringify(readmeast);
|
70 |
|
71 | fs.writeFile(fileName, output, (err) => {
|
72 | if (err) {
|
73 | error(err);
|
74 | }
|
75 | info(`${fileName} created`);
|
76 | });
|
77 |
|
78 | return fileName;
|
79 | };
|
80 | } else {
|
81 | return (args) => args;
|
82 | }
|
83 | }
|
84 |
|
85 | module.exports = { writemarkdown, writereadme };
|