|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const glob = require('glob'); |
| 4 | +const fs = require('fs'); |
| 5 | +const path = require('path'); |
| 6 | +const pipe = require('@k88/pipe-compose').pipe; |
| 7 | + |
| 8 | +const inlineCommentsRegex = /<!--[\S\s]*-->/; |
| 9 | +const includeRegex = /docs-generator:include\(['"]?([\w\/.]*)['"]?\)/; |
| 10 | +const docStartRegex = /docs-generator:start/; |
| 11 | +const docEndRegex = /docs-generator:end/; |
| 12 | + |
| 13 | +const docStartComment = '<!-- docs-generator:start - Do not remove or modify this section -->'; |
| 14 | +const docEndComment = '<!-- docs-generator:end - Do not remove or modify this section -->'; |
| 15 | + |
| 16 | +const rootDir = path.dirname(__dirname); |
| 17 | +const pattern = `${rootDir}/packages/**/docs.config.js`; |
| 18 | +const packages = glob.sync(pattern, { ignore: '**/node_modules/**' }); |
| 19 | + |
| 20 | +/** |
| 21 | + * Validates the configuration format |
| 22 | + * |
| 23 | + * @param config |
| 24 | + * @returns boolean |
| 25 | + */ |
| 26 | +const validateConfig = (config) => config.main && config.output; |
| 27 | + |
| 28 | +/** |
| 29 | + * Flattens array |
| 30 | + * |
| 31 | + * @param arr |
| 32 | + * @returns {*[]} |
| 33 | + */ |
| 34 | +const flatten = (arr) => [].concat.apply([], arr); |
| 35 | + |
| 36 | +/** |
| 37 | + * Reads the file |
| 38 | + * |
| 39 | + * @param filePath the file path |
| 40 | + * @returns {string} the file content |
| 41 | + */ |
| 42 | +const readFile = (filePath) => fs.readFileSync(filePath, 'utf8'); |
| 43 | + |
| 44 | +/** |
| 45 | + * Looks up for `<!-- docs-generator:include('/path') --!>` and includes the content |
| 46 | + * |
| 47 | + * @param dir |
| 48 | + * @returns {function(*): *[]} |
| 49 | + */ |
| 50 | +const addInclude = (dir) => { |
| 51 | + return lines => { |
| 52 | + const newLines = lines.map(line => { |
| 53 | + if (!inlineCommentsRegex.test(line)) { |
| 54 | + return line; |
| 55 | + } |
| 56 | + |
| 57 | + const match = line.match(includeRegex); |
| 58 | + if (!match || !match.length) { |
| 59 | + return line; |
| 60 | + } |
| 61 | + |
| 62 | + const file = readFile(path.join(dir, match[1])); |
| 63 | + const newLine = [ |
| 64 | + line, |
| 65 | + docStartComment, |
| 66 | + file.split('\n'), |
| 67 | + docEndComment, |
| 68 | + ]; |
| 69 | + |
| 70 | + return flatten(newLine); |
| 71 | + }); |
| 72 | + |
| 73 | + return flatten(newLines); |
| 74 | + } |
| 75 | +}; |
| 76 | + |
| 77 | +/** |
| 78 | + * Cleans up/removes all the auto-generated lines |
| 79 | + * @param lines |
| 80 | + * @returns {*} |
| 81 | + */ |
| 82 | +const cleanGeneratedLines = (lines) => { |
| 83 | + const docStartLines = []; |
| 84 | + const docEndLines = []; |
| 85 | + |
| 86 | + lines.forEach((line, index) => { |
| 87 | + if (!inlineCommentsRegex.test(line)) { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + if (docStartRegex.test(line)) { |
| 92 | + docStartLines.push(index); |
| 93 | + } |
| 94 | + |
| 95 | + if (docEndRegex.test(line)) { |
| 96 | + docEndLines.push(index); |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | + if (docEndLines.length !== docStartLines.length) { |
| 101 | + console.error('Found unmatched docs-generator blocks'); |
| 102 | + return lines; |
| 103 | + } |
| 104 | + |
| 105 | + while (docStartLines.length) { |
| 106 | + const startIndex = docStartLines.pop(); |
| 107 | + const endIndex = docEndLines.pop(); |
| 108 | + |
| 109 | + lines.splice(startIndex, endIndex - startIndex + 1); |
| 110 | + } |
| 111 | + |
| 112 | + return lines; |
| 113 | +}; |
| 114 | + |
| 115 | +/** |
| 116 | + * Main script generator |
| 117 | + * @param dir |
| 118 | + */ |
| 119 | +const generate = (dir) => { |
| 120 | + const config = require(path.join(dir, 'docs.config.js')); |
| 121 | + if (!validateConfig(config)) { |
| 122 | + console.error('Configuration for %s is invalid', dir); |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + const main = readFile(path.join(dir, config.main)); |
| 127 | + const output = path.join(dir, config.output); |
| 128 | + |
| 129 | + const lines = main.split('\n'); |
| 130 | + const formattedLines = pipe(lines, cleanGeneratedLines, addInclude(dir)); |
| 131 | + |
| 132 | + fs.writeFileSync(output, formattedLines.join('\n')); |
| 133 | +}; |
| 134 | + |
| 135 | +packages |
| 136 | + .map(path.dirname) |
| 137 | + .forEach(generate); |
0 commit comments