forked from igorshubovych/markdownlint-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdownlint.js
executable file
·291 lines (262 loc) · 9.13 KB
/
markdownlint.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const path = require('path');
const Module = require('module');
const program = require('commander');
const getStdin = require('get-stdin');
const jsYaml = require('js-yaml');
const jsoncParser = require('jsonc-parser');
const differenceWith = require('lodash.differencewith');
const flatten = require('lodash.flatten');
const extend = require('deep-extend');
const ignore = require('ignore');
const markdownlint = require('markdownlint');
const markdownlintRuleHelpers = require('markdownlint-rule-helpers');
const rc = require('rc');
const glob = require('glob');
const minimatch = require('minimatch');
const pkg = require('./package');
function jsoncParse(text) {
return JSON.parse(jsoncParser.stripComments(text));
}
const projectConfigFiles = [
'.markdownlint.json',
'.markdownlint.yaml',
'.markdownlint.yml'
];
const configFileParsers = [jsoncParse, jsYaml.safeLoad];
const fsOptions = {encoding: 'utf8'};
function readConfiguration(args) {
let config = rc('markdownlint', {});
const userConfigFile = args.config;
for (const projectConfigFile of projectConfigFiles) {
try {
fs.accessSync(projectConfigFile, fs.R_OK);
const projectConfig = markdownlint.readConfigSync(projectConfigFile, configFileParsers);
config = extend(config, projectConfig);
break;
} catch (error) {
// Ignore failure
}
}
// Normally parsing this file is not needed,
// because it is already parsed by rc package.
// However I have to do it to overwrite configuration
// from .markdownlint.{json,yaml,yml}.
if (userConfigFile) {
try {
const userConfig = markdownlint.readConfigSync(userConfigFile, configFileParsers);
config = extend(config, userConfig);
} catch (error) {
console.warn('Cannot read or parse config file ' + args.config + ': ' + error.message);
}
}
return config;
}
function prepareFileList(files, fileExtensions, previousResults) {
const globOptions = {
nodir: true
};
let extensionGlobPart = '*.';
if (fileExtensions.length === 1) {
// Glob seems not to match patterns like 'foo.{js}'
extensionGlobPart += fileExtensions[0];
} else {
extensionGlobPart += '{' + fileExtensions.join(',') + '}';
}
files = files.map(function (file) {
try {
if (fs.lstatSync(file).isDirectory()) {
// Directory (file falls through to below)
if (previousResults) {
const matcher = new minimatch.Minimatch(
path.resolve(process.cwd(), path.join(file, '**', extensionGlobPart)), globOptions);
return previousResults.filter(function (fileInfo) {
return matcher.match(fileInfo.absolute);
}).map(function (fileInfo) {
return fileInfo.original;
});
}
return glob.sync(path.join(file, '**', extensionGlobPart), globOptions);
}
} catch (error) {
// Not a directory, not a file, may be a glob
if (previousResults) {
const matcher = new minimatch.Minimatch(path.resolve(process.cwd(), file), globOptions);
return previousResults.filter(function (fileInfo) {
return matcher.match(fileInfo.absolute);
}).map(function (fileInfo) {
return fileInfo.original;
});
}
return glob.sync(file, globOptions);
}
// File
return file;
});
return flatten(files).map(function (file) {
return {
original: file,
relative: path.relative(process.cwd(), file),
absolute: path.resolve(file)
};
});
}
function printResult(lintResult) {
const results = flatten(Object.keys(lintResult).map(file => {
return lintResult[file].map(result => {
return {
file: file,
lineNumber: result.lineNumber,
column: (result.errorRange && result.errorRange[0]) || 0,
names: result.ruleNames.join('/'),
description: result.ruleDescription +
(result.errorDetail ? ' [' + result.errorDetail + ']' : '') +
(result.errorContext ? ' [Context: "' + result.errorContext + '"]' : '')
};
});
}));
let lintResultString = '';
if (results.length > 0) {
results.sort((a, b) => {
return a.file.localeCompare(b.file) || a.lineNumber - b.lineNumber ||
a.names.localeCompare(b.names) || a.description.localeCompare(b.description);
});
lintResultString = results.map(result => {
const {file, lineNumber, column, names, description} = result;
const columnText = column ? `:${column}` : '';
return `${file}:${lineNumber}${columnText} ${names} ${description}`;
}).join('\n');
// Note: process.exit(1) will end abruptly, interrupting asynchronous IO
// streams (e.g., when the output is being piped). Just set the exit code
// and let the program terminate normally.
// @see {@link https://nodejs.org/dist/latest-v8.x/docs/api/process.html#process_process_exit_code}
// @see {@link https://github.com/igorshubovych/markdownlint-cli/pull/29#issuecomment-343535291}
process.exitCode = 1;
}
if (program.output) {
try {
fs.writeFileSync(program.output, lintResultString);
} catch (error) {
console.warn('Cannot write to output file ' + program.output + ': ' + error.message);
process.exitCode = 2;
}
} else if (lintResultString) {
console.error(lintResultString);
}
}
function concatArray(item, array) {
array.push(item);
return array;
}
program
.version(pkg.version)
.description(pkg.description)
.usage('[options] <files|directories|globs>')
.option('-f, --fix', 'fix basic errors (does not work with STDIN)')
.option('-s, --stdin', 'read from STDIN (does not work with files)')
.option('-o, --output [outputFile]', 'write issues to file (no console)')
.option('-c, --config [configFile]', 'configuration file (JSON, JSONC, or YAML)')
.option('-i, --ignore [file|directory|glob]', 'file(s) to ignore/exclude', concatArray, [])
.option('-p, --ignore-path [file]', 'path to file with ignore pattern(s)')
.option('-r, --rules [file|directory|glob|package]', 'custom rule files', concatArray, []);
program.parse(process.argv);
function tryResolvePath(filepath) {
try {
if (path.basename(filepath) === filepath && path.extname(filepath) === '') {
// Looks like a package name, resolve it relative to cwd
// Get list of directories, where requested module can be.
let paths = Module._nodeModulePaths(process.cwd());
paths = paths.concat(Module.globalPaths);
if (require.resolve.paths) {
// Node >= 8.9.0
return require.resolve(filepath, {paths: paths});
}
return Module._resolveFilename(filepath, {paths: paths});
}
// Maybe it is a path to package installed locally
return require.resolve(path.join(process.cwd(), filepath));
} catch (error) {
return filepath;
}
}
function loadCustomRules(rules) {
return flatten(rules.map(function (rule) {
try {
const resolvedPath = [tryResolvePath(rule)];
const fileList = flatten(prepareFileList(resolvedPath, ['js']).map(function (filepath) {
return require(filepath.absolute);
}));
if (fileList.length === 0) {
throw new Error('No such rule');
}
return fileList;
} catch (error) {
console.error('Cannot load custom rule ' + rule + ': ' + error.message);
process.exit(3);
}
}));
}
let ignorePath = '.markdownlintignore';
let {existsSync} = fs;
if (program.ignorePath) {
ignorePath = program.ignorePath;
existsSync = () => true;
}
let ignoreFilter = () => true;
if (existsSync(ignorePath)) {
const ignoreText = fs.readFileSync(ignorePath, fsOptions);
const ignoreInstance = ignore().add(ignoreText);
ignoreFilter = fileInfo => !ignoreInstance.ignores(fileInfo.relative);
}
const files = prepareFileList(program.args, ['md', 'markdown'])
.filter(ignoreFilter);
const ignores = prepareFileList(program.ignore, ['md', 'markdown'], files);
const customRules = loadCustomRules(program.rules);
const diff = differenceWith(files, ignores, function (a, b) {
return a.absolute === b.absolute;
}).map(function (paths) {
return paths.original;
});
function lintAndPrint(stdin, files) {
files = files || [];
const config = readConfiguration(program);
const lintOptions = {
config,
customRules,
files
};
if (stdin) {
lintOptions.strings = {
stdin
};
}
if (program.fix) {
const fixOptions = {
...lintOptions,
resultVersion: 3
};
files.forEach(file => {
fixOptions.files = [file];
const fixResult = markdownlint.sync(fixOptions);
const fixes = fixResult[file].filter(error => error.fixInfo);
if (fixes.length > 0) {
const originalText = fs.readFileSync(file, fsOptions);
const fixedText = markdownlintRuleHelpers.applyFixes(originalText, fixes);
if (originalText !== fixedText) {
fs.writeFileSync(file, fixedText, fsOptions);
}
}
});
}
const lintResult = markdownlint.sync(lintOptions);
printResult(lintResult);
}
if ((files.length > 0) && !program.stdin) {
lintAndPrint(null, diff);
} else if ((files.length === 0) && program.stdin && !program.fix) {
getStdin().then(lintAndPrint);
} else {
program.help();
}