Skip to content

Commit

Permalink
fix(box): always ignore node_modules folder of themes
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Dec 9, 2019
1 parent 979d1f4 commit 78799b0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
10 changes: 7 additions & 3 deletions lib/box/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const fs = require('hexo-fs');
const chalk = require('chalk');
const { EventEmitter } = require('events');
const micromatch = require('micromatch');
const { ignore } = require('../hexo/default_config');

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

Expand All @@ -29,11 +30,14 @@ function Box(ctx, base, options) {
this.watcher = null;
this.Cache = ctx.model('Cache');
this.File = this._createFileClass();
this.ignore = ctx.config.ignore;

let ignoreCfg = ignore;
if (ctx.config.ignore) {
const targets = Array.isArray(ctx.config.ignore) ? ctx.config.ignore : [ctx.config.ignore];
this.options.ignored = (this.options.ignored || []).concat(targets.map(s => toRegExp(ctx, s)).filter(x => x));
if (ctx.config.ignore.length) ignoreCfg = ignoreCfg.concat(ctx.config.ignore);
}
this.ignore = ignoreCfg;
const targets = ignoreCfg;
this.options.ignored = (this.options.ignored || []).concat(targets.map(s => toRegExp(ctx, s)).filter(x => x));
}

require('util').inherits(Box, EventEmitter);
Expand Down
3 changes: 1 addition & 2 deletions lib/hexo/default_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ module.exports = {
deploy: {},

// ignore files from processing
ignore: [],
ignore: ['**/themes/*/node_modules/**'],

// Category & Tag
meta_generator: true
};

43 changes: 41 additions & 2 deletions test/scripts/box/box.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ describe('Box', () => {
});

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

box.addProcessor(file => {
Expand All @@ -283,7 +283,7 @@ describe('Box', () => {
});

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

box.addProcessor(file => {
Expand All @@ -302,6 +302,45 @@ describe('Box', () => {
}).finally(() => fs.rmdir(box.base));
});

it('process() - skip node_modules of theme by default', () => {
const box = newBox('test', { ignore: null });
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, 'themes', 'bar', 'node_modules', 'bar_library', 'bar.js'), 'themes')
]).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('process() - always skip node_modules of theme', () => {
const box = newBox('test', { ignore: '**/ignore_me' });
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'),
fs.writeFile(pathFn.join(box.base, 'themes', 'bar', 'node_modules', 'bar_library', 'bar.js'), 'themes')
]).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 78799b0

Please sign in to comment.