Skip to content

Commit

Permalink
added command line option output to write db.json and _multiconfig.… (
Browse files Browse the repository at this point in the history
#3122)

* added command line option `output` to write db.json and _multiconfig.yml to a path provided

* fixes for appveyor
  • Loading branch information
mgoldsborough authored and JLHwung committed Apr 25, 2018
1 parent 30fcc4f commit ad33b3c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 12 deletions.
14 changes: 10 additions & 4 deletions lib/hexo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ const sep = pathFn.sep;
const dbVersion = 1;

function Hexo(base = process.cwd(), args = {}) {
const mcp = multiConfigPath(this);

EventEmitter.call(this);

this.base_dir = base + sep;
Expand Down Expand Up @@ -82,12 +80,20 @@ function Hexo(base = process.cwd(), args = {}) {

this._isGenerating = false;

// If `output` is provided, use that as the
// root for saving the db. Otherwise default to `base`.
const dbPath = args.output || base;

this.log.d(`Writing database to ${dbPath}/db.json`);

this.database = new Database({
version: dbVersion,
path: pathFn.join(base, 'db.json')
path: pathFn.join(dbPath, 'db.json')
});

this.config_path = args.config ? mcp(base, args.config) :
const mcp = multiConfigPath(this);

this.config_path = args.config ? mcp(base, args.config, args.output) :
pathFn.join(base, '_config.yml');

registerModels(this);
Expand Down
8 changes: 6 additions & 2 deletions lib/hexo/multi_config_path.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const fs = require('hexo-fs');
const _ = require('lodash');
const yml = require('js-yaml');

module.exports = ctx => function multiConfigPath(base, configPaths) {
module.exports = ctx => function multiConfigPath(base, configPaths, outputDir) {
const log = ctx.log;

const defaultPath = pathFn.join(base, '_config.yml');
Expand Down Expand Up @@ -63,7 +63,11 @@ module.exports = ctx => function multiConfigPath(base, configPaths) {

log.i('Config based on', count, 'files');

const outputPath = pathFn.join(base, '_multiconfig.yml');
const multiconfigRoot = outputDir || base;
const outputPath = pathFn.join(multiconfigRoot, '_multiconfig.yml');

log.d(`Writing _multiconfig.yml to ${outputPath}`);

fs.writeFileSync(outputPath, yml.dump(combinedConfig));

// write file and return path
Expand Down
53 changes: 47 additions & 6 deletions test/scripts/hexo/multi_config_path.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var pathFn = require('path');
var osFn = require('os');
var should = require('chai').should(); // eslint-disable-line
var fs = require('hexo-fs');
var yml = require('js-yaml');
Expand All @@ -14,6 +15,22 @@ describe('config flag handling', () => {

function ConsoleReader() {
this.reader = [];
this.d = function(...args) {
var type = 'debug';
var message = '';
for (var i = 0; i < args.length;) {
message += args[i];
if (++i < args.length) {
message += ' ';
}
}

this.reader.push({
type,
msg: message
});
}.bind(this);

this.i = function(...args) {
var type = 'info';
var message = '';
Expand Down Expand Up @@ -145,15 +162,15 @@ describe('config flag handling', () => {

hexo.log.reader[0].type.should.eql('info');
hexo.log.reader[0].msg.should.eql('Config based on 2 files');
hexo.log.reader[3].type.should.eql('warning');
hexo.log.reader[3].msg.should.eql('Config file notafile.yml not found.');
hexo.log.reader[4].type.should.eql('info');
hexo.log.reader[4].msg.should.eql('Config based on 1 files');
hexo.log.reader[6].type.should.eql('warning');
hexo.log.reader[6].msg.should.eql('Config file notafile.yml not found.');
hexo.log.reader[7].type.should.eql('info');
hexo.log.reader[7].msg.should.eql('Config based on 1 files');
// because who cares about grammar anyway?

mcp(base, 'notafile.yml,alsonotafile.json').should.not.eql(combinedPath);
hexo.log.reader[7].type.should.eql('error');
hexo.log.reader[7].msg.should.eql('No config files found.'
hexo.log.reader[11].type.should.eql('error');
hexo.log.reader[11].msg.should.eql('No config files found.'
+ ' Using _config.yml.');
});

Expand Down Expand Up @@ -205,4 +222,28 @@ describe('config flag handling', () => {
config.favorites.food.should.eql('sushi');
config.type.should.eql('dinosaur');
});

it('write multiconfig to specified path', () => {
let outputPath = osFn.tmpdir();
let combinedPath = pathFn.join(outputPath, '_multiconfig.yml');

mcp(base, 'test1.yml', outputPath).should.not.eql(combinedPath);
mcp(base, 'test1.yml,test2.yml', outputPath).should.eql(combinedPath);
mcp(base, 'test1.yml,test1.json', outputPath).should.eql(combinedPath);
mcp(base, 'test1.json,test2.json', outputPath).should.eql(combinedPath);
mcp(base, 'notafile.yml,test1.json', outputPath).should.eql(combinedPath);
mcp(base, 'notafile.yml,alsonotafile.json', outputPath).should.not.eql(combinedPath);

hexo.log.reader[1].type.should.eql('debug');
hexo.log.reader[1].msg.should.eql(`Writing _multiconfig.yml to ${combinedPath}`);
hexo.log.reader[2].type.should.eql('info');
hexo.log.reader[2].msg.should.eql('Config based on 2 files');
hexo.log.reader[6].type.should.eql('warning');
hexo.log.reader[6].msg.should.eql('Config file notafile.yml not found.');
hexo.log.reader[7].type.should.eql('info');
hexo.log.reader[7].msg.should.eql('Config based on 1 files');
hexo.log.reader[11].type.should.eql('error');
hexo.log.reader[11].msg.should.eql('No config files found.'
+ ' Using _config.yml.');
});
});

0 comments on commit ad33b3c

Please sign in to comment.