UNPKG

2.24 kBJavaScriptView Raw
1/*
2 * Copyright 2019 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12const { each, pairs } = require('ferrum');
13const stringify = require('remark-stringify');
14const unified = require('unified');
15const gfm = require('remark-gfm');
16const path = require('path');
17const fs = require('fs-extra');
18const yaml = require('js-yaml');
19
20function 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 // add YAML frontmatter
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 // info(`${fileName} created`);
45 });
46 });
47
48 return schemas;
49 };
50}
51
52function 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 // add YAML frontmatter
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
85module.exports = { writemarkdown, writereadme };