Skip to content

Commit

Permalink
add --ignore support to cli. closes #329
Browse files Browse the repository at this point in the history
  • Loading branch information
millermedeiros committed Jul 14, 2016
1 parent 610ec2d commit ebc469f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 20 deletions.
23 changes: 18 additions & 5 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var fs = require('fs');
var glob = require('glob');
var merge = require('mout/object/merge');
var minimist = require('minimist');
var minimatch = require('minimatch');
var options = require('./options');
var path = require('path');
var stdin = require('stdin');
Expand All @@ -25,7 +26,10 @@ var parseOpts = {
'v': 'version',
'c': 'config',
'p': 'preset'
}
},
'string': [
'ignore'
]
};


Expand Down Expand Up @@ -87,7 +91,7 @@ function run(argv) {
return;
}

files = expandGlobs(files);
files = expandGlobs(files, argv.ignore);

if (argv.i) {
files.forEach(function(file) {
Expand Down Expand Up @@ -137,19 +141,28 @@ function logError(e) {
}


function expandGlobs(filePaths) {
function expandGlobs(filePaths, ignore) {
return filePaths.reduce(function(arr, file) {
// if file path contains "magical chars" (glob) we expand it, otherwise we
// simply use the file path
// simply use the file path (`push` is faster than `concat` and avoid `fs`)
if (glob.hasMagic(file)) {
return arr.concat(glob.sync(file, {
ignore: ignore,
// we want to return the glob itself to report that it didn't find any
// files, better to giver clear error messages than to fail silently
nonull: true,
nodir: true
}));
}
arr.push(file);
if (
!ignore ||
!minimatch(file, ignore, {
// `dot:true` to follow same behavior as `glob.sync:ignore`
dot: true
})
) {
arr.push(file);
}
return arr;
}, []);
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
"debug": "^0.7.4",
"disparity": "^2.0.0",
"esformatter-parser": "^1.0.0",
"glob": "^5.0.3",
"glob": "^7.0.5",
"minimatch": "^3.0.2",
"minimist": "^1.1.1",
"mout": ">=0.9 <2.0",
"npm-run": "^2.0.0",
Expand Down
64 changes: 50 additions & 14 deletions test/cli.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,21 +255,58 @@ describe('Command line interface', function() {
);

// glob expansion
testCLI('glob', [comparePath('default/arr*-in.js')], function(formattedFile) {
expect(formattedFile).to.equal(
readOut('default/array_expression') +
readOut('default/array_pattern') +
readOut('default/arrow_function_expression')
);
});
testCLI(
'glob',
[comparePath('default/arr*-in.js')],
function(result) {
expect(result).to.equal(
readOut('default/array_expression') +
readOut('default/array_pattern') +
readOut('default/arrow_function_expression')
);
}
);

// glob expansion + ignore
testCLI(
'glob ignore',
['--ignore', '**/*_pattern-in.js', comparePath('default/arr*-in.js')],
function(result) {
expect(result).to.equal(
readOut('default/array_expression') +
readOut('default/arrow_function_expression')
);
}
);

// glob + multiple ignore
testCLI(
'glob ignore multi',
[
'--ignore',
'**/*_pattern-in.js',
'--ignore',
'**/array_expression-in.js',
comparePath('default/arr*-in.js')
],
function(result) {
expect(result).to.equal(
readOut('default/arrow_function_expression')
);
}
);

// invalid glob expansion should throw error
testCLI('glob', [comparePath('default/fake-file*-in.js')], function(formattedFile) {
var msg = formattedFile.message.trim();
var filePath = comparePath('default/fake-file*-in.js');
expect(msg).to.contain("Error: Can't read source file.");
expect(msg).to.contain(filePath);
});
testCLI(
'glob invalid',
[comparePath('default/fake-file*-in.js')],
function(formattedFile) {
var msg = formattedFile.message.trim();
var filePath = comparePath('default/fake-file*-in.js');
expect(msg).to.contain("Error: Can't read source file.");
expect(msg).to.contain(filePath);
}
);

// invalid JS files should throw errors
testCLI('invalid js', [comparePath('error/invalid-*.js')], function(formattedFile) {
Expand Down Expand Up @@ -304,7 +341,6 @@ describe('Command line interface', function() {
});
});


// -----------------------------
// SLOW TESTS are executed later
// -----------------------------
Expand Down

0 comments on commit ebc469f

Please sign in to comment.