Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy351 committed Jan 23, 2015
2 parents bd86ee0 + 4234ba7 commit 1e07226
Show file tree
Hide file tree
Showing 72 changed files with 1,104 additions and 555 deletions.
4 changes: 2 additions & 2 deletions assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"hexo-generator-index": "*",
"hexo-generator-tag": "*",
"hexo-renderer-ejs": "*",
"hexo-renderer-stylus": "*",
"hexo-renderer-marked": "*",
"hexo-renderer-stylus": "^0.2.0",
"hexo-renderer-marked": "^0.2.0",
"hexo-server": "*"
}
}
12 changes: 10 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,22 @@ gulp.task('coverage:clean', function(callback){
del(['coverage/**/*'], callback);
});

gulp.task('mocha', ['coverage'], function(){
function mochaStream(){
return gulp.src('test/index.js')
.pipe($.mocha({
reporter: 'spec'
}))
}));
}

gulp.task('mocha', ['coverage'], function(){
return mochaStream()
.pipe($.istanbul.writeReports());
});

gulp.task('mocha:nocov', function(){
return mochaStream();
});

gulp.task('jshint', function(){
return gulp.src(lib)
.pipe($.jshint())
Expand Down
29 changes: 26 additions & 3 deletions lib/box/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ function File(data){
this.type = data.type;
this.params = data.params;
this.content = data.content;
this.stats = data.stats;
}

function wrapReadOptions(options){
Expand Down Expand Up @@ -62,11 +63,33 @@ File.prototype.readSync = function(options){
}
};

File.prototype.stat = function(callback){
return fs.stat(this.source).nodeify(callback);
File.prototype.stat = function(options, callback){
if (!callback && typeof options === 'function'){
callback = options;
options = {};
}

options = options || {};

var stats = this.stats;
var cache = options.hasOwnProperty('cache') ? options.cache : true;
var self = this;

return new Promise(function(resolve, reject){
if (stats && cache) return resolve(stats);

fs.stat(self.source).then(resolve, reject);
}).nodeify(callback);
};

File.prototype.statSync = function(){
File.prototype.statSync = function(options){
options = options || {};

var cache = options.hasOwnProperty('cache') ? options.cache : true;
var stats = this.stats;

if (stats && cache) return stats;

return fs.statSync(this.source);
};

Expand Down
58 changes: 34 additions & 24 deletions lib/box/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var escapeRegExp = util.escapeRegExp;
var join = pathFn.join;
var sep = pathFn.sep;

var patternNoob = new Pattern(function(){
var defaultPattern = new Pattern(function(){
return {};
});

Expand All @@ -34,7 +34,10 @@ function Box(ctx, base, options){
this.processingFiles = {};
this.watcher = null;
this.Cache = ctx.model('Cache');
// Maybe it will be better to use WeakMap to avoid memory leak?
// But it's still on the road...
this.bufferStore = {};
this.statStore = {};

var _File = this.File = function(data){
File.call(this, data);
Expand Down Expand Up @@ -68,23 +71,21 @@ function Box(ctx, base, options){
};
}

var escapeBackslash;
var escapeBackslash = sep !== '/' ? escapeBackslashWindows : escapeBackslashDefault;

if (sep === '/'){
escapeBackslash = function(path){
return path;
};
} else {
escapeBackslash = function(path){
// Replace backslashes on Windows
return path.replace(/\\/g, '/');
};
function escapeBackslashDefault(path){
return path;
}

function escapeBackslashWindows(path){
// Replace backslashes on Windows
return path.replace(/\\/g, '/');
}

Box.prototype.addProcessor = function(pattern, fn){
if (!fn && typeof pattern === 'function'){
fn = pattern;
pattern = patternNoob;
pattern = defaultPattern;
}

if (typeof fn !== 'function') throw new TypeError('fn must be a function');
Expand All @@ -103,7 +104,7 @@ Box.prototype.process = function(callback){
if (!exist) return;
return self._loadFiles();
}).then(function(files){
if (files) return self._process(files);
if (files && files.length) return self._process(files);
}).nodeify(callback);
};

Expand Down Expand Up @@ -176,7 +177,7 @@ Box.prototype._loadFiles = function(){
});
};

function getChecksum(path){
function getShasum(path){
return new Promise(function(resolve, reject){
var hash = crypto.createHash('sha1');
var buffers = [];
Expand All @@ -194,7 +195,7 @@ function getChecksum(path){
}).on('end', function(){
resolve({
content: Buffer.concat(buffers, length),
checksum: hash.digest('hex')
shasum: hash.digest('hex')
});
}).on('error', reject);
});
Expand All @@ -206,34 +207,41 @@ Box.prototype._handleUpdatedFile = function(path){
var fullPath = join(this.base, path);
var self = this;

return getChecksum(fullPath).then(function(data){
return Promise.all([
getShasum(fullPath),
fs.stat(fullPath)
]).spread(function(data, stats){
var id = escapeBackslash(fullPath.substring(ctx.base_dir.length));
var cache = Cache.findById(id);
var checksum = data.checksum;
var shasum = data.shasum;

self.bufferStore[path] = data.content;
self.statStore[path] = stats;

if (!cache){
ctx.log.info('Added: %s', chalk.magenta(id));
ctx.log.debug('Added: %s', chalk.magenta(id));

return Cache.insert({
_id: id,
checksum: checksum
shasum: shasum,
modified: stats.mtime
}).thenReturn({
type: 'create',
path: path
});
} else if (cache.checksum === checksum){
ctx.log.info('Unchanged: %s', chalk.magenta(id));
} else if (cache.shasum === shasum){
ctx.log.debug('Unchanged: %s', chalk.magenta(id));

return {
type: 'skip',
path: path
};
} else {
ctx.log.info('Updated: %s', chalk.magenta(id));
ctx.log.debug('Updated: %s', chalk.magenta(id));

cache.shasum = shasum;
cache.modified = stats.mtime;

cache.checksum = checksum;
return cache.save().thenReturn({
type: 'update',
path: path
Expand All @@ -248,6 +256,7 @@ Box.prototype._handleDeletedFile = function(path){
var Cache = this.Cache;

this.bufferStore[path] = null;
this.statStore[path] = null;

return new Promise(function(resolve, reject){
var id = escapeBackslash(fullPath.substring(ctx.base_dir.length));
Expand Down Expand Up @@ -301,7 +310,8 @@ Box.prototype._dispatch = function(item){
path: path,
type: item.type,
params: params,
content: self.bufferStore[path]
content: self.bufferStore[path],
stats: self.statStore[path]
});

return Promise.method(processor.process).call(ctx, file).thenReturn(count + 1);
Expand Down
22 changes: 22 additions & 0 deletions lib/cli/find_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var Promise = require('bluebird');
var pathFn = require('path');
var fs = require('hexo-fs');

module.exports = function(cwd, args){
if (args.config) return Promise.resolve();

return findConfigFile(cwd);
};

function findConfigFile(path){
return fs.readdir(path).then(function(files){
for (var i = 0, len = files.length; i < len; i++){
if (files[i].substring(0, 8) === '_config.') return path;
}

var parent = pathFn.dirname(path);
if (parent === path) return;

return findConfigFile(parent);
});
}
48 changes: 16 additions & 32 deletions lib/cli/init.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
var Hexo = require('../hexo');
var pathFn = require('path');
var fs = require('hexo-fs');

var cwd = process.cwd();
var lastCwd = cwd;
var findConfig = require('./find_config');

var byeWords = [
'Good bye',
Expand All @@ -14,36 +10,20 @@ var byeWords = [
'Catch you later'
];

// Find Hexo folder recursively
function findConfigFile(){
// TODO: support for _config.yaml, _config.json or other extension name
return fs.exists(pathFn.join(cwd, '_config.yml')).then(function(exist){
if (exist) return;

lastCwd = cwd;
cwd = pathFn.dirname(cwd);

// Stop on root folder
if (lastCwd === cwd) return;

return findConfigFile();
});
}

function sayGoodbye(){
return byeWords[(Math.random() * byeWords.length) | 0];
}

module.exports = function(args){
var cwd = process.cwd();
var hexo;

findConfigFile().then(function(){
// Use CWD if config file is not found
if (cwd === lastCwd){
hexo = new Hexo(process.cwd(), args);
} else {
hexo = new Hexo(cwd, args);
}
function exit(err){
if (hexo) return hexo.exit(err);
}

findConfig(cwd, args).then(function(path){
hexo = new Hexo(path || cwd, args);

return hexo.init();
}).then(function(){
Expand All @@ -63,17 +43,21 @@ module.exports = function(args){

// Listen to Ctrl+C (SIGINT) signal
process.on('SIGINT', function(){
hexo.log.info(sayGoodbye());
if (!hexo) return process.exit();

hexo.log.info(sayGoodbye());
hexo.unwatch();

hexo.exit().then(function(){
exit().then(function(){
process.exit();
});
});

return hexo.call(command, args);
}).then(function(err){
return hexo.exit(err);
}).then(function(){
return exit();
}, function(err){
hexo.unwatch();
return exit(err);
});
};
Loading

0 comments on commit 1e07226

Please sign in to comment.