From 8d83bdff7335ea82866a2b055f0a0287de8e844c Mon Sep 17 00:00:00 2001 From: weyusi Date: Tue, 9 Jul 2019 15:15:47 +0930 Subject: [PATCH] refactor: switch minimatch to micromatch --- lib/box/index.js | 8 ++------ lib/plugins/processor/common.js | 5 ++--- package.json | 2 +- test/scripts/box/box.js | 26 ++++++++++++++++++++------ 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/lib/box/index.js b/lib/box/index.js index d85d79a85f..edd6459556 100644 --- a/lib/box/index.js +++ b/lib/box/index.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.'); } diff --git a/lib/plugins/processor/common.js b/lib/plugins/processor/common.js index 6a7f8f5c4c..edf32c3a41 100644 --- a/lib/plugins/processor/common.js +++ b/lib/plugins/processor/common.js @@ -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); }; diff --git a/package.json b/package.json index 03563fbeff..c5f9437bab 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/scripts/box/box.js b/test/scripts/box/box.js index 2cf686c52b..131034f6c2 100644 --- a/test/scripts/box/box.js +++ b/test/scripts/box/box.js @@ -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';