Skip to content

Commit

Permalink
Fixed hexo._generate() throwing error when generators returns nothing.
Browse files Browse the repository at this point in the history
…Fixed #1000
  • Loading branch information
tommy351 committed Jan 24, 2015
1 parent 45f39df commit f1d896f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
5 changes: 1 addition & 4 deletions lib/box/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var _ = require('lodash');
var File = require('./file');
var util = require('hexo-util');
var fs = require('hexo-fs');
var prettyHrtime = require('pretty-hrtime');
var crypto = require('crypto');
var chalk = require('chalk');

Expand Down Expand Up @@ -291,7 +290,6 @@ Box.prototype._dispatch = function(item){
var self = this;
var ctx = this.context;
var base = this.base;
var start = process.hrtime();

// Skip processing files
if (this.processingFiles[path]) return;
Expand All @@ -317,8 +315,7 @@ Box.prototype._dispatch = function(item){
return Promise.method(processor.process).call(ctx, file).thenReturn(count + 1);
}, 0).then(function(count){
if (count){
var interval = prettyHrtime(process.hrtime(start));
ctx.log.debug('Processed in %s: %s', chalk.cyan(interval), chalk.magenta(path));
ctx.log.debug('Processed: %s', chalk.magenta(path));
}
}, function(err){
ctx.log.error({err: err}, 'Process failed: %s', chalk.magenta(path));
Expand Down
12 changes: 4 additions & 8 deletions lib/hexo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ var Router = require('./router');
var Theme = require('../theme');
var defaultConfig = require('./default_config');
var loadDatabase = require('./load_database');
var prettyHrtime = require('pretty-hrtime');

var libDir = pathFn.dirname(__dirname);
var sep = pathFn.sep;
Expand Down Expand Up @@ -301,20 +300,17 @@ Hexo.prototype._generate = function(options){
// Run generators
return Promise.reduce(keys, function(result, key){
var generator = generators[key];
var start = process.hrtime();

return generator.call(self, siteLocals).then(function(data){
var interval = prettyHrtime(process.hrtime(start));
log.debug('Generator: %s', chalk.magenta(key));

log.debug('Generator in %s: %s', chalk.cyan(interval), chalk.magenta(key));

return data ? result.concat(data) : data;
return data ? result.concat(data) : result;
});
}, []);
})
// Add routes
.each(function(item){
// Add routes
if (item.path == null) return;
if (typeof item !== 'object' || item.path == null) return;

var path = route.format(item.path);
var data = item.data;
Expand Down
4 changes: 1 addition & 3 deletions lib/plugins/console/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ function generateConsole(args){
function generateFile(path){
var data = route.get(path);
var dest = pathFn.join(publicDir, path);
var start = process.hrtime();

// TODO: Retry when EMFILE error occurred
return fs.ensureWriteStream(dest).then(function(stream){
return pipeStream(data, stream);
}).then(function(){
var interval = prettyHrtime(process.hrtime(start));
log.info('Generated in %s: %s', chalk.cyan(interval), chalk.magenta(path));
log.info('Generated: %s', chalk.magenta(path));
});
}

Expand Down
17 changes: 17 additions & 0 deletions test/scripts/hexo/hexo.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,23 @@ describe('Hexo', function(){
});
});

it('_generate() - return nothing in generator', function(){
hexo.extend.generator.register('test_nothing', function(){
//
});

hexo.extend.generator.register('test_normal', function(){
return {
path: 'bar',
data: 'bar'
};
});

return hexo._generate().then(function(){
return checkStream(route.get('bar'), 'bar');
});
});

it('_generate() - validate locals');

it('_generate() - do nothing if it\'s generating');
Expand Down

0 comments on commit f1d896f

Please sign in to comment.