From 6b4c4bb86e529123cc9e202743e7d6fee9d0c1b7 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Sun, 22 Dec 2024 17:43:23 +0800 Subject: [PATCH] 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);