Created
March 9, 2014 21:12
-
-
Save avalez/9454636 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _ = require("underscore"); | |
var gift = require('gift') | |
var DeepFstream = require("punch").Utils.DeepFstream; | |
var Path = require("path"); | |
var fs = require('fs-extra'); | |
module.exports = { | |
repo: null, | |
lastPublishedDate: null, | |
publishOptions: null, | |
retrieveOptions: function(supplied_config) { | |
var self = this; | |
var error = "Cannot find git publisher settings in config"; | |
if (_.has(supplied_config, "publish") && _.has(supplied_config["publish"], "options")) { | |
return supplied_config["publish"]["options"]; | |
} else { | |
throw error; | |
} | |
}, | |
isModified: function(modified_date) { | |
var self = this; | |
return ( modified_date > self.lastPublishedDate ); | |
}, | |
copyFiles: function(supplied_config, complete) { | |
var self = this; | |
var output_dir = (supplied_config && supplied_config.output_dir) || ""; | |
var output_dir_path = Path.join(process.cwd(), output_dir); | |
var file_stream = new DeepFstream(output_dir_path); | |
file_stream.on("directory", function(entry, callback) { | |
return callback(); | |
}); | |
file_stream.on("file", function(entry, callback) { | |
if (self.isModified(entry.props.mtime)) { | |
var entry_path = Path.relative(output_dir_path, entry.path); | |
var source_path = Path.join(output_dir, entry_path); | |
var target_path = Path.join(self.repo.path, entry_path); | |
return fs.copy(source_path, target_path, function(err) { | |
if (err) { | |
console.log("Error occured in copying to %s", target_path); | |
} | |
self.repo.add(entry_path, callback); | |
}); | |
} else { | |
return callback(); | |
} | |
}); | |
file_stream.on("end", function() { | |
return self.repo.commit("automatic commit by " + process.env.BUILD_TAG, function(err) { | |
if (err) { | |
console.log("Error comitting update %s", process.env.BUILD_TAG); | |
complete(); | |
} else { | |
self.repo.remote_push('origin', complete); | |
} | |
}); | |
}); | |
}, | |
publish: function(supplied_config, last_published_date, callback) { | |
var self = this; | |
self.publishOptions = self.retrieveOptions(supplied_config); | |
console.log(self.publishOptions); | |
var repo_url = self.publishOptions.repo_url; | |
if (!repo_url) { | |
throw "repo_url config option is requires"; | |
} | |
var repo_dir = (self.publishOptions.repo_dir) || "target"; | |
self.repo = gift(repo_dir); | |
self.lastPublishedDate = last_published_date; | |
return self.repo.status(function(err, status) { | |
if (err) { | |
gift.clone(repo_url, repo_dir, function(err, repo) { | |
if (err) { | |
console.log("Error cloning git repository:", err); | |
callback(); | |
} else { | |
self.repo = repo; | |
return self.copyFiles(supplied_config, callback); | |
} | |
}); | |
} else { | |
return self.copyFiles(supplied_config, callback); | |
} | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment