Skip to content

Commit

Permalink
Refactor set and fix issue with dependent rebuilds
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmerfield committed Apr 15, 2021
1 parent 55e0950 commit b30db2e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 31 deletions.
2 changes: 1 addition & 1 deletion app/sync/update/rebuildDependents.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = function (blogID, path, callback) {
async.eachSeries(
dependent_paths,
function (dependent_path, next) {
Entry.get(blogID, dependent_path, function (err, entry) {
Entry.get(blogID, dependent_path, function (entry) {
if (err) {
console.log("Error rebuilding dependents:", path, err);
return next();
Expand Down
67 changes: 37 additions & 30 deletions app/sync/update/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,28 @@ function isTemplate(path) {
return normalize(path).indexOf("/templates/") === 0;
}

module.exports = function (blog, path, options, callback) {
var queue;
function buildAndSet(blog, path, options, callback) {
build(blog, path, options, function (err, entry) {
if (err && err.code === "WRONGTYPE")
return Ignore(blog.id, path, WRONG_TYPE, callback);

if (err) return callback(err);

Entry.set(blog.id, entry.path, entry, function (err) {
if (err) return callback(err);
// This file is a draft, write a preview file
// to the users Dropbox and continue down
// We look up the remote path later in this module...
if (entry.draft && !isHidden(entry.path)) {
Preview.write(blog.id, path, callback);
} else {
callback();
}
});
});
}

module.exports = function (blog, path, options, callback) {
if (callback === undefined && typeof options === "function") {
callback = options;
options = {};
Expand All @@ -39,45 +58,33 @@ module.exports = function (blog, path, options, callback) {
// Blot likes leading slashes
if (path[0] !== "/") path = "/" + path;

queue = {
is_preview: isPreview.bind(this, blog.id, path),
dependents: rebuildDependents.bind(this, blog.id, path),
};
var queue = {};

// Store the case-preserved name against the
// path to this file
if (options.name) {
queue.metadata = Metadata.add.bind(this, blog.id, path, options.name);
}

async.parallel(queue, function (err, result) {
isPreview(blog.id, path, function (err, is_preview) {
if (err) return callback(err);

// This is a preview file, don't create an entry
if (result.is_preview) return callback();

// The file belongs to a template and there
// fore should not become a blog post.
if (isTemplate(path)) return callback();
// Store the case-preserved name against the
// path to this file
if (options.name) {
queue.metadata = Metadata.add.bind(this, blog.id, path, options.name);
}

// The file is public. Its name begins
// with an underscore, or it's inside a folder
// whose name begins with an underscore. It should
// therefore not be a blog post.
if (isPublic(path)) return Ignore(blog.id, path, PUBLIC_FILE, callback);
if (isPublic(path)) {
queue.ignore = Ignore.bind(this, blog.id, path, PUBLIC_FILE);
}

build(blog, path, options, function (err, entry) {
if (err && err.code === "WRONGTYPE")
return Ignore(blog.id, path, WRONG_TYPE, callback);
// This file should become a blog post or page!
if (!isPublic(path) && !isTemplate(path) && !is_preview) {
queue.buildAndSet = buildAndSet.bind(this, blog, path, options);
}

async.parallel(queue, function (err) {
if (err) return callback(err);

// This file is a draft, write a preview file
// to the users Dropbox and continue down
// We look up the remote path later in this module...
if (entry.draft && !isHidden(entry.path)) Preview.write(blog.id, path);

Entry.set(blog.id, entry.path, entry, callback);
rebuildDependents(blog.id, path, callback);
});
});
};

0 comments on commit b30db2e

Please sign in to comment.