Skip to content

Commit

Permalink
Load file status before Box.process
Browse files Browse the repository at this point in the history
- Remove File.changed method
- There's no big difference to load file status in demand so I bring back file type "skip".
  • Loading branch information
tommy351 committed Feb 27, 2016
1 parent 50c2649 commit 8f35f15
Show file tree
Hide file tree
Showing 12 changed files with 294 additions and 427 deletions.
87 changes: 39 additions & 48 deletions lib/box/index.js
Expand Up @@ -58,13 +58,10 @@ function getHash(path) {
}

Box.prototype._createFileClass = function() {
var Cache = this.Cache;
var ctx = this.context;

var _File = function(data) {
File.call(this, data);

this._typeSolved = data.type === File.TYPE_DELETE;
};

require('util').inherits(_File, File);
Expand All @@ -77,48 +74,17 @@ Box.prototype._createFileClass = function() {
options = {};
}

var self = this;

return this.read().then(function(content) {
return ctx.render.render({
text: content,
path: self.source
}, options);
}).asCallback(callback);
return ctx.render.render({
path: this.source
}, options).asCallback(callback);
};

_File.prototype.renderSync = function(options) {
return ctx.render.renderSync({
text: this.readSync(),
path: this.source
}, options);
};

_File.prototype.changed = function(callback) {
if (this._typeSolved) {
return Promise.resolve(this.type !== File.TYPE_SKIP).asCallback(callback);
}

var self = this;

// TODO: Share file type between processors
return Cache.compareFile(
this.source.substring(ctx.base_dir.length),
function() {
return getHash(self.source);
},

function() {
return self.stat();
}
).then(function(result) {
self.type = result.type;
self._typeSolved = true;

return result.type !== File.TYPE_SKIP;
}).asCallback(callback);
};

return _File;
};

Expand All @@ -137,25 +103,50 @@ Box.prototype.addProcessor = function(pattern, fn) {
});
};

function readDir(base, fn, prefix) {
Box.prototype._readDir = function(base, fn, prefix) {
prefix = prefix || '';

var self = this;

return fs.readdir(base).map(function(path) {
return fs.stat(join(base, path)).then(function(stats) {
if (stats.isDirectory()) {
return readDir(join(base, path), fn, prefix + path + '/');
return self._readDir(join(base, path), fn, prefix + path + '/');
}

var relativePath = prefix + path;
return fn(relativePath).thenReturn(relativePath);
return self._checkFileStatus(prefix + path).then(function(file) {
return fn(file).thenReturn(file);
});
});
}).catch(function(err) {
if (err.cause && err.cause.code === 'ENOENT') return;
throw err;
}).reduce(function(files, item) {
return files.concat(item);
}, []);
}
};

Box.prototype._checkFileStatus = function(path) {
var Cache = this.Cache;
var src = join(this.base, path);
var ctx = this.context;

return Cache.compareFile(
src.substring(ctx.base_dir.length),
function() {
return getHash(src);
},

function() {
return fs.stat(src);
}
).then(function(result) {
return {
type: result.type,
path: path
};
});
};

Box.prototype.process = function(callback) {
var self = this;
Expand All @@ -175,10 +166,10 @@ Box.prototype.process = function(callback) {
});

// Read files from directory
return readDir(base, function(path) {
// If the files is not in the cache, means it's new
var type = ~cacheFiles.indexOf(path) ? File.TYPE_UPDATE : File.TYPE_CREATE;
return self._processFile(type, path);
return self._readDir(base, function(file) {
return self._processFile(file.type, file.path);
}).map(function(file) {
return file.path;
}).then(function(files) {
// Handle deleted files
return Promise.filter(cacheFiles, function(path) {
Expand Down Expand Up @@ -273,8 +264,8 @@ Box.prototype.watch = function(callback) {
var prefix = getPath(path);
if (prefix) prefix += '/';

readDir(path, function(path) {
return self._processFile(File.TYPE_CREATE, path);
self._readDir(path, function(file) {
return self._processFile(file.type, file.path);
}, prefix);
});
}).asCallback(callback);
Expand Down
104 changes: 51 additions & 53 deletions lib/plugins/processor/asset.js
Expand Up @@ -15,6 +15,10 @@ module.exports = function(ctx) {
var config = ctx.config;
var timezone = config.timezone;

if (file.type === 'skip' && doc) {
return;
}

if (file.type === 'delete') {
if (doc) {
return doc.remove();
Expand All @@ -23,65 +27,61 @@ module.exports = function(ctx) {
return;
}

return file.changed().then(function(changed) {
if (!changed && doc) return;

return Promise.all([
file.stat(),
file.read()
]).spread(function(stats, content) {
var data = yfm(content);
var output = ctx.render.getOutput(path);
return Promise.all([
file.stat(),
file.read()
]).spread(function(stats, content) {
var data = yfm(content);
var output = ctx.render.getOutput(path);

data.source = path;
data.raw = content;
data.source = path;
data.raw = content;

data.date = common.toDate(data.date);
data.date = common.toDate(data.date);

if (data.date) {
if (timezone) data.date = common.timezone(data.date, timezone);
} else {
data.date = stats.ctime;
}

data.updated = common.toDate(data.updated);
if (data.date) {
if (timezone) data.date = common.timezone(data.date, timezone);
} else {
data.date = stats.ctime;
}

if (data.updated) {
if (timezone) data.updated = common.timezone(data.updated, timezone);
} else {
data.updated = stats.mtime;
}
data.updated = common.toDate(data.updated);

if (data.permalink) {
data.path = data.permalink;
delete data.permalink;
if (data.updated) {
if (timezone) data.updated = common.timezone(data.updated, timezone);
} else {
data.updated = stats.mtime;
}

if (data.path[data.path.length - 1] === '/') {
data.path += 'index';
}
if (data.permalink) {
data.path = data.permalink;
delete data.permalink;

if (!pathFn.extname(data.path)) {
data.path += '.' + output;
}
} else {
var extname = pathFn.extname(path);
data.path = path.substring(0, path.length - extname.length) + '.' + output;
if (data.path[data.path.length - 1] === '/') {
data.path += 'index';
}

if (!data.layout && output !== 'html' && output !== 'htm') {
data.layout = false;
if (!pathFn.extname(data.path)) {
data.path += '.' + output;
}
} else {
var extname = pathFn.extname(path);
data.path = path.substring(0, path.length - extname.length) + '.' + output;
}

// FIXME: Data may be inserted when reading files. Load it again to prevent
// race condition. We have to solve this in warehouse.
var doc = Page.findOne({source: path});
if (!data.layout && output !== 'html' && output !== 'htm') {
data.layout = false;
}

if (doc) {
return doc.replace(data);
}
// FIXME: Data may be inserted when reading files. Load it again to prevent
// race condition. We have to solve this in warehouse.
var doc = Page.findOne({source: path});

if (doc) {
return doc.replace(data);
}

return Page.insert(data);
});
return Page.insert(data);
});
}

Expand All @@ -98,13 +98,11 @@ module.exports = function(ctx) {
return;
}

return file.changed().then(function(changed) {
return Asset.save({
_id: id,
path: file.path,
modified: changed,
renderable: file.params.renderable
});
return Asset.save({
_id: id,
path: file.path,
modified: file.type !== 'skip',
renderable: file.params.renderable
});
}

Expand Down
10 changes: 5 additions & 5 deletions lib/plugins/processor/data.js
Expand Up @@ -14,6 +14,10 @@ module.exports = function(ctx) {
var id = path.substring(0, path.length - extname.length);
var doc = Data.findById(id);

if (file.type === 'skip' && doc) {
return;
}

if (file.type === 'delete') {
if (doc) {
return doc.remove();
Expand All @@ -22,11 +26,7 @@ module.exports = function(ctx) {
return;
}

return file.changed().then(function(changed) {
if (!changed && doc) return;

return file.render();
}).then(function(result) {
return file.render().then(function(result) {
if (result == null) return;

return Data.save({
Expand Down

0 comments on commit 8f35f15

Please sign in to comment.