-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
executable file
·156 lines (124 loc) · 4.05 KB
/
index.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#! /usr/bin/env node
const fs = require('fs');
const path = require('path');
const { promisify } = require('util');
const minimist = require('minimist');
const glob = require('glob');
const chalk = require('chalk');
const markdownlint = require('markdownlint');
const pkg = require('./package.json');
const resolvePaths = paths => paths
.map(p => path.resolve(p))
.reduce(
(memo, p) => {
const parent = memo.find(item => p.indexOf(item) === 0);
const children = memo.filter(item => item.indexOf(p) === 0);
if (parent) {
return memo;
}
if (children) {
return [...memo, p].filter(item => !children.includes(item));
}
return [...memo, p];
},
[],
);
const readConfig = file => promisify(markdownlint.readConfig)(file)
.catch(() => ({ default: true }));
const readIgnore = file => promisify(fs.exists)(file)
.then(exists => (
!exists
? []
: promisify(fs.readFile)(file, 'utf8')
.then(data => data.trim().split('\n'))
.catch(() => [])
));
const hasKnownExtension = fname => [
'markdown',
'mdown',
'mkdn',
'mkd',
'md',
].indexOf(fname.split('.').pop()) >= 0;
const readFiles = (paths, opts) => Promise.all(
paths.map(p => promisify(fs.stat)(p).then(stats => (
stats.isDirectory()
? promisify(glob)(path.join(p, '**', '**'), {
ignore: opts.ignore.map(i => path.join(p, i, '**')),
})
: p
))),
)
.then(results => results.reduce(
(memo, item) => memo.concat(item),
[],
))
.then(files => files.filter(hasKnownExtension));
const sortResults = (a, b) => (
(a.lineNumber > b.lineNumber && 1)
|| (a.lineNumber < b.lineNumber && -1)
|| 0
);
const printResults = (results, verbose) => {
const rulesUrl = 'https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md';
const stats = { files: 0, total: 0 };
Object.keys(results).forEach((key) => {
if (!results[key].length) {
return;
}
stats.files += 1;
console.log(chalk.underline(key));
results[key].sort(sortResults).forEach((result) => {
stats.total += 1;
stats[result.ruleAlias] = (stats[result.ruleAlias] + 1) || 1;
console.log([
chalk.grey(` ${result.lineNumber}`),
(result.errorRange && chalk.grey(`:${result.errorRange[0]}`)) || '',
chalk.yellow(` ${result.ruleNames[1]} [${result.ruleNames[0]}]`),
chalk.bold.blue(` ${result.ruleDescription}`),
result.errorDetail ? ` [${result.errorDetail}]` : '',
result.errorContext ? chalk.italic(` "${result.errorContext}"`) : '',
(verbose && chalk.dim(` ${rulesUrl}#${result.ruleNames[0].toLowerCase()}`)),
].join(''));
});
});
console.log('\nSummary:');
console.log(`${stats.total} issues in ${stats.files} file(s)`);
Object.keys(stats)
.filter(k => ['total', 'files'].indexOf(k) < 0)
.forEach(k => console.log(` - ${chalk.yellow(k)} ${stats[k]}`));
process.exit(stats.total ? 1 : 0);
};
const printError = (err) => {
console.error(err);
process.exit(1);
};
const help = () => console.log(`Usage:
mdlint [path1] [path2] ...
Options:
-c, --config Path to config file. Default: '.mdlintrc'
-i, --ignore Path to file with patterns to ignore. Default: '.mdlintignore'
-v, --verbose Show verbose output.
-h, --help Show this help.
-V, --version Show ${pkg.name} version.
${pkg.author.name} ${(new Date()).getFullYear()}`);
module.exports = (paths, opts = {}) => readFiles(resolvePaths(paths), opts)
.then(files => promisify(markdownlint)({ files, config: opts.config }));
if (require.main === module) {
const { _: paths, ...opts } = minimist(process.argv.slice(2));
if (opts.V || opts.version) {
console.log(pkg.version);
process.exit(0);
}
if (!paths.length || opts.h || opts.help) {
help();
process.exit(0);
}
Promise.all([
readConfig(opts.c || opts.config || '.mdlintrc'),
readIgnore(opts.i || opts.ignore || '.mdlintignore'),
])
.then(([config, ignore]) => module.exports(paths, { config, ignore }))
.then(printResults)
.catch(printError);
}