/
checkDocs.js
98 lines (77 loc) · 2.54 KB
/
checkDocs.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env node
// @flow
import fs from 'fs';
import path from 'path';
import {
getRules,
isFile,
} from './utilities';
const windows = (array, size) => {
const output = [];
for (let ii = 0; ii < array.length - size + 1; ii++) {
output.push(array.slice(ii, ii + size));
}
return output;
};
const getDocIndexRules = () => {
const content = fs.readFileSync(path.resolve(__dirname, '../../.README/README.md'), 'utf-8');
const rules = content.split('\n').map((line) => {
const match = /^{"gitdown": "include", "file": "([^"]+)"}$/.exec(line);
if (match === null) {
return null;
} else {
return match[1].replace('./rules/', '').replace('.md', '');
}
}).filter((rule) => {
return rule !== null;
});
if (rules.length === 0) {
throw new Error('Docs checker is broken - it could not extract rules from docs index file.');
}
return rules;
};
const hasCorrectAssertions = (docPath, name) => {
const content = fs.readFileSync(docPath, 'utf-8');
const match = /<!-- assertions ([a-zA-Z]+) -->/.exec(content);
if (match === null) {
return false;
} else {
return match[1] === name;
}
};
/**
* Performed checks:
* - file `/.README/rules/<rule>.md` exists
* - file `/.README/rules/<rule>.md` contains correct assertions placeholder (`<!-- assertions ... -->`)
* - rule is included in gitdown directive in `/.README/README.md`
* - rules in `/.README/README.md` are alphabetically sorted
*/
const checkDocs = (rulesNames) => {
const docIndexRules = getDocIndexRules();
const sorted = windows(docIndexRules, 2)
.every((chunk) => {
return chunk[0] < chunk[1];
});
if (!sorted) {
throw new Error('Rules are not alphabetically sorted in `.README/README.md` file.');
}
const invalid = rulesNames.filter((names) => {
const docPath = path.resolve(__dirname, '../../.README/rules', names[1] + '.md');
const docExists = isFile(docPath);
const inIndex = docIndexRules.includes(names[1]);
const hasAssertions = docExists ? hasCorrectAssertions(docPath, names[0]) : false;
return !(docExists && inIndex && hasAssertions);
});
if (invalid.length > 0) {
const invalidList = invalid
.map((names) => {
return names[0];
}).join(', ');
throw new Error(
'Docs checker encountered an error in: ' + invalidList + '. ' +
'Make sure that for every rule you created documentation file with assertions placeholder in camelCase ' +
'and included the file path in `.README/README.md` file.',
);
}
};
checkDocs(getRules());