markdownlint-cli 0.30.0 → 0.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +3 -2
  2. package/markdownlint.js +73 -97
  3. package/package.json +21 -17
package/README.md CHANGED
@@ -112,9 +112,10 @@ If a rule is passed to both `--enable` and `--disable`, it will be disabled.
112
112
  `markdownlint-cli` returns one of the following exit codes:
113
113
 
114
114
  - `0`: Program ran successfully
115
- - `1`: Linting errors / bad parameter
115
+ - `1`: Linting errors
116
116
  - `2`: Unable to write `-o`/`--output` output file
117
117
  - `3`: Unable to load `-r`/`--rules` custom rule
118
+ - `4`: Unexpected error (e.g. malformed config)
118
119
 
119
120
  ## Use with pre-commit
120
121
 
@@ -122,7 +123,7 @@ To run `markdownlint-cli` as part of a [pre-commit][pre-commit] workflow, add so
122
123
 
123
124
  ```yaml
124
125
  - repo: https://github.com/igorshubovych/markdownlint-cli
125
- rev: v0.30.0
126
+ rev: v0.31.0
126
127
  hooks:
127
128
  - id: markdownlint
128
129
  ```
package/markdownlint.js CHANGED
@@ -10,13 +10,10 @@ const process = require('process');
10
10
  const program = require('commander');
11
11
 
12
12
  const options = program.opts();
13
- const differenceWith = require('lodash.differencewith');
14
- const flatten = require('lodash.flatten');
13
+ const glob = require('glob');
15
14
  const markdownlint = require('markdownlint');
16
15
  const rc = require('run-con');
17
- const glob = require('glob');
18
16
  const minimatch = require('minimatch');
19
- const minimist = require('minimist');
20
17
  const pkg = require('./package.json');
21
18
 
22
19
  function jsoncParse(text) {
@@ -27,51 +24,43 @@ function jsYamlSafeLoad(text) {
27
24
  return require('js-yaml').load(text);
28
25
  }
29
26
 
30
- const projectConfigFiles = [
31
- '.markdownlint.json',
32
- '.markdownlint.yaml',
33
- '.markdownlint.yml'
34
- ];
27
+ const exitCodes = {
28
+ lintFindings: 1,
29
+ failedToWriteOutputFile: 2,
30
+ failedToLoadCustomRules: 3,
31
+ unexpectedError: 4
32
+ };
33
+
34
+ const projectConfigFiles = ['.markdownlint.json', '.markdownlint.yaml', '.markdownlint.yml'];
35
35
  const configFileParsers = [jsoncParse, jsYamlSafeLoad];
36
36
  const fsOptions = {encoding: 'utf8'};
37
37
  const processCwd = process.cwd();
38
38
 
39
39
  function readConfiguration(userConfigFile) {
40
40
  const jsConfigFile = /\.js$/i.test(userConfigFile);
41
- const rcArgv = minimist(process.argv.slice(2));
42
- if (jsConfigFile) {
43
- // Prevent rc package from parsing .js config file as INI
44
- delete rcArgv.config;
45
- }
46
41
 
47
42
  // Load from well-known config files
48
- let config = rc('markdownlint', {}, rcArgv);
43
+ let config = rc('markdownlint', {});
49
44
  for (const projectConfigFile of projectConfigFiles) {
50
45
  try {
51
46
  fs.accessSync(projectConfigFile, fs.R_OK);
52
47
  const projectConfig = markdownlint.readConfigSync(projectConfigFile, configFileParsers);
53
- config = require('deep-extend')(config, projectConfig);
48
+ config = {...config, ...projectConfig};
54
49
  break;
55
50
  } catch {
56
51
  // Ignore failure
57
52
  }
58
53
  }
59
54
 
60
- // Normally parsing this file is not needed,
61
- // because it is already parsed by rc package.
62
- // However I have to do it to overwrite configuration
63
- // from .markdownlint.{json,yaml,yml}.
55
+ // Normally parsing this file is not needed, because it is already parsed by rc package.
56
+ // However I have to do it to overwrite configuration from .markdownlint.{json,yaml,yml}.
64
57
  if (userConfigFile) {
65
58
  try {
66
- const userConfig = jsConfigFile
67
- // Evaluate .js configuration file as code
68
- ? require(path.resolve(processCwd, userConfigFile))
69
- // Load JSON/YAML configuration as data
70
- : markdownlint.readConfigSync(userConfigFile, configFileParsers);
59
+ const userConfig = jsConfigFile ? require(path.resolve(processCwd, userConfigFile)) : markdownlint.readConfigSync(userConfigFile, configFileParsers);
71
60
  config = require('deep-extend')(config, userConfig);
72
61
  } catch (error) {
73
62
  console.error(`Cannot read or parse config file '${userConfigFile}': ${error.message}`);
74
- process.exitCode = 1;
63
+ process.exitCode = exitCodes.unexpectedError;
75
64
  }
76
65
  }
77
66
 
@@ -91,18 +80,13 @@ function prepareFileList(files, fileExtensions, previousResults) {
91
80
  extensionGlobPart += '{' + fileExtensions.join(',') + '}';
92
81
  }
93
82
 
94
- files = files.map(function (file) {
83
+ files = files.map(file => {
95
84
  try {
96
85
  if (fs.lstatSync(file).isDirectory()) {
97
86
  // Directory (file falls through to below)
98
87
  if (previousResults) {
99
- const matcher = new minimatch.Minimatch(
100
- path.resolve(processCwd, path.join(file, '**', extensionGlobPart)), globOptions);
101
- return previousResults.filter(function (fileInfo) {
102
- return matcher.match(fileInfo.absolute);
103
- }).map(function (fileInfo) {
104
- return fileInfo.original;
105
- });
88
+ const matcher = new minimatch.Minimatch(path.resolve(processCwd, path.join(file, '**', extensionGlobPart)), globOptions);
89
+ return previousResults.filter(fileInfo => matcher.match(fileInfo.absolute)).map(fileInfo => fileInfo.original);
106
90
  }
107
91
 
108
92
  return glob.sync(path.join(file, '**', extensionGlobPart), globOptions);
@@ -111,11 +95,7 @@ function prepareFileList(files, fileExtensions, previousResults) {
111
95
  // Not a directory, not a file, may be a glob
112
96
  if (previousResults) {
113
97
  const matcher = new minimatch.Minimatch(path.resolve(processCwd, file), globOptions);
114
- return previousResults.filter(function (fileInfo) {
115
- return matcher.match(fileInfo.absolute);
116
- }).map(function (fileInfo) {
117
- return fileInfo.original;
118
- });
98
+ return previousResults.filter(fileInfo => matcher.match(fileInfo.absolute)).map(fileInfo => fileInfo.original);
119
99
  }
120
100
 
121
101
  return glob.sync(file, globOptions);
@@ -124,50 +104,48 @@ function prepareFileList(files, fileExtensions, previousResults) {
124
104
  // File
125
105
  return file;
126
106
  });
127
- return flatten(files).map(function (file) {
128
- return {
129
- original: file,
130
- relative: path.relative(processCwd, file),
131
- absolute: path.resolve(file)
132
- };
133
- });
107
+ return files.flat().map(file => ({
108
+ original: file,
109
+ relative: path.relative(processCwd, file),
110
+ absolute: path.resolve(file)
111
+ }));
134
112
  }
135
113
 
136
114
  function printResult(lintResult) {
137
- const results = flatten(Object.keys(lintResult).map(file => lintResult[file].map(result => {
138
- if (options.json) {
115
+ const results = Object.keys(lintResult).flatMap(file =>
116
+ lintResult[file].map(result => {
117
+ if (options.json) {
118
+ return {
119
+ fileName: file,
120
+ ...result
121
+ };
122
+ }
123
+
139
124
  return {
140
- fileName: file,
141
- ...result
125
+ file: file,
126
+ lineNumber: result.lineNumber,
127
+ column: (result.errorRange && result.errorRange[0]) || 0,
128
+ names: result.ruleNames.join('/'),
129
+ description: result.ruleDescription + (result.errorDetail ? ' [' + result.errorDetail + ']' : '') + (result.errorContext ? ' [Context: "' + result.errorContext + '"]' : '')
142
130
  };
143
- }
144
-
145
- return {
146
- file: file,
147
- lineNumber: result.lineNumber,
148
- column: (result.errorRange && result.errorRange[0]) || 0,
149
- names: result.ruleNames.join('/'),
150
- description: result.ruleDescription
151
- + (result.errorDetail ? ' [' + result.errorDetail + ']' : '')
152
- + (result.errorContext ? ' [Context: "' + result.errorContext + '"]' : '')
153
- };
154
- })));
131
+ })
132
+ );
155
133
 
156
134
  let lintResultString = '';
157
135
  if (results.length > 0) {
158
136
  if (options.json) {
159
- results.sort((a, b) => a.fileName.localeCompare(b.fileName) || a.lineNumber - b.lineNumber
160
- || a.ruleDescription.localeCompare(b.ruleDescription));
137
+ results.sort((a, b) => a.fileName.localeCompare(b.fileName) || a.lineNumber - b.lineNumber || a.ruleDescription.localeCompare(b.ruleDescription));
161
138
  lintResultString = JSON.stringify(results, null, 2);
162
139
  } else {
163
- results.sort((a, b) => a.file.localeCompare(b.file) || a.lineNumber - b.lineNumber
164
- || a.names.localeCompare(b.names) || a.description.localeCompare(b.description));
165
-
166
- lintResultString = results.map(result => {
167
- const {file, lineNumber, column, names, description} = result;
168
- const columnText = column ? `:${column}` : '';
169
- return `${file}:${lineNumber}${columnText} ${names} ${description}`;
170
- }).join('\n');
140
+ results.sort((a, b) => a.file.localeCompare(b.file) || a.lineNumber - b.lineNumber || a.names.localeCompare(b.names) || a.description.localeCompare(b.description));
141
+
142
+ lintResultString = results
143
+ .map(result => {
144
+ const {file, lineNumber, column, names, description} = result;
145
+ const columnText = column ? `:${column}` : '';
146
+ return `${file}:${lineNumber}${columnText} ${names} ${description}`;
147
+ })
148
+ .join('\n');
171
149
  }
172
150
 
173
151
  // Note: process.exit(1) will end abruptly, interrupting asynchronous IO
@@ -175,18 +153,16 @@ function printResult(lintResult) {
175
153
  // and let the program terminate normally.
176
154
  // @see {@link https://nodejs.org/dist/latest-v8.x/docs/api/process.html#process_process_exit_code}
177
155
  // @see {@link https://github.com/igorshubovych/markdownlint-cli/pull/29#issuecomment-343535291}
178
- process.exitCode = 1;
156
+ process.exitCode = exitCodes.lintFindings;
179
157
  }
180
158
 
181
159
  if (options.output) {
182
- lintResultString = lintResultString.length > 0
183
- ? lintResultString + os.EOL
184
- : lintResultString;
160
+ lintResultString = lintResultString.length > 0 ? lintResultString + os.EOL : lintResultString;
185
161
  try {
186
162
  fs.writeFileSync(options.output, lintResultString);
187
163
  } catch (error) {
188
164
  console.warn('Cannot write to output file ' + options.output + ': ' + error.message);
189
- process.exitCode = 2;
165
+ process.exitCode = exitCodes.failedToWriteOutputFile;
190
166
  }
191
167
  } else if (lintResultString && !options.quiet) {
192
168
  console.error(lintResultString);
@@ -241,12 +217,10 @@ function tryResolvePath(filepath) {
241
217
  }
242
218
 
243
219
  function loadCustomRules(rules) {
244
- return flatten(rules.map(function (rule) {
220
+ return rules.flatMap(rule => {
245
221
  try {
246
222
  const resolvedPath = [tryResolvePath(rule)];
247
- const fileList = flatten(prepareFileList(resolvedPath, ['js']).map(function (filepath) {
248
- return require(filepath.absolute);
249
- }));
223
+ const fileList = prepareFileList(resolvedPath, ['js']).flatMap(filepath => require(filepath.absolute));
250
224
  if (fileList.length === 0) {
251
225
  throw new Error('No such rule');
252
226
  }
@@ -254,9 +228,9 @@ function loadCustomRules(rules) {
254
228
  return fileList;
255
229
  } catch (error) {
256
230
  console.error('Cannot load custom rule ' + rule + ': ' + error.message);
257
- return process.exit(3);
231
+ return process.exit(exitCodes.failedToLoadCustomRules);
258
232
  }
259
- }));
233
+ });
260
234
  }
261
235
 
262
236
  let ignorePath = '.markdownlintignore';
@@ -274,15 +248,10 @@ if (existsSync(ignorePath)) {
274
248
  ignoreFilter = fileInfo => !ignoreInstance.ignores(fileInfo.relative);
275
249
  }
276
250
 
277
- const files = prepareFileList(program.args, ['md', 'markdown'])
278
- .filter(value => ignoreFilter(value));
251
+ const files = prepareFileList(program.args, ['md', 'markdown']).filter(value => ignoreFilter(value));
279
252
  const ignores = prepareFileList(options.ignore, ['md', 'markdown'], files);
280
253
  const customRules = loadCustomRules(options.rules);
281
- const diff = differenceWith(files, ignores, function (a, b) {
282
- return a.absolute === b.absolute;
283
- }).map(function (paths) {
284
- return paths.original;
285
- });
254
+ const diff = files.filter(file => !ignores.some(ignore => ignore.absolute === file.absolute)).map(paths => paths.original);
286
255
 
287
256
  function lintAndPrint(stdin, files) {
288
257
  files = files || [];
@@ -338,11 +307,18 @@ function lintAndPrint(stdin, files) {
338
307
  printResult(lintResult);
339
308
  }
340
309
 
341
- if ((files.length > 0) && !options.stdin) {
342
- lintAndPrint(null, diff);
343
- } else if ((files.length === 0) && options.stdin && !options.fix) {
344
- const getStdin = require('get-stdin');
345
- getStdin().then(lintAndPrint);
346
- } else {
347
- program.help();
310
+ try {
311
+ if (files.length > 0 && !options.stdin) {
312
+ lintAndPrint(null, diff);
313
+ } else if (files.length === 0 && options.stdin && !options.fix) {
314
+ // eslint-disable-next-line node/no-unsupported-features/es-syntax
315
+ import('get-stdin')
316
+ .then(module => module.default())
317
+ .then(lintAndPrint);
318
+ } else {
319
+ program.help();
320
+ }
321
+ } catch (error) {
322
+ console.error(error);
323
+ process.exit(exitCodes.unexpectedError);
348
324
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "markdownlint-cli",
3
- "version": "0.30.0",
3
+ "version": "0.31.0",
4
4
  "description": "MarkdownLint Command Line Interface",
5
5
  "main": "markdownlint.js",
6
6
  "bin": {
@@ -13,7 +13,7 @@
13
13
  "invalid": "node ./markdownlint.js --config test/test-config.json -- test/incorrect.md",
14
14
  "fix": "cp test/incorrect.md test/incorrect.copy.md && node ./markdownlint.js --fix test/incorrect.copy.md ; cat test/incorrect.copy.md && rm test/incorrect.copy.md",
15
15
  "test": "ava",
16
- "watch": "npm test -- --watch --tap | tap-growl",
16
+ "watch": "npm test --watch",
17
17
  "start": "node ./markdownlint.js",
18
18
  "precommit": "xo && npm test"
19
19
  },
@@ -37,30 +37,25 @@
37
37
  },
38
38
  "homepage": "https://github.com/igorshubovych/markdownlint-cli#readme",
39
39
  "dependencies": {
40
- "commander": "~8.3.0",
41
- "deep-extend": "~0.6.0",
42
- "get-stdin": "~8.0.0",
40
+ "commander": "~9.0.0",
41
+ "get-stdin": "~9.0.0",
43
42
  "glob": "~7.2.0",
44
- "ignore": "~5.1.9",
43
+ "ignore": "~5.2.0",
45
44
  "js-yaml": "^4.1.0",
46
45
  "jsonc-parser": "~3.0.0",
47
- "lodash.differencewith": "~4.5.0",
48
- "lodash.flatten": "~4.4.0",
49
- "markdownlint": "~0.24.0",
50
- "markdownlint-rule-helpers": "~0.15.0",
46
+ "markdownlint": "~0.25.1",
47
+ "markdownlint-rule-helpers": "~0.16.0",
51
48
  "minimatch": "~3.0.4",
52
- "minimist": "~1.2.5",
53
49
  "run-con": "~1.2.10"
54
50
  },
55
51
  "devDependencies": {
56
- "ava": "^3.15.0",
57
- "execa": "^5.1.1",
58
- "husky": "^7.0.4",
59
- "tap-growl": "^3.0.0",
52
+ "ava": "^4.0.1",
53
+ "execa": "^6.0.0",
60
54
  "test-rule-package": "./test/custom-rules/test-rule-package",
61
- "xo": "^0.46.4"
55
+ "xo": "^0.47.0"
62
56
  },
63
57
  "xo": {
58
+ "prettier": true,
64
59
  "space": true,
65
60
  "rules": {
66
61
  "comma-dangle": 0,
@@ -73,12 +68,21 @@
73
68
  "unicorn/prefer-ternary": 0
74
69
  }
75
70
  },
71
+ "prettier": {
72
+ "arrowParens": "avoid",
73
+ "bracketSpacing": false,
74
+ "endOfLine": "auto",
75
+ "printWidth": 1000,
76
+ "singleQuote": true,
77
+ "trailingComma": "none"
78
+ },
76
79
  "ava": {
77
80
  "files": [
78
81
  "test/**/*.js",
79
82
  "!test/custom-rules/**/*.js",
80
83
  "!test/md043-config.js"
81
84
  ],
82
- "failFast": true
85
+ "failFast": true,
86
+ "workerThreads": false
83
87
  }
84
88
  }