Skip to content
Permalink
Browse files

Merge pull request #3538 from weyusi/micromatch

refactor: switch minimatch to micromatch
  • Loading branch information
tcrowe committed Jul 9, 2019
2 parents c2e4672 + 8d83bdf commit f172774d37f8dffe01d9acecd3613a8b6782e22a
Showing with 25 additions and 16 deletions.
  1. +2 −6 lib/box/index.js
  2. +2 −3 lib/plugins/processor/common.js
  3. +1 −1 package.json
  4. +20 −6 test/scripts/box/box.js
@@ -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(() => ({}));

@@ -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);
@@ -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.');
}

@@ -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;

@@ -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);
};
@@ -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",
@@ -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();

@@ -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 = {};

@@ -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';

0 comments on commit f172774

Please sign in to comment.
You can’t perform that action at this time.