From 4be9a996479e7a289a8351ba53024b2a22bcaf5c Mon Sep 17 00:00:00 2001 From: SukkaW Date: Fri, 31 Jan 2020 18:21:59 +0800 Subject: [PATCH] feat(load_plugins): ignore hexo-theme-[config.theme] --- lib/hexo/load_config.js | 6 ++--- lib/hexo/load_plugins.js | 37 +++++++++++++++++++++---------- test/scripts/hexo/load_plugins.js | 33 ++++++++++++++++++++++++++- 3 files changed, 60 insertions(+), 16 deletions(-) diff --git a/lib/hexo/load_config.js b/lib/hexo/load_config.js index 06228907df..2725dad513 100644 --- a/lib/hexo/load_config.js +++ b/lib/hexo/load_config.js @@ -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'); @@ -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; @@ -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); }); diff --git a/lib/hexo/load_plugins.js b/lib/hexo/load_plugins.js index d6e2c6105c..14591216a9 100644 --- a/lib/hexo/load_plugins.js +++ b/lib/hexo/load_plugins.js @@ -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; @@ -12,18 +12,28 @@ 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 || {}); @@ -31,6 +41,9 @@ function loadModuleList(ctx) { 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; @@ -39,7 +52,7 @@ function loadModuleList(ctx) { // Make sure the plugin exists const path = ctx.resolvePlugin(name); - return fs.exists(path); + return exists(path); }); } @@ -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)); }); }); } @@ -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(() => { diff --git a/test/scripts/hexo/load_plugins.js b/test/scripts/hexo/load_plugins.js index cdf8aef921..2d410e76c7 100644 --- a/test/scripts/hexo/load_plugins.js +++ b/test/scripts/hexo/load_plugins.js @@ -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 = [ @@ -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'; @@ -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', () => {