Skip to content

Commit

Permalink
fix(box): ignore correct .git and node modules in the theme folder (
Browse files Browse the repository at this point in the history
#4306)

* improve ignoreCfg

* fix test case

* fix this.ignore property

* format code

* fix test case
  • Loading branch information
jiangtj authored May 18, 2020
1 parent 83e1434 commit 956242e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 50 deletions.
13 changes: 5 additions & 8 deletions lib/box/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const { createReadStream, readdir, stat, watch } = require('hexo-fs');
const { magenta } = require('chalk');
const { EventEmitter } = require('events');
const { isMatch, makeRe } = require('micromatch');
const ignore = ['**/themes/*/node_modules/**', '**/themes/*/.git/**'];

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

Expand All @@ -31,14 +30,12 @@ class Box extends EventEmitter {
this.watcher = null;
this.Cache = ctx.model('Cache');
this.File = this._createFileClass();

let ignoreCfg = ignore;
if (ctx.config.ignore) {
if (ctx.config.ignore.length) ignoreCfg = ignoreCfg.concat(ctx.config.ignore);
let targets = this.options.ignored || [];
if (ctx.config.ignore && ctx.config.ignore.length) {
targets = targets.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));
this.ignore = targets;
this.options.ignored = targets.map(s => toRegExp(ctx, s)).filter(x => x);
}
_createFileClass() {
const ctx = this.context;
Expand Down
5 changes: 4 additions & 1 deletion lib/hexo/load_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@ module.exports = async ctx => {
const themeDirFromNodeModules = join(ctx.plugin_dir, 'hexo-theme-' + theme) + sep; // base_dir/node_modules/hexo-theme-[config.theme]/

// themeDirFromThemes has higher priority than themeDirFromNodeModules
let ignored = [];
if (await exists(themeDirFromThemes)) {
ctx.theme_dir = themeDirFromThemes;
ignored = ['**/themes/*/node_modules/**', '**/themes/*/.git/**'];
} else if (await exists(themeDirFromNodeModules)) {
ctx.theme_dir = themeDirFromNodeModules;
ignored = ['**/node_modules/hexo-theme-*/node_modules/**', '**/node_modules/hexo-theme-*/.git/**'];
}
ctx.theme_script_dir = join(ctx.theme_dir, 'scripts') + sep;
ctx.theme = new Theme(ctx);
ctx.theme = new Theme(ctx, { ignored });

};

Expand Down
4 changes: 2 additions & 2 deletions lib/theme/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const View = require('./view');
const I18n = require('hexo-i18n');

class Theme extends Box {
constructor(ctx) {
super(ctx, ctx.theme_dir);
constructor(ctx, options) {
super(ctx, ctx.theme_dir, options);

this.config = {};

Expand Down
39 changes: 0 additions & 39 deletions test/scripts/box/box.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,45 +301,6 @@ describe('Box', () => {
await rmdir(box.base);
});

it('process() - skip node_modules of theme by default', async () => {
const box = newBox('test', { ignore: null });
const data = {};

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

await Promise.all([
writeFile(join(box.base, 'foo.txt'), 'foo'),
writeFile(join(box.base, 'themes', 'bar', 'node_modules', 'bar_library', 'bar.js'), 'themes')
]);
await box.process();

data.should.have.all.keys(['foo.txt']);

await rmdir(box.base);
});

it('process() - always skip node_modules of theme', async () => {
const box = newBox('test', { ignore: '**/ignore_me' });
const data = {};

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

await Promise.all([
writeFile(join(box.base, 'foo.txt'), 'foo'),
writeFile(join(box.base, 'ignore_me', 'bar.txt'), 'ignore_me'),
writeFile(join(box.base, 'themes', 'bar', 'node_modules', 'bar_library', 'bar.js'), 'themes')
]);
await box.process();

data.should.have.all.keys(['foo.txt']);

await rmdir(box.base);
});

it('watch() - create', async () => {
const box = newBox('test');
const path = 'a.txt';
Expand Down
7 changes: 7 additions & 0 deletions test/scripts/hexo/load_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const pathFn = require('path');
const fs = require('hexo-fs');
const { makeRe } = require('micromatch');

describe('Load config', () => {
const Hexo = require('../../../lib/hexo');
Expand Down Expand Up @@ -108,6 +109,9 @@ describe('Load config', () => {
hexo.theme_dir.should.eql(pathFn.join(hexo.base_dir, 'themes', 'test') + pathFn.sep);
hexo.theme_script_dir.should.eql(pathFn.join(hexo.theme_dir, 'scripts') + pathFn.sep);
hexo.theme.base.should.eql(hexo.theme_dir);
const ignore = ['**/themes/*/node_modules/**', '**/themes/*/.git/**'];
hexo.theme.ignore.should.eql(ignore);
hexo.theme.options.ignored.should.eql(ignore.map(item => makeRe(item)));
}).finally(() => {
fs.rmdir(pathFn.join(hexo.base_dir, 'themes', 'test'));
fs.unlink(hexo.config_path);
Expand All @@ -121,6 +125,9 @@ describe('Load config', () => {
hexo.theme_dir.should.eql(pathFn.join(hexo.plugin_dir, 'hexo-theme-test') + pathFn.sep);
hexo.theme_script_dir.should.eql(pathFn.join(hexo.theme_dir, 'scripts') + pathFn.sep);
hexo.theme.base.should.eql(hexo.theme_dir);
const ignore = ['**/node_modules/hexo-theme-*/node_modules/**', '**/node_modules/hexo-theme-*/.git/**'];
hexo.theme.ignore.should.eql(ignore);
hexo.theme.options.ignored.should.eql(ignore.map(item => makeRe(item)));
}).finally(() => {
fs.rmdir(pathFn.join(hexo.plugin_dir, 'hexo-theme-test'));
fs.unlink(hexo.config_path);
Expand Down

0 comments on commit 956242e

Please sign in to comment.