Skip to content

Commit

Permalink
feat(load_config): remove findConfigPath
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed Dec 22, 2024
1 parent ebe92ba commit 6b4c4bb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
22 changes: 6 additions & 16 deletions lib/hexo/load_config.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -12,13 +12,12 @@ export = async (ctx: Hexo): Promise<void> => {
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)));
Expand Down Expand Up @@ -63,13 +62,4 @@ export = async (ctx: Hexo): Promise<void> => {
}
ctx.theme_script_dir = join(ctx.theme_dir, 'scripts') + sep;
ctx.theme = new Theme(ctx, { ignored });

};

async function findConfigPath(path: string): Promise<string> {
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);
}
6 changes: 3 additions & 3 deletions test/scripts/hexo/load_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 6b4c4bb

Please sign in to comment.