Skip to content

Commit

Permalink
Add -i/--ignore option to ignore/exclude files (fixes #16, fixes #24).
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Jan 27, 2018
1 parent 0730386 commit 2640160
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ $ markdownlint --help

Options:

-h, --help output usage information
-V, --version output the version number
-c, --config [configFile] Configuration file
-h, --help output usage information
-V, --version output the version number
-c, --config [configFile] configuration file
-i, --ignore [file|directory|glob] files to ignore/exclude
```

## Configuration
Expand Down
15 changes: 12 additions & 3 deletions markdownlint.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
var fs = require('fs');
var path = require('path');
var program = require('commander');
var difference = require('lodash.difference');
var flatten = require('lodash.flatten');
var extend = require('deep-extend');
var markdownlint = require('markdownlint');
Expand Down Expand Up @@ -74,19 +75,27 @@ function printResult(lintResult) {
}
}

function concatArray(item, array) {
array.push(item);
return array;
}

program
.version(pkg.version)
.description(pkg.description)
.usage('[options] <files|directories|globs>')
.option('-c, --config [configFile]', 'Configuration file');
.option('-c, --config [configFile]', 'configuration file')
.option('-i, --ignore [file|directory|glob]', 'files to ignore/exclude', concatArray, []);

program.parse(process.argv);

var files = prepareFileList(program.args);
var ignores = prepareFileList(program.ignore);
var diff = difference(files, ignores);

if (files && files.length > 0) {
if (files.length > 0) {
var config = readConfiguration(program);
var lintResult = lint(files, config);
var lintResult = lint(diff, config);
printResult(lintResult);
} else {
program.help();
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"commander": "~2.9.0",
"deep-extend": "~0.4.1",
"glob": "~7.0.3",
"lodash.flatten": "~4.3.0",
"lodash.difference": "~4.5.0",
"lodash.flatten": "~4.4.0",
"markdownlint": "~0.7.0",
"rc": "~1.1.6"
},
Expand Down
41 changes: 41 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,44 @@ test('dir linting works with failing .markdown files', async t => {
t.true(err.stderr.match(errorPattern).length === 10);
}
});

test('glob linting with failing files passes when failures ignored by glob', async t => {
const result = await execa('../markdownlint.js',
['--config', 'test-config.json', '**/*.md', '--ignore', '**/incorrect.md']);
t.true(result.stdout === '');
t.true(result.stderr === '');
});

test('glob linting with failing files passes when everything ignored by glob', async t => {
const result = await execa('../markdownlint.js',
['--config', 'test-config.json', '**/*.md', '--ignore', '**/*']);
t.true(result.stdout === '');
t.true(result.stderr === '');
});

test('glob linting with failing files has fewer errors when ignored by dir', async t => {
try {
await execa('../markdownlint.js',
['--config', 'test-config.json', '**/*.md', '--ignore', 'subdir-incorrect']);
} catch (err) {
t.true(err.stdout === '');
t.true(err.stderr.match(errorPattern).length === 8);
}
});

test('dir linting with failing files has fewer errors when ignored by file', async t => {
try {
await execa('../markdownlint.js',
['--config', 'test-config.json', 'subdir-incorrect', '--ignore', 'subdir-incorrect/incorrect.md']);
} catch (err) {
t.true(err.stdout === '');
t.true(err.stderr.match(errorPattern).length === 2);
}
});

test('glob linting with failing files passes when ignored by multiple globs', async t => {
const result = await execa('../markdownlint.js',
['--config', 'test-config.json', 'subdir-incorrect', '--ignore', '**/*.md', '--ignore', '**/*.markdown']);
t.true(result.stdout === '');
t.true(result.stderr === '');
});

0 comments on commit 2640160

Please sign in to comment.