From a7fe2f2153f2b4a9cf4b9e3c11acc3641fdb2b48 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Sun, 22 Dec 2024 17:43:23 +0800 Subject: [PATCH 1/2] feat(load_config): remove findConfigPath --- lib/hexo/load_config.ts | 22 ++++++---------------- test/scripts/hexo/load_config.ts | 6 +++--- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/lib/hexo/load_config.ts b/lib/hexo/load_config.ts index 009aa4587c..6e905b6daf 100644 --- a/lib/hexo/load_config.ts +++ b/lib/hexo/load_config.ts @@ -1,8 +1,8 @@ -import { sep, resolve, join, parse } from 'path'; +import { sep, resolve, join } from 'path'; import tildify from 'tildify'; import Theme from '../theme'; import Source from './source'; -import { exists, readdir } from 'hexo-fs'; +import { exists } from 'hexo-fs'; import { magenta } from 'picocolors'; import { deepMerge } from 'hexo-util'; import validateConfig from './validate_config'; @@ -12,13 +12,12 @@ export = async (ctx: Hexo): Promise => { if (!ctx.env.init) return; const baseDir = ctx.base_dir; - let configPath = ctx.config_path; + const configPath = ctx.config_path; - const path = await exists(configPath) ? configPath : await findConfigPath(configPath); - if (!path) return; - configPath = path; + const configExists = await exists(configPath); + if (!configExists) return; - let config = await ctx.render.render({ path }); + let config = await ctx.render.render({ path: configPath }); if (!config || typeof config !== 'object') return; ctx.log.debug('Config loaded: %s', magenta(tildify(configPath))); @@ -63,13 +62,4 @@ export = async (ctx: Hexo): Promise => { } ctx.theme_script_dir = join(ctx.theme_dir, 'scripts') + sep; ctx.theme = new Theme(ctx, { ignored }); - }; - -async function findConfigPath(path: string): Promise { - const { dir, name } = parse(path); - - const files = await readdir(dir); - const item = files.find(item => item.startsWith(name)); - if (item != null) return join(dir, item); -} diff --git a/test/scripts/hexo/load_config.ts b/test/scripts/hexo/load_config.ts index 64e0536e62..ad7ddbbb2a 100644 --- a/test/scripts/hexo/load_config.ts +++ b/test/scripts/hexo/load_config.ts @@ -40,7 +40,7 @@ describe('Load config', () => { try { await writeFile(configPath, '{"baz": 3}'); await loadConfig(hexo); - hexo.config.baz.should.eql(3); + hexo.config.should.eql(defaultConfig); } finally { await unlink(configPath); } @@ -79,8 +79,8 @@ describe('Load config', () => { try { await writeFile(realPath, '{"foo": 2}'); await loadConfig(hexo); - hexo.config.foo.should.eql(2); - hexo.config_path.should.eql(realPath); + hexo.config.should.eql(defaultConfig); + hexo.config_path.should.not.eql(realPath); } finally { hexo.config_path = join(hexo.base_dir, '_config.yml'); await unlink(realPath); From 70f68d574d74f4f91c58d4726f91fc1e33472302 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Mon, 23 Dec 2024 17:10:49 +0800 Subject: [PATCH 2/2] feat(load_config): support _config.json --- lib/hexo/load_config.ts | 21 +++++++++++++++------ test/scripts/hexo/load_config.ts | 9 ++++++--- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/hexo/load_config.ts b/lib/hexo/load_config.ts index 6e905b6daf..717affe345 100644 --- a/lib/hexo/load_config.ts +++ b/lib/hexo/load_config.ts @@ -1,8 +1,8 @@ -import { sep, resolve, join } from 'path'; +import { sep, resolve, join, parse } from 'path'; import tildify from 'tildify'; import Theme from '../theme'; import Source from './source'; -import { exists } from 'hexo-fs'; +import { exists, readdir } from 'hexo-fs'; import { magenta } from 'picocolors'; import { deepMerge } from 'hexo-util'; import validateConfig from './validate_config'; @@ -12,12 +12,13 @@ export = async (ctx: Hexo): Promise => { if (!ctx.env.init) return; const baseDir = ctx.base_dir; - const configPath = ctx.config_path; + let configPath = ctx.config_path; - const configExists = await exists(configPath); - if (!configExists) return; + const path = await exists(configPath) ? configPath : await findConfigPath(configPath); + if (!path) return; + configPath = path; - let config = await ctx.render.render({ path: configPath }); + let config = await ctx.render.render({ path }); if (!config || typeof config !== 'object') return; ctx.log.debug('Config loaded: %s', magenta(tildify(configPath))); @@ -63,3 +64,11 @@ export = async (ctx: Hexo): Promise => { ctx.theme_script_dir = join(ctx.theme_dir, 'scripts') + sep; ctx.theme = new Theme(ctx, { ignored }); }; + +async function findConfigPath(path: string): Promise { + const { dir, name } = parse(path); + + const files = await readdir(dir); + const item = files.find(item => item === name + '.json'); + if (item != null) return join(dir, item); +} diff --git a/test/scripts/hexo/load_config.ts b/test/scripts/hexo/load_config.ts index ad7ddbbb2a..687c38f66f 100644 --- a/test/scripts/hexo/load_config.ts +++ b/test/scripts/hexo/load_config.ts @@ -14,6 +14,7 @@ describe('Load config', () => { after(() => rmdir(hexo.base_dir)); beforeEach(() => { + hexo.config_path = join(hexo.base_dir, '_config.yml'); hexo.config = JSON.parse(JSON.stringify(defaultConfig)); }); @@ -40,7 +41,8 @@ describe('Load config', () => { try { await writeFile(configPath, '{"baz": 3}'); await loadConfig(hexo); - hexo.config.should.eql(defaultConfig); + hexo.config.baz.should.eql(3); + hexo.config_path.should.eql(configPath); } finally { await unlink(configPath); } @@ -53,6 +55,7 @@ describe('Load config', () => { await writeFile(configPath, 'foo: 1'); await loadConfig(hexo); hexo.config.should.eql(defaultConfig); + hexo.config_path.should.not.eql(configPath); } finally { await unlink(configPath); } @@ -79,8 +82,8 @@ describe('Load config', () => { try { await writeFile(realPath, '{"foo": 2}'); await loadConfig(hexo); - hexo.config.should.eql(defaultConfig); - hexo.config_path.should.not.eql(realPath); + hexo.config.foo.should.eql(2); + hexo.config_path.should.eql(realPath); } finally { hexo.config_path = join(hexo.base_dir, '_config.yml'); await unlink(realPath);