Skip to content

Commit

Permalink
test(load_plugins): load hexo plugin in the theme's package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed Sep 21, 2021
1 parent d363db2 commit 8c2f6c2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/hexo/load_plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function loadModuleList(ctx, basedir) {
}

function loadModules(ctx) {
return Promise.all([ctx.base_dir, ctx.theme_dir].map(basedir => loadModuleList(ctx, basedir)))
return Promise.map([ctx.base_dir, ctx.theme_dir], basedir => loadModuleList(ctx, basedir))
.then(([hexoModuleList, themeModuleList]) => {
return Object.entries(Object.assign(hexoModuleList, themeModuleList));
})
Expand Down
45 changes: 27 additions & 18 deletions test/scripts/hexo/load_plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,31 @@ describe('Load plugins', () => {
delete hexo._script_test;
}

function createPackageFile(...args) {
function createPackageFile(name, path) {
const pkg = {
name: 'hexo-site',
version: '0.0.0',
private: true,
dependencies: {}
dependencies: {
[name]: '*'
}
};

for (const arg of args) {
pkg.dependencies[arg] = '*';
}

return fs.writeFile(join(hexo.base_dir, 'package.json'), JSON.stringify(pkg, null, ' '));
path = path || join(hexo.base_dir, 'package.json');
return fs.writeFile(path, JSON.stringify(pkg, null, ' '));
}

function createPackageFileWithDevDeps(...args) {
function createPackageFileWithDevDeps(name) {
const pkg = {
name: 'hexo-site',
version: '0.0.0',
private: true,
dependencies: {},
devDependencies: {}
devDependencies: {
[name]: '*'
}
};

for (let i = 0, len = args.length; i < len; i++) {
pkg.devDependencies[args[i]] = '*';
}

return fs.writeFile(join(hexo.base_dir, 'package.json'), JSON.stringify(pkg, null, ' '));
}

Expand All @@ -67,6 +64,10 @@ describe('Load plugins', () => {

after(() => fs.rmdir(hexo.base_dir));

afterEach(() => {
createPackageFile('hexo-another-plugin');
});

it('load plugins', () => {
const name = 'hexo-plugin-test';
const path = join(hexo.plugin_dir, name, 'index.js');
Expand Down Expand Up @@ -106,9 +107,19 @@ describe('Load plugins', () => {
});
});

it('ignore plugin whose name is "hexo-theme-[hexo.config.theme]"', async () => {
hexo.config.theme = 'test_theme';
it('load plugins in the theme\'s package.json', async () => {
const name = 'hexo-plugin-test';
const path = join(hexo.plugin_dir, name, 'index.js');
return Promise.all([
createPackageFile(name, join(hexo.theme_dir, 'package.json')),
fs.writeFile(path, script)
]).then(() => loadPlugins(hexo)).then(() => {
validate(path);
return fs.unlink(path);
});
});

it('ignore plugin whose name is started with "hexo-theme-"', async () => {
const script = 'hexo._script_test = true';
const name = 'hexo-theme-test_theme';
const path = join(hexo.plugin_dir, name, 'index.js');
Expand All @@ -124,9 +135,7 @@ describe('Load plugins', () => {
return fs.unlink(path);
});

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

it('ignore scoped plugin whose name is started with "hexo-theme-"', async () => {
const script = 'hexo._script_test = true';
const name = '@hexojs/hexo-theme-test_theme';
const path = join(hexo.plugin_dir, name, 'index.js');
Expand Down

0 comments on commit 8c2f6c2

Please sign in to comment.