Skip to content

Commit 9c99da5

Browse files
feat(load_config): support _config.json
1 parent 6b4c4bb commit 9c99da5

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

lib/hexo/load_config.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { sep, resolve, join } from 'path';
1+
import { sep, resolve, join, parse } from 'path';
22
import tildify from 'tildify';
33
import Theme from '../theme';
44
import Source from './source';
5-
import { exists } from 'hexo-fs';
5+
import { exists, readdir } from 'hexo-fs';
66
import { magenta } from 'picocolors';
77
import { deepMerge } from 'hexo-util';
88
import validateConfig from './validate_config';
@@ -12,12 +12,13 @@ export = async (ctx: Hexo): Promise<void> => {
1212
if (!ctx.env.init) return;
1313

1414
const baseDir = ctx.base_dir;
15-
const configPath = ctx.config_path;
15+
let configPath = ctx.config_path;
1616

17-
const configExists = await exists(configPath);
18-
if (!configExists) return;
17+
const path = await exists(configPath) ? configPath : await findConfigPath(configPath);
18+
if (!path) return;
19+
configPath = path;
1920

20-
let config = await ctx.render.render({ path: configPath });
21+
let config = await ctx.render.render({ path });
2122
if (!config || typeof config !== 'object') return;
2223

2324
ctx.log.debug('Config loaded: %s', magenta(tildify(configPath)));
@@ -63,3 +64,11 @@ export = async (ctx: Hexo): Promise<void> => {
6364
ctx.theme_script_dir = join(ctx.theme_dir, 'scripts') + sep;
6465
ctx.theme = new Theme(ctx, { ignored });
6566
};
67+
68+
async function findConfigPath(path: string): Promise<string> {
69+
const { dir, name } = parse(path);
70+
71+
const files = await readdir(dir);
72+
const item = files.find(item => item === name + '.json');
73+
if (item != null) return join(dir, item);
74+
}

test/scripts/hexo/load_config.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('Load config', () => {
1414
after(() => rmdir(hexo.base_dir));
1515

1616
beforeEach(() => {
17+
hexo.config_path = join(hexo.base_dir, '_config.yml');
1718
hexo.config = JSON.parse(JSON.stringify(defaultConfig));
1819
});
1920

@@ -40,7 +41,8 @@ describe('Load config', () => {
4041
try {
4142
await writeFile(configPath, '{"baz": 3}');
4243
await loadConfig(hexo);
43-
hexo.config.should.eql(defaultConfig);
44+
hexo.config.baz.should.eql(3);
45+
hexo.config_path.should.eql(configPath);
4446
} finally {
4547
await unlink(configPath);
4648
}
@@ -53,6 +55,7 @@ describe('Load config', () => {
5355
await writeFile(configPath, 'foo: 1');
5456
await loadConfig(hexo);
5557
hexo.config.should.eql(defaultConfig);
58+
hexo.config_path.should.not.eql(configPath);
5659
} finally {
5760
await unlink(configPath);
5861
}
@@ -79,8 +82,8 @@ describe('Load config', () => {
7982
try {
8083
await writeFile(realPath, '{"foo": 2}');
8184
await loadConfig(hexo);
82-
hexo.config.should.eql(defaultConfig);
83-
hexo.config_path.should.not.eql(realPath);
85+
hexo.config.foo.should.eql(2);
86+
hexo.config_path.should.eql(realPath);
8487
} finally {
8588
hexo.config_path = join(hexo.base_dir, '_config.yml');
8689
await unlink(realPath);

0 commit comments

Comments
 (0)