Skip to content

Commit

Permalink
refactor: switch minimatch to micromatch
Browse files Browse the repository at this point in the history
  • Loading branch information
weyusi committed Jul 9, 2019
1 parent c2e4672 commit 8d83bdf
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
8 changes: 2 additions & 6 deletions lib/box/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { Pattern, HashStream } = require('hexo-util');
const fs = require('hexo-fs');
const chalk = require('chalk');
const { EventEmitter } = require('events');
const minimatch = require('minimatch');
const micromatch = require('micromatch');

const defaultPattern = new Pattern(() => ({}));

Expand All @@ -30,10 +30,6 @@ function Box(ctx, base, options) {
this.Cache = ctx.model('Cache');
this.File = this._createFileClass();
this.ignore = ctx.config.ignore;

if (!Array.isArray(this.ignore)) {
this.ignore = [this.ignore];
}
}

require('util').inherits(Box, EventEmitter);
Expand Down Expand Up @@ -105,7 +101,7 @@ Box.prototype.addProcessor = function(pattern, fn) {
Box.prototype._readDir = function(base, fn, prefix = '') {
const { ignore } = this;

if (base && ignore && ignore.length && ignore.some(item => minimatch(base, item))) {
if (base && ignore && ignore.length && micromatch.isMatch(base, ignore)) {
return Promise.resolve('Ignoring dir.');
}

Expand Down
5 changes: 2 additions & 3 deletions lib/plugins/processor/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { Pattern } = require('hexo-util');
const moment = require('moment-timezone');
const minimatch = require('minimatch');
const micromatch = require('micromatch');

const DURATION_MINUTE = 1000 * 60;

Expand Down Expand Up @@ -48,7 +48,6 @@ exports.timezone = (date, timezone) => {

exports.isMatch = (path, patterns) => {
if (!patterns) return false;
if (!Array.isArray(patterns)) patterns = [patterns];

return patterns.some(pattern => pattern && minimatch(path, pattern));
return micromatch.isMatch(path, patterns);
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"hexo-util": "^0.6.3",
"js-yaml": "^3.12.0",
"lodash": "^4.17.11",
"minimatch": "^3.0.4",
"micromatch": "^4.0.2",
"moment": "^2.22.2",
"moment-timezone": "^0.5.21",
"nunjucks": "^3.1.3",
Expand Down
26 changes: 20 additions & 6 deletions test/scripts/box/box.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ describe('Box', () => {
box.base.should.eql(pathFn.join(baseDir, 'foo') + pathFn.sep);
});

it('constructor - make ignore an array if its not one', () => {
const box = newBox('foo', {ignore: 'fooDir'});
box.ignore.should.eql(['fooDir']);
});

it('addProcessor() - no pattern', () => {
const box = newBox();

Expand Down Expand Up @@ -227,7 +222,7 @@ describe('Box', () => {
}).finally(() => fs.rmdir(box.base));
});

it('process() - skip files if they match glob epression in ignore', () => {
it('process() - skip files if they match a glob epression in ignore', () => {
const box = newBox('test', {ignore: '**/ignore_me'});
const data = {};

Expand All @@ -246,6 +241,25 @@ describe('Box', () => {
}).finally(() => fs.rmdir(box.base));
});

it('process() - skip files if they match any of the glob expressions in ignore', () => {
const box = newBox('test', {ignore: ['**/ignore_me', '**/ignore_me_too']});
const data = {};

box.addProcessor(file => {
data[file.path] = file;
});

return Promise.all([
fs.writeFile(pathFn.join(box.base, 'foo.txt'), 'foo'),
fs.writeFile(pathFn.join(box.base, 'ignore_me', 'bar.txt'), 'ignore_me')
]).then(() => box.process()).then(() => {
const keys = Object.keys(data);

keys.length.should.eql(1);
keys[0].should.eql('foo.txt');
}).finally(() => fs.rmdir(box.base));
});

it('watch() - create', () => {
const box = newBox('test');
const path = 'a.txt';
Expand Down

0 comments on commit 8d83bdf

Please sign in to comment.