Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated config loader to accept absolute paths to config files #3118

Merged
merged 3 commits into from
Apr 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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