Skip to content

Commit

Permalink
Merge pull request #28 from Gawdl3y/master
Browse files Browse the repository at this point in the history
Add FTP transport
  • Loading branch information
yongkangchen committed Aug 20, 2014
2 parents 99e0ced + db61716 commit 6bbe6d7
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 6 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ Upload your files to remote host after every change.

Create file `.remote-sync.json` in your project root with these settings:

* `transport`Only `scp` supported right now.
* `transport``scp` for SCP/SFTP, or `ftp` for FTP
* `hostname` — Remote host address.
* `port` - Remort port to connect on.
* `username` — Remote host username.
* `password` — Remote host password.
* `keyfile` — Absolute path to SSH key.
* `keyfile` — Absolute path to SSH key. (only used for SCP)
* `passphrase` — Passphrase for the SSH key (only used for SCP)
* `target` — Target directory on remote host.
* `ignore` — Array of [minimatch](https://github.com/isaacs/minimatch) patterns
to ignore.
Expand Down Expand Up @@ -49,3 +50,18 @@ useAgent example:
]
}
```

FTP example:
```json
{
"transport": "ftp",
"hostname": "10.10.10.10",
"port": 21,
"username": "vagrant",
"password": "vagrant",
"target": "/home/vagrant/dirname/subdirname",
"ignore": [
".git/**"
]
}
```
8 changes: 6 additions & 2 deletions lib/RemoteSync.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,9 @@ deinit = ->

getTransport = ->
return transport if transport
ScpTransport = require "./transports/ScpTransport"
transport = new ScpTransport logger, settings
if settings.transport is 'scp' or settings.type is 'sftp'
ScpTransport = require "./transports/ScpTransport"
transport = new ScpTransport logger, settings
else if settings.transport is 'ftp'
FtpTransport = require "./transports/FtpTransport"
transport = new FtpTransport logger, settings
121 changes: 121 additions & 0 deletions lib/transports/FtpTransport.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
FTPConnection = null
mkdirp = null
fs = null
path = require "path"

module.exports =
class ScpTransport
constructor: (@logger, @settings) ->

dispose: ->
if @connection
@connection.raw.quit (err, data) =>
@logger.error err if err
@connection = null

upload: (localFilePath, callback) ->
targetFilePath = path.join(@settings.target,
path.relative(atom.project.getPath(), localFilePath))
.replace(/\\/g, "/")

errorHandler = (err) =>
@logger.error err
callback()

@_getConnection (err, c) =>
return errorHandler err if err

@logger.log "Uploading: #{localFilePath} to #{targetFilePath}"

c.mkdir path.dirname(targetFilePath), true, (err) =>
return errorHandler err if err

c.put localFilePath, targetFilePath, (err) =>
return errorHandler err if err

@logger.log "Uploaded: #{localFilePath} to #{targetFilePath}"

callback()

download: (targetFilePath, localFilePath, callback) ->
if not localFilePath
localFilePath = atom.project.getPath()

localFilePath = path.resolve(localFilePath,
path.relative(@settings.target, targetFilePath))

errorHandler = (err) =>
@logger.error err

@_getConnection (err, c) =>
return errorHandler err if err

@logger.log "Downloading: #{targetFilePath} to #{localFilePath}"

mkdirp = require "mkdirp" if not mkdirp
mkdirp path.dirname(localFilePath), (err) =>
return errorHandler err if err

c.get targetFilePath, (err, readableStream) =>
return errorHandler err if err

fs = require "fs-plus" if not fs
writableStream = fs.createWriteStream(localFilePath)
writableStream.on "unpipe", ->
callback new Error("Error saving file")
readableStream.pipe writableStream

@logger.log "Downloaded: #{targetFilePath} to #{localFilePath}"

callback?()

fetchFileTree: (localPath, callback) ->
targetPath = path.resolve(@settings.target,
path.relative(atom.project.getPath(), localPath))
{isIgnore} = @settings

@_getConnection (err, c) ->
return callback err if err

c.list targetPath, (err, list) ->
return callback err if err

files = []
for file, i in list
if file.type is '-' and file.name not isIgnore(file.name, targetPath)
files.push file.name

callback null, files

_getConnection: (callback) ->
{hostname, port, username, password, keyfile, useAgent, passphrase} = @settings

if @connection
return callback null, @connection

@logger.log "Connecting: #{username}@#{hostname}:#{port}"

FtpConnection = require "ftp" if not FtpConnection

connection = new FtpConnection
wasReady = false

connection.on "ready", ->
wasReady = true
callback null, connection

connection.on "error", (err) =>
unless wasReady
callback err
@connection = null

connection.on "end", =>
@connection = null

connection.connect
host: hostname
port: port
user: username
password: password

@connection = connection
3 changes: 2 additions & 1 deletion lib/transports/ScpTransport.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class ScpTransport

upload: (localFilePath, callback) ->
targetFilePath = path.join(@settings.target,
path.relative(atom.project.getPath(), localFilePath).replace("\\","/"))
path.relative(atom.project.getPath(), localFilePath))
.replace(/\\/g, "/")

errorHandler = (err) =>
@logger.error err
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"async": "^0.2",
"mkdirp": "^0.3",
"atom-message-panel": "^1.1.1",
"fs-plus": "^2.1"
"fs-plus": "^2.1",
"ftp": "^0.3.7"
}
}

0 comments on commit 6bbe6d7

Please sign in to comment.