|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +// @flow |
| 4 | + |
| 5 | +import fs from 'fs'; |
| 6 | +import path from 'path'; |
| 7 | +import { |
| 8 | + getRules, |
| 9 | + isFile |
| 10 | +} from './utilities'; |
| 11 | + |
| 12 | +const windows = (array, size) => { |
| 13 | + const output = []; |
| 14 | + |
| 15 | + for (let ii = 0; ii < array.length - size + 1; ii++) { |
| 16 | + output.push(array.slice(ii, ii + size)); |
| 17 | + } |
| 18 | + |
| 19 | + return output; |
| 20 | +}; |
| 21 | + |
| 22 | +const getDocIndexRules = () => { |
| 23 | + const content = fs.readFileSync(path.resolve(__dirname, '../../.README/README.md'), 'utf-8'); |
| 24 | + |
| 25 | + const rules = content.split('\n').map((line) => { |
| 26 | + const match = /^{"gitdown": "include", "file": "([^"]+)"}$/.exec(line); |
| 27 | + |
| 28 | + if (match === null) { |
| 29 | + return null; |
| 30 | + } else { |
| 31 | + return match[1].replace('./rules/', '').replace('.md', ''); |
| 32 | + } |
| 33 | + }).filter((rule) => { |
| 34 | + return rule !== null; |
| 35 | + }); |
| 36 | + |
| 37 | + if (rules.length === 0) { |
| 38 | + throw new Error('Docs checker is broken - it could not extract rules from docs index file.'); |
| 39 | + } |
| 40 | + |
| 41 | + return rules; |
| 42 | +}; |
| 43 | + |
| 44 | +const hasCorrectAssertions = (docPath, name) => { |
| 45 | + const content = fs.readFileSync(docPath, 'utf-8'); |
| 46 | + |
| 47 | + const match = /<!-- assertions ([a-zA-Z]+) -->/.exec(content); |
| 48 | + |
| 49 | + if (match === null) { |
| 50 | + return false; |
| 51 | + } else { |
| 52 | + return match[1] === name; |
| 53 | + } |
| 54 | +}; |
| 55 | + |
| 56 | +/** |
| 57 | + * Performed checks: |
| 58 | + * - file `/.README/rules/<rule>.md` exists |
| 59 | + * - file `/.README/rules/<rule>.md` contains correct assertions placeholder (`<!-- assertions ... -->`) |
| 60 | + * - rule is included in gitdown directive in `/.README/README.md` |
| 61 | + * - rules in `/.README/README.md` are alphabetically sorted |
| 62 | + */ |
| 63 | +const checkDocs = (rulesNames) => { |
| 64 | + const docIndexRules = getDocIndexRules(); |
| 65 | + |
| 66 | + const sorted = windows(docIndexRules, 2) |
| 67 | + .every((chunk) => { |
| 68 | + return chunk[0] < chunk[1]; |
| 69 | + }); |
| 70 | + |
| 71 | + if (!sorted) { |
| 72 | + throw new Error('Rules are not alphabetically sorted in `.README/README.md` file.'); |
| 73 | + } |
| 74 | + |
| 75 | + const invalid = rulesNames.filter((names) => { |
| 76 | + const docPath = path.resolve(__dirname, '../../.README/rules', names[1] + '.md'); |
| 77 | + const docExists = isFile(docPath); |
| 78 | + const inIndex = docIndexRules.indexOf(names[1]) !== -1; |
| 79 | + const hasAssertions = docExists ? hasCorrectAssertions(docPath, names[0]) : false; |
| 80 | + |
| 81 | + return !(docExists && inIndex && hasAssertions); |
| 82 | + }); |
| 83 | + |
| 84 | + if (invalid.length > 0) { |
| 85 | + const invalidList = invalid |
| 86 | + .map((names) => { |
| 87 | + return names[0]; |
| 88 | + }).join(', '); |
| 89 | + |
| 90 | + throw new Error( |
| 91 | + 'Docs checker encountered an error in: ' + invalidList + '. ' + |
| 92 | + 'Make sure that for every rule you created documentation file with assertions placeholder in camelCase ' + |
| 93 | + 'and included the file path in `.README/README.md` file.' |
| 94 | + ); |
| 95 | + } |
| 96 | +}; |
| 97 | + |
| 98 | +checkDocs(getRules()); |
0 commit comments