Skip to content

Commit

Permalink
updated config loader to accept absolute paths to config files (#3118)
Browse files Browse the repository at this point in the history
* updated config loader to accept absolute paths to config files

* fixes for coveralls and codeclimate

* refactored isAbsolute() check
  • Loading branch information
mgoldsborough authored and JLHwung committed Apr 26, 2018
1 parent ad33b3c commit e5f16e2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
20 changes: 15 additions & 5 deletions lib/hexo/multi_config_path.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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') {
Expand Down
35 changes: 34 additions & 1 deletion test/scripts/hexo/multi_config_path.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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', () => {
Expand All @@ -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');

Expand All @@ -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);
Expand Down

0 comments on commit e5f16e2

Please sign in to comment.