Skip to content

Commit

Permalink
Force regenerate. Resolve #1233
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy351 committed Apr 27, 2015
1 parent 997988d commit 5d5092c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/plugins/console/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var join = pathFn.join;

function generateConsole(args){
/* jshint validthis: true */
var force = args.f || args.force;
var route = this.route;
var publicDir = this.public_dir;
var log = this.log;
Expand All @@ -23,10 +24,10 @@ function generateConsole(args){
var dest = join(publicDir, path);

return fs.exists(dest).then(function(exist){
if (exist && !route.isModified(path)) return;
if (!force && exist && !route.isModified(path)) return;

return checkShasum(path).then(function(changed){
if (changed || !exist) return writeFile(path);
if (force || changed || !exist) return writeFile(path);
});
});
}
Expand Down
1 change: 1 addition & 0 deletions lib/plugins/console/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = function(ctx){
console.register('generate', 'Generate static files.', {
options: [
{name: '-d, --deploy', desc: 'Deploy after generated'},
{name: '-f, --force', desc: 'Force regenerate'},
{name: '-w, --watch', desc: 'Watch file changes'}
]
}, require('./generate'));
Expand Down
30 changes: 30 additions & 0 deletions test/scripts/console/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,36 @@ describe('generate', function(){
});
});

it('force regenerate', function(){
var src = pathFn.join(hexo.source_dir, 'test.txt');
var dest = pathFn.join(hexo.public_dir, 'test.txt');
var content = 'test';
var mtime;

return fs.writeFile(src, content).then(function(){
// First generation
return generate({});
}).then(function(){
// Read file status
return fs.stat(dest);
}).then(function(stats){
mtime = stats.mtime.getTime();
}).delay(1000).then(function(){
// Force regenerate
return generate({force: true});
}).then(function(){
return fs.stat(dest);
}).then(function(stats){
stats.mtime.getTime().should.be.above(mtime);

// Remove source files and generated files
return Promise.all([
fs.unlink(src),
fs.unlink(dest)
]);
});
});

it('watch - update', function(){
var src = pathFn.join(hexo.source_dir, 'test.txt');
var dest = pathFn.join(hexo.public_dir, 'test.txt');
Expand Down

0 comments on commit 5d5092c

Please sign in to comment.