Skip to content
This repository
Browse code

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

  • Loading branch information...
commit 7cd7ca12bebbbca1468fc0fd9e2aa2be367dff37 1 parent 8f4dfcd
Lakshan Perera authored
3  lib/default_config.js
@@ -38,8 +38,7 @@ module.exports = {
38 38 },
39 39
40 40 publishers: {
41   - "s3": path.join(__dirname, "publishers/s3.js"),
42   - "sftp": path.join(__dirname, "publishers/sftp.js")
  41 + "s3": path.join(__dirname, "publishers/s3.js")
43 42 },
44 43
45 44 compilers: {
1  lib/index.js
@@ -37,7 +37,6 @@ exports.Parsers.Markdown = require("./parsers/markdown.js");
37 37
38 38 exports.Publishers = {};
39 39 exports.Publishers.S3 = require("./publishers/s3.js");
40   -exports.Publishers.Sftp = require("./publishers/sftp.js");
41 40
42 41 exports.TemplateEngines = {};
43 42 exports.TemplateEngines.Base = require("./template_engines/base_engine.js");
120 lib/publishers/sftp.js
... ... @@ -1,120 +0,0 @@
1   -var _ = require("underscore");
2   -var fs = require("fs");
3   -var path = require("path");
4   -var Sftp = require("sftp");
5   -var DeepFstream = require("../utils/deep_fstream");
6   -
7   -module.exports = {
8   -
9   - client: null,
10   -
11   - timeoutId: null,
12   -
13   - retrieveOptions: function(supplied_config){
14   - var error = "Cannot find sftp settings in config";
15   -
16   - if (_.has(supplied_config, "publish") && _.has(supplied_config["publish"], "options")) {
17   - return supplied_config["publish"]["options"];
18   - } else {
19   - throw error;
20   - }
21   - },
22   -
23   - isModified: function(modified_date) {
24   - var self = this;
25   -
26   - return ( modified_date > self.lastPublishedDate );
27   - },
28   -
29   - connectToRemote: function(supplied_config, callback){
30   - // correct the private key
31   - if(_.has(supplied_config, "private_key")){
32   - supplied_config["privateKey"] = supplied_config["private_key"];
33   - }
34   -
35   - return new Sftp(supplied_config, callback);
36   - },
37   -
38   - checkAndCreateRemoteDirectory: function(remote_dir_path, callback){
39   - var self = this;
40   -
41   - // check for the directory in remote host
42   - self.client.stat(remote_dir_path, function(err, stats){
43   - if(err || !stats.isDirectory()){
44   - // create directory
45   - self.client.mkdir(remote_dir_path, 0755, function(err){
46   - if (err) {
47   - throw err;
48   - }
49   -
50   - // directory created
51   - // proceed with traversing files in the directory
52   - return callback();
53   - });
54   - } else {
55   - // directory exists in remote host
56   - // proceed with traversing files in the directory
57   - return callback();
58   - }
59   - });
60   - },
61   -
62   - uploadFile: function(local_path, remote_path, callback){
63   - var self = this;
64   -
65   - fs.readFile(local_path, function(error, buf){
66   - if (error) {
67   - throw error;
68   - }
69   -
70   - self.client.writeFile(remote_path, buf, function(err){
71   - if(err) {
72   - throw err;
73   - }
74   -
75   - console.log("saved to %s", remote_path);
76   - return callback();
77   - });
78   - });
79   - },
80   -
81   - publish: function(supplied_config, last_published_date, complete){
82   -
83   - var self = this;
84   -
85   - var retrieved_options = self.retrieveOptions(supplied_config);
86   - var upload_path = retrieved_options.upload_path || "./";
87   - var output_dir = path.join(process.cwd(), supplied_config.output_dir);
88   -
89   - self.lastPublishedDate = last_published_date;
90   -
91   - self.client = self.connectToRemote(retrieved_options, function() {
92   -
93   - var file_stream = new DeepFstream(output_dir);
94   -
95   - file_stream.on("directory", function(entry, callback) {
96   - var remote_dir_path = path.normalize(entry.path.replace(output_dir, upload_path));
97   - self.checkAndCreateRemoteDirectory(remote_dir_path, callback);
98   - });
99   -
100   - file_stream.on("file", function(entry, callback) {
101   - if (self.isModified(entry.props.mtime)) {
102   - var remote_path = path.normalize(entry.path.replace(output_dir, upload_path));
103   - self.uploadFile(entry.path, remote_path, callback);
104   - } else {
105   - return callback();
106   - }
107   - });
108   -
109   - file_stream.on("end", function() {
110   - self.client.disconnect(function() {
111   - return complete();
112   - });
113   - });
114   -
115   - });
116   - }
117   -
118   -};
119   -
120   -
1  package.json
@@ -37,7 +37,6 @@
37 37 "marked": ">= 0.2.5",
38 38 "mime": ">= 1.2.5",
39 39 "mustache": ">0.7.0 || >=0.5.2 <0.7.0",
40   - "sftp": "git://github.com/laktek/node-sftp.git",
41 40 "uglify-js": ">= 1.3.3",
42 41 "underscore": ">= 1.4.2"
43 42 },
195 spec/publishers/sftp.spec.js
... ... @@ -1,195 +0,0 @@
1   -var fs = require("fs");
2   -var sftp_publisher = require("../../lib/publishers/sftp.js");
3   -
4   -describe("calling publish", function(){
5   -
6   - it("should retrieve sftp options from the config", function(){
7   -
8   - var supplied_config = {"output_dir": "path/output_dir"};
9   -
10   - spyOn(sftp_publisher, "retrieveOptions").andReturn({"upload_path": "public_html/site"});
11   - spyOn(sftp_publisher, "connectToRemote");
12   -
13   - sftp_publisher.publish(supplied_config);
14   -
15   - expect(sftp_publisher.retrieveOptions).toHaveBeenCalledWith(supplied_config);
16   -
17   - });
18   -
19   - it("should initiate the connection to remote host", function(){
20   -
21   - var supplied_config = {"output_dir": "path/output_dir"};
22   -
23   - spyOn(sftp_publisher, "retrieveOptions").andReturn({"upload_path": "public_html/site"});
24   - spyOn(sftp_publisher, "connectToRemote");
25   -
26   - sftp_publisher.publish(supplied_config);
27   -
28   - expect(sftp_publisher.connectToRemote).toHaveBeenCalled();
29   -
30   - });
31   -
32   -});
33   -
34   -describe("retrieve the sftp options from the config", function(){
35   -
36   - it("returns the sftp options defined in publish section of config", function(){
37   -
38   - var sftp_config = {"username": "mike", "password": "mike1324"};
39   - var supplied_config = {"publish": { "strategy": "sftp", "options": sftp_config }};
40   -
41   - expect(sftp_publisher.retrieveOptions(supplied_config)).toEqual(sftp_config);
42   -
43   - });
44   -
45   - it("throws an error if config doesn't contain options for sftp", function(){
46   -
47   - var supplied_config = {"publish": { }};
48   - var error = "Cannot find sftp settings in config";
49   -
50   - expect(function(){ sftp_publisher.retrieveOptions(supplied_config) }).toThrow(error);
51   -
52   - });
53   -
54   - it("throws an error if config doesn't define a publish section", function(){
55   -
56   - var supplied_config = {};
57   - var error = "Cannot find sftp settings in config";
58   -
59   - expect(function(){ sftp_publisher.retrieveOptions(supplied_config) }).toThrow(error);
60   -
61   - });
62   -
63   -});
64   -
65   -describe("check if a file is modified", function() {
66   -
67   - it("return true if file modified date is newer than last published date", function() {
68   - sftp_publisher.lastPublishedDate = new Date(2012, 6, 25);
69   -
70   - expect(sftp_publisher.isModified(new Date(2012, 6, 30))).toEqual(true);
71   - });
72   -
73   -});
74   -
75   -describe("check and create a remote directory", function(){
76   -
77   - it("takes the stat for the remote directory", function(){
78   - var spy_stat = jasmine.createSpy();
79   - var spy_callback = jasmine.createSpy();
80   - sftp_publisher.client = {stat: spy_stat};
81   -
82   - sftp_publisher.checkAndCreateRemoteDirectory("public_html/site", spy_callback);
83   - expect(spy_stat).toHaveBeenCalled();
84   - });
85   -
86   - it("executes the callback if remote directory already exists", function(){
87   - var spy_stat = jasmine.createSpy();
88   - spy_stat.andCallFake(function(path, cbk){
89   - cbk(null, {isDirectory: function(){ return true} });
90   - });
91   - var spy_callback = jasmine.createSpy();
92   - sftp_publisher.client = {stat: spy_stat};
93   -
94   - sftp_publisher.checkAndCreateRemoteDirectory("public_html/site", spy_callback);
95   - expect(spy_callback).toHaveBeenCalled();
96   - });
97   -
98   - it("creates a remote directory if it doesn't exists", function(){
99   - var spy_stat = jasmine.createSpy();
100   - spy_stat.andCallFake(function(path, cbk){
101   - cbk("error", null);
102   - });
103   - var spy_mkdir = jasmine.createSpy();
104   - var spy_callback = jasmine.createSpy();
105   - sftp_publisher.client = {stat: spy_stat, mkdir: spy_mkdir};
106   -
107   - sftp_publisher.checkAndCreateRemoteDirectory("public_html/site", spy_callback);
108   - expect(spy_mkdir.mostRecentCall.args[0]).toEqual("public_html/site");
109   - });
110   -
111   - it("executes the callback after creating the remote directory", function(){
112   - var spy_stat = jasmine.createSpy();
113   - spy_stat.andCallFake(function(path, cbk){
114   - cbk("error", null);
115   - });
116   - var spy_mkdir = jasmine.createSpy();
117   - spy_mkdir.andCallFake(function(path, mode, cbk){
118   - cbk();
119   - });
120   - var spy_callback = jasmine.createSpy();
121   - sftp_publisher.client = {stat: spy_stat, mkdir: spy_mkdir};
122   -
123   - sftp_publisher.checkAndCreateRemoteDirectory("public_html/site", spy_callback);
124   - expect(spy_callback).toHaveBeenCalled();
125   - });
126   -
127   - it("throws an exception if there's an error in creating the remote directory", function(){
128   - var spy_stat = jasmine.createSpy();
129   - spy_stat.andCallFake(function(path, cbk){
130   - cbk("error", null);
131   - });
132   - var spy_mkdir = jasmine.createSpy();
133   - spy_mkdir.andCallFake(function(path, mode, cbk){
134   - cbk("error");
135   - });
136   - var spy_callback = jasmine.createSpy();
137   - sftp_publisher.client = {stat: spy_stat, mkdir: spy_mkdir};
138   -
139   - expect(function(){sftp_publisher.checkAndCreateRemoteDirectory("public_html/site", spy_callback)}).toThrow();
140   - });
141   -
142   -});
143   -
144   -describe("upload file", function() {
145   -
146   - it("reads the file in given path", function(){
147   - spyOn(fs, "readFile");
148   - var spy_callback = jasmine.createSpy();
149   -
150   - sftp_publisher.uploadFile("output/file", "public_html/site", spy_callback);
151   - expect(fs.readFile.mostRecentCall.args[0]).toEqual("output/file");
152   - });
153   -
154   - it("writes the file to the given remote path", function(){
155   - spyOn(fs, "readFile").andCallFake(function(path, callback){
156   - callback(null, "content");
157   - });
158   - var spy_writefile = jasmine.createSpy();
159   - var spy_callback = jasmine.createSpy();
160   - sftp_publisher.client = {writeFile: spy_writefile};
161   -
162   - sftp_publisher.uploadFile("output/file", "public_html/site", spy_callback);
163   - expect(spy_writefile.mostRecentCall.args[0]).toEqual("public_html/site");
164   - });
165   -
166   - it("executes the callback after writing the file", function(){
167   - spyOn(fs, "readFile").andCallFake(function(path, callback){
168   - callback(null, "content");
169   - });
170   - var spy_writefile = jasmine.createSpy();
171   - spy_writefile.andCallFake(function(path, buffer, callback){
172   - callback(null);
173   - });
174   - var spy_callback = jasmine.createSpy();
175   - sftp_publisher.client = {writeFile: spy_writefile};
176   -
177   - sftp_publisher.uploadFile("output/file", "public_html/site", spy_callback);
178   - expect(spy_callback).toHaveBeenCalled();
179   - });
180   -
181   - it("throws an exception if there's an error in writing the file", function(){
182   - spyOn(fs, "readFile").andCallFake(function(path, callback){
183   - callback(null, "content");
184   - });
185   - var spy_writefile = jasmine.createSpy();
186   - spy_writefile.andCallFake(function(path, buffer, callback){
187   - callback("error");
188   - });
189   - var spy_callback = jasmine.createSpy();
190   - sftp_publisher.client = {writeFile: spy_writefile};
191   -
192   - expect(function(){ sftp_publisher.uploadFile("output/file", "public_html/site", spy_callback) }).toThrow();
193   - });
194   -
195   -});

0 comments on commit 7cd7ca1

Please sign in to comment.
Something went wrong with that request. Please try again.