diff --git a/lib/hexo/multi_config_path.js b/lib/hexo/multi_config_path.js index 07d24d295a..db84a3588a 100644 --- a/lib/hexo/multi_config_path.js +++ b/lib/hexo/multi_config_path.js @@ -22,12 +22,14 @@ module.exports = ctx => function multiConfigPath(base, configPaths, outputDir) { } else { // only one config - if (!fs.existsSync(pathFn.join(base, configPaths))) { + let configPath = pathFn.isAbsolute(configPaths) ? configPaths : pathFn.resolve(base, configPaths); + + if (!fs.existsSync(configPath)) { log.w(`Config file ${configPaths} not found, using default.`); - return defaultPath; + configPath = defaultPath; } - return pathFn.resolve(base, configPaths); + return configPath; } const numPaths = paths.length; @@ -36,13 +38,21 @@ module.exports = ctx => function multiConfigPath(base, configPaths, outputDir) { const combinedConfig = {}; let count = 0; for (let i = 0; i < numPaths; i++) { - if (!fs.existsSync(pathFn.join(base, paths[i]))) { + let configPath = ''; + + if (pathFn.isAbsolute(paths[i])) { + configPath = paths[i]; + } else { + configPath = pathFn.join(base, paths[i]); + } + + if (!fs.existsSync(configPath)) { log.w(`Config file ${paths[i]} not found.`); continue; } // files read synchronously to ensure proper overwrite order - const file = fs.readFileSync(pathFn.join(base, paths[i])); + const file = fs.readFileSync(configPath); const ext = pathFn.extname(paths[i]).toLowerCase(); if (ext === '.yml') { diff --git a/test/scripts/hexo/multi_config_path.js b/test/scripts/hexo/multi_config_path.js index 2daab44921..1d9ec35ac0 100644 --- a/test/scripts/hexo/multi_config_path.js +++ b/test/scripts/hexo/multi_config_path.js @@ -115,18 +115,32 @@ describe('config flag handling', () => { '}' ].join('\n'); + var testJson3 = [ + '{', + '"author": "james bond",', + '"favorites": {', + ' "food": "martini",', + ' "ice_cream": "vanilla"', + ' }', + '}' + ].join('\n'); + before(() => { fs.writeFileSync(base + 'test1.yml', testYaml1); fs.writeFileSync(base + 'test2.yml', testYaml2); fs.writeFileSync(base + 'test1.json', testJson1); fs.writeFileSync(base + 'test2.json', testJson2); + fs.writeFileSync('/tmp/test3.json', testJson3); }); afterEach(() => { hexo.log.reader = []; }); - after(() => fs.rmdir(hexo.base_dir)); + after(() => { + fs.rmdirSync(hexo.base_dir); + fs.unlinkSync('/tmp/test3.json'); + }); it('no file', () => { mcp(base).should.equal(base + '_config.yml'); @@ -140,6 +154,8 @@ describe('config flag handling', () => { mcp(base, 'test1.json').should.eql( pathFn.resolve(base + 'test1.json')); + + mcp(base, '/tmp/test3.json').should.eql('/tmp/test3.json'); }); it('1 not found file warning', () => { @@ -151,6 +167,15 @@ describe('config flag handling', () => { + ' not found, using default.'); }); + it('1 not found file warning absolute', () => { + let notFile = '/tmp/not_a_file.json'; + + mcp(base, notFile).should.eql(pathFn.join(base, '_config.yml')); + hexo.log.reader[0].type.should.eql('warning'); + hexo.log.reader[0].msg.should.eql('Config file ' + notFile + + ' not found, using default.'); + }); + it('combined config output', () => { var combinedPath = pathFn.join(base, '_multiconfig.yml'); @@ -174,6 +199,14 @@ describe('config flag handling', () => { + ' Using _config.yml.'); }); + it('combine config output with absolute paths', () => { + var combinedPath = pathFn.join(base, '_multiconfig.yml'); + + mcp(base, 'test1.json,/tmp/test3.json').should.eql(combinedPath); + hexo.log.reader[0].type.should.eql('info'); + hexo.log.reader[0].msg.should.eql('Config based on 2 files'); + }); + it('2 YAML overwrite', () => { var configFile = mcp(base, 'test1.yml,test2.yml'); var config = fs.readFileSync(configFile);