Skip to content

Commit

Permalink
feat(load_plugins): ignore hexo-theme-[config.theme]
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Jan 31, 2020
1 parent 667d9a0 commit 4be9a99
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 16 deletions.
6 changes: 3 additions & 3 deletions lib/hexo/load_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { sep, resolve, join, parse } = require('path');
const tildify = require('tildify');
const Theme = require('../theme');
const Source = require('./source');
const fs = require('hexo-fs');
const { exists, readdir } = require('hexo-fs');
const { magenta } = require('chalk');
const { deepMerge } = require('hexo-util');

Expand All @@ -14,7 +14,7 @@ module.exports = ctx => {
const baseDir = ctx.base_dir;
let configPath = ctx.config_path;

return fs.exists(configPath).then(exist => {
return exists(configPath).then(exist => {
return exist ? configPath : findConfigPath(configPath);
}).then(path => {
if (!path) return;
Expand Down Expand Up @@ -50,7 +50,7 @@ module.exports = ctx => {
function findConfigPath(path) {
const { dir, name } = parse(path);

return fs.readdir(dir).then(files => {
return readdir(dir).then(files => {
const item = files.find(item => item.startsWith(name));
if (item != null) return join(dir, item);
});
Expand Down
37 changes: 25 additions & 12 deletions lib/hexo/load_plugins.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

const { join } = require('path');
const fs = require('hexo-fs');
const { exists, readFile, listDir } = require('hexo-fs');
const Promise = require('bluebird');
const chalk = require('chalk');
const { magenta } = require('chalk');

module.exports = ctx => {
if (!ctx.env.init || ctx.env.safe) return;
Expand All @@ -12,25 +12,38 @@ module.exports = ctx => {
};

function loadModuleList(ctx) {
if (ctx.config && Array.isArray(ctx.config.plugins)) {
return Promise.resolve(ctx.config.plugins).filter(item => typeof item === 'string');
let customThemeName;

if (ctx.config) {
const { theme, plugins } = ctx.config;

if (Array.isArray(plugins)) {
return Promise.resolve(ctx.config.plugins).filter(item => typeof item === 'string');
}

if (theme) {
customThemeName = String(theme);
}
}

const packagePath = join(ctx.base_dir, 'package.json');

// Make sure package.json exists
return fs.exists(packagePath).then(exist => {
return exists(packagePath).then(exist => {
if (!exist) return [];

// Read package.json and find dependencies
return fs.readFile(packagePath).then(content => {
return readFile(packagePath).then(content => {
const json = JSON.parse(content);
const deps = Object.keys(json.dependencies || {});
const devDeps = Object.keys(json.devDependencies || {});

return deps.concat(devDeps);
});
}).filter(name => {
// Ignore plugin whose name is "hexo-theme-[ctx.config.theme]"
if (name === `hexo-theme-${customThemeName}`) return false;

// Ignore plugins whose name is not started with "hexo-"
if (!/^hexo-|^@[^/]+\/hexo-/.test(name)) return false;

Expand All @@ -39,7 +52,7 @@ function loadModuleList(ctx) {

// Make sure the plugin exists
const path = ctx.resolvePlugin(name);
return fs.exists(path);
return exists(path);
});
}

Expand All @@ -49,9 +62,9 @@ function loadModules(ctx) {

// Load plugins
return ctx.loadPlugin(path).then(() => {
ctx.log.debug('Plugin loaded: %s', chalk.magenta(name));
ctx.log.debug('Plugin loaded: %s', magenta(name));
}).catch(err => {
ctx.log.error({err}, 'Plugin load failed: %s', chalk.magenta(name));
ctx.log.error({err}, 'Plugin load failed: %s', magenta(name));
});
});
}
Expand All @@ -60,15 +73,15 @@ function loadScripts(ctx) {
const baseDirLength = ctx.base_dir.length;

function displayPath(path) {
return chalk.magenta(path.substring(baseDirLength));
return magenta(path.substring(baseDirLength));
}

return Promise.filter([
ctx.theme_script_dir,
ctx.script_dir
], scriptDir => { // Ignore the directory if it does not exist
return scriptDir ? fs.exists(scriptDir) : false;
}).map(scriptDir => fs.listDir(scriptDir).map(name => {
return scriptDir ? exists(scriptDir) : false;
}).map(scriptDir => listDir(scriptDir).map(name => {
const path = join(scriptDir, name);

return ctx.loadPlugin(path).then(() => {
Expand Down
33 changes: 32 additions & 1 deletion test/scripts/hexo/load_plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const Promise = require('bluebird');

describe('Load plugins', () => {
const Hexo = require('../../../lib/hexo');
const hexo = new Hexo(pathFn.join(__dirname, 'plugin_test'), {silent: true});
const hexo = new Hexo(pathFn.join(__dirname, 'plugin_test'), { silent: true });
const loadPlugins = require('../../../lib/hexo/load_plugins');

const script = [
Expand Down Expand Up @@ -128,6 +128,23 @@ describe('Load plugins', () => {
});
});

it('ignore plugin whose name is "hexo-theme-[hexo.config.theme]"', () => {
hexo.config.theme = 'test_theme';

const script = 'hexo._script_test = true';
const name = 'hexo-theme-test_theme';
const path = pathFn.join(hexo.plugin_dir, name, 'index.js');

return Promise.all([
createPackageFile(name),
fs.writeFile(path, script)
]).then(() => loadPlugins(hexo)).then(() => {
should.not.exist(hexo._script_test);
delete hexo.config.theme;
return fs.unlink(path);
});
});

it('ignore plugins whose name is not started with "hexo-"', () => {
const script = 'hexo._script_test = true';
const name = 'another-plugin';
Expand All @@ -142,6 +159,20 @@ describe('Load plugins', () => {
});
});

it('ignore plugins which is typescript definition', () => {
const script = 'hexo._script_test = true';
const name = '@types/hexo-test-plugin';
const path = pathFn.join(hexo.plugin_dir, name, 'index.js');

return Promise.all([
createPackageFile(name),
fs.writeFile(path, script)
]).then(() => loadPlugins(hexo)).then(() => {
should.not.exist(hexo._script_test);
return fs.unlink(path);
});
});

it('ignore plugins which are in package.json but not exist actually', () => createPackageFile('hexo-plugin-test').then(() => loadPlugins(hexo)));

it('load scripts', () => {
Expand Down

0 comments on commit 4be9a99

Please sign in to comment.