Permalink
Browse files

Made SFTP publisher into a seperate plugin - https://github.com/lakte…

  • Loading branch information...
1 parent 8f4dfcd commit 7cd7ca12bebbbca1468fc0fd9e2aa2be367dff37 @laktek committed Nov 8, 2012
Showing with 1 addition and 319 deletions.
  1. +1 −2 lib/default_config.js
  2. +0 −1 lib/index.js
  3. +0 −120 lib/publishers/sftp.js
  4. +0 −1 package.json
  5. +0 −195 spec/publishers/sftp.spec.js
View
@@ -38,8 +38,7 @@ module.exports = {
},
publishers: {
- "s3": path.join(__dirname, "publishers/s3.js"),
- "sftp": path.join(__dirname, "publishers/sftp.js")
+ "s3": path.join(__dirname, "publishers/s3.js")
},
compilers: {
View
@@ -37,7 +37,6 @@ exports.Parsers.Markdown = require("./parsers/markdown.js");
exports.Publishers = {};
exports.Publishers.S3 = require("./publishers/s3.js");
-exports.Publishers.Sftp = require("./publishers/sftp.js");
exports.TemplateEngines = {};
exports.TemplateEngines.Base = require("./template_engines/base_engine.js");
View
@@ -1,120 +0,0 @@
-var _ = require("underscore");
-var fs = require("fs");
-var path = require("path");
-var Sftp = require("sftp");
-var DeepFstream = require("../utils/deep_fstream");
-
-module.exports = {
-
- client: null,
-
- timeoutId: null,
-
- retrieveOptions: function(supplied_config){
- var error = "Cannot find sftp 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 );
- },
-
- connectToRemote: function(supplied_config, callback){
- // correct the private key
- if(_.has(supplied_config, "private_key")){
- supplied_config["privateKey"] = supplied_config["private_key"];
- }
-
- return new Sftp(supplied_config, callback);
- },
-
- checkAndCreateRemoteDirectory: function(remote_dir_path, callback){
- var self = this;
-
- // check for the directory in remote host
- self.client.stat(remote_dir_path, function(err, stats){
- if(err || !stats.isDirectory()){
- // create directory
- self.client.mkdir(remote_dir_path, 0755, function(err){
- if (err) {
- throw err;
- }
-
- // directory created
- // proceed with traversing files in the directory
- return callback();
- });
- } else {
- // directory exists in remote host
- // proceed with traversing files in the directory
- return callback();
- }
- });
- },
-
- uploadFile: function(local_path, remote_path, callback){
- var self = this;
-
- fs.readFile(local_path, function(error, buf){
- if (error) {
- throw error;
- }
-
- self.client.writeFile(remote_path, buf, function(err){
- if(err) {
- throw err;
- }
-
- console.log("saved to %s", remote_path);
- return callback();
- });
- });
- },
-
- publish: function(supplied_config, last_published_date, complete){
-
- var self = this;
-
- var retrieved_options = self.retrieveOptions(supplied_config);
- var upload_path = retrieved_options.upload_path || "./";
- var output_dir = path.join(process.cwd(), supplied_config.output_dir);
-
- self.lastPublishedDate = last_published_date;
-
- self.client = self.connectToRemote(retrieved_options, function() {
-
- var file_stream = new DeepFstream(output_dir);
-
- file_stream.on("directory", function(entry, callback) {
- var remote_dir_path = path.normalize(entry.path.replace(output_dir, upload_path));
- self.checkAndCreateRemoteDirectory(remote_dir_path, callback);
- });
-
- file_stream.on("file", function(entry, callback) {
- if (self.isModified(entry.props.mtime)) {
- var remote_path = path.normalize(entry.path.replace(output_dir, upload_path));
- self.uploadFile(entry.path, remote_path, callback);
- } else {
- return callback();
- }
- });
-
- file_stream.on("end", function() {
- self.client.disconnect(function() {
- return complete();
- });
- });
-
- });
- }
-
-};
-
-
View
@@ -37,7 +37,6 @@
"marked": ">= 0.2.5",
"mime": ">= 1.2.5",
"mustache": ">0.7.0 || >=0.5.2 <0.7.0",
- "sftp": "git://github.com/laktek/node-sftp.git",
"uglify-js": ">= 1.3.3",
"underscore": ">= 1.4.2"
},
@@ -1,195 +0,0 @@
-var fs = require("fs");
-var sftp_publisher = require("../../lib/publishers/sftp.js");
-
-describe("calling publish", function(){
-
- it("should retrieve sftp options from the config", function(){
-
- var supplied_config = {"output_dir": "path/output_dir"};
-
- spyOn(sftp_publisher, "retrieveOptions").andReturn({"upload_path": "public_html/site"});
- spyOn(sftp_publisher, "connectToRemote");
-
- sftp_publisher.publish(supplied_config);
-
- expect(sftp_publisher.retrieveOptions).toHaveBeenCalledWith(supplied_config);
-
- });
-
- it("should initiate the connection to remote host", function(){
-
- var supplied_config = {"output_dir": "path/output_dir"};
-
- spyOn(sftp_publisher, "retrieveOptions").andReturn({"upload_path": "public_html/site"});
- spyOn(sftp_publisher, "connectToRemote");
-
- sftp_publisher.publish(supplied_config);
-
- expect(sftp_publisher.connectToRemote).toHaveBeenCalled();
-
- });
-
-});
-
-describe("retrieve the sftp options from the config", function(){
-
- it("returns the sftp options defined in publish section of config", function(){
-
- var sftp_config = {"username": "mike", "password": "mike1324"};
- var supplied_config = {"publish": { "strategy": "sftp", "options": sftp_config }};
-
- expect(sftp_publisher.retrieveOptions(supplied_config)).toEqual(sftp_config);
-
- });
-
- it("throws an error if config doesn't contain options for sftp", function(){
-
- var supplied_config = {"publish": { }};
- var error = "Cannot find sftp settings in config";
-
- expect(function(){ sftp_publisher.retrieveOptions(supplied_config) }).toThrow(error);
-
- });
-
- it("throws an error if config doesn't define a publish section", function(){
-
- var supplied_config = {};
- var error = "Cannot find sftp settings in config";
-
- expect(function(){ sftp_publisher.retrieveOptions(supplied_config) }).toThrow(error);
-
- });
-
-});
-
-describe("check if a file is modified", function() {
-
- it("return true if file modified date is newer than last published date", function() {
- sftp_publisher.lastPublishedDate = new Date(2012, 6, 25);
-
- expect(sftp_publisher.isModified(new Date(2012, 6, 30))).toEqual(true);
- });
-
-});
-
-describe("check and create a remote directory", function(){
-
- it("takes the stat for the remote directory", function(){
- var spy_stat = jasmine.createSpy();
- var spy_callback = jasmine.createSpy();
- sftp_publisher.client = {stat: spy_stat};
-
- sftp_publisher.checkAndCreateRemoteDirectory("public_html/site", spy_callback);
- expect(spy_stat).toHaveBeenCalled();
- });
-
- it("executes the callback if remote directory already exists", function(){
- var spy_stat = jasmine.createSpy();
- spy_stat.andCallFake(function(path, cbk){
- cbk(null, {isDirectory: function(){ return true} });
- });
- var spy_callback = jasmine.createSpy();
- sftp_publisher.client = {stat: spy_stat};
-
- sftp_publisher.checkAndCreateRemoteDirectory("public_html/site", spy_callback);
- expect(spy_callback).toHaveBeenCalled();
- });
-
- it("creates a remote directory if it doesn't exists", function(){
- var spy_stat = jasmine.createSpy();
- spy_stat.andCallFake(function(path, cbk){
- cbk("error", null);
- });
- var spy_mkdir = jasmine.createSpy();
- var spy_callback = jasmine.createSpy();
- sftp_publisher.client = {stat: spy_stat, mkdir: spy_mkdir};
-
- sftp_publisher.checkAndCreateRemoteDirectory("public_html/site", spy_callback);
- expect(spy_mkdir.mostRecentCall.args[0]).toEqual("public_html/site");
- });
-
- it("executes the callback after creating the remote directory", function(){
- var spy_stat = jasmine.createSpy();
- spy_stat.andCallFake(function(path, cbk){
- cbk("error", null);
- });
- var spy_mkdir = jasmine.createSpy();
- spy_mkdir.andCallFake(function(path, mode, cbk){
- cbk();
- });
- var spy_callback = jasmine.createSpy();
- sftp_publisher.client = {stat: spy_stat, mkdir: spy_mkdir};
-
- sftp_publisher.checkAndCreateRemoteDirectory("public_html/site", spy_callback);
- expect(spy_callback).toHaveBeenCalled();
- });
-
- it("throws an exception if there's an error in creating the remote directory", function(){
- var spy_stat = jasmine.createSpy();
- spy_stat.andCallFake(function(path, cbk){
- cbk("error", null);
- });
- var spy_mkdir = jasmine.createSpy();
- spy_mkdir.andCallFake(function(path, mode, cbk){
- cbk("error");
- });
- var spy_callback = jasmine.createSpy();
- sftp_publisher.client = {stat: spy_stat, mkdir: spy_mkdir};
-
- expect(function(){sftp_publisher.checkAndCreateRemoteDirectory("public_html/site", spy_callback)}).toThrow();
- });
-
-});
-
-describe("upload file", function() {
-
- it("reads the file in given path", function(){
- spyOn(fs, "readFile");
- var spy_callback = jasmine.createSpy();
-
- sftp_publisher.uploadFile("output/file", "public_html/site", spy_callback);
- expect(fs.readFile.mostRecentCall.args[0]).toEqual("output/file");
- });
-
- it("writes the file to the given remote path", function(){
- spyOn(fs, "readFile").andCallFake(function(path, callback){
- callback(null, "content");
- });
- var spy_writefile = jasmine.createSpy();
- var spy_callback = jasmine.createSpy();
- sftp_publisher.client = {writeFile: spy_writefile};
-
- sftp_publisher.uploadFile("output/file", "public_html/site", spy_callback);
- expect(spy_writefile.mostRecentCall.args[0]).toEqual("public_html/site");
- });
-
- it("executes the callback after writing the file", function(){
- spyOn(fs, "readFile").andCallFake(function(path, callback){
- callback(null, "content");
- });
- var spy_writefile = jasmine.createSpy();
- spy_writefile.andCallFake(function(path, buffer, callback){
- callback(null);
- });
- var spy_callback = jasmine.createSpy();
- sftp_publisher.client = {writeFile: spy_writefile};
-
- sftp_publisher.uploadFile("output/file", "public_html/site", spy_callback);
- expect(spy_callback).toHaveBeenCalled();
- });
-
- it("throws an exception if there's an error in writing the file", function(){
- spyOn(fs, "readFile").andCallFake(function(path, callback){
- callback(null, "content");
- });
- var spy_writefile = jasmine.createSpy();
- spy_writefile.andCallFake(function(path, buffer, callback){
- callback("error");
- });
- var spy_callback = jasmine.createSpy();
- sftp_publisher.client = {writeFile: spy_writefile};
-
- expect(function(){ sftp_publisher.uploadFile("output/file", "public_html/site", spy_callback) }).toThrow();
- });
-
-});

0 comments on commit 7cd7ca1

Please sign in to comment.