-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlint.mjs
73 lines (59 loc) · 1.76 KB
/
lint.mjs
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
/*
* This script runs markdownlint for all md filed in `posts` and `drafts` dirs.
*
* All errors will be logged.
*
* Any error found in `posts` files will result in non-zero exit code.
* Errors found `drafts` files will just be logged.
*/
import { globSync } from 'glob';
import markdownlint from 'markdownlint';
const config = markdownlint.readConfigSync('.markdownlint.json');
const files = globSync([
'posts/*.md',
'drafts/*.md',
]);
/** @type {markdownlint.Options} */
const options = {
config,
files,
};
console.log('Linting markdown files in posts and drafts ...\n');
const results = markdownlint.sync(options);
let postsErrorCount = 0;
let draftsErrorCount = 0;
for (const fileName in results) {
const result = results[fileName];
if (result.length === 0) {
// no errors
console.log(`✔️ ${fileName} - ok`);
continue;
}
// there were some errors... count and log them
for (const err of result) {
const isDraft = fileName.startsWith('drafts/');
let symbol = '❌';
if (isDraft) {
draftsErrorCount++;
symbol = '🚧';
} else {
postsErrorCount++;
}
console.log(`${symbol} ${fileName}:${err.lineNumber} - ${err.ruleNames.join('/')} ${err.ruleDescription} [${err.errorDetail}]`);
}
}
console.log('\n──────────');
if (postsErrorCount === 0 && draftsErrorCount === 0) {
console.log('All fine. 👍️');
} else {
if (postsErrorCount > 0) {
console.log(`❌ ${postsErrorCount} error${postsErrorCount > 1 ? 's' : ''} found in posts`);
}
if (draftsErrorCount > 0) {
console.log(`🚧 ${draftsErrorCount} error${draftsErrorCount > 1 ? 's' : ''} found in drafts`);
}
}
console.log('──────────');
if (postsErrorCount > 0) {
process.exitCode = 1;
}