Created
January 21, 2012 12:38
-
-
Save samcday/1652663 to your computer and use it in GitHub Desktop.
Easy-mode dev work to remote server!
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
require "sugar" | |
async = require "async" | |
fs = require "fs" | |
path = require "path" | |
child_process = require "child_process" | |
FTPClient = require "./ftp" | |
[watchPath, ftpHost, ftpUser, ftpPass, ftpPath] = process.argv.from 2 | |
conn = new FTPClient | |
host: ftpHost | |
#debug: (msg) -> | |
# console.log msg | |
conn.on "connect", () -> | |
console.log "Connected to #{ftpHost}. Authenticating." | |
conn.auth ftpUser, ftpPass, () -> | |
console.log "Authenticated." | |
conn.on "error", () -> | |
console.log "FTP error", arguments | |
conn.on "timeout", () -> | |
console.log arguments | |
conn.on "close", () -> | |
console.log "Closed." | |
conn.on "end", () -> | |
console.log "FTP connection closed." | |
console.log "Connecting to #{ftpHost}" | |
conn.connect() | |
inotifyWatch = child_process.spawn "inotifywait", ["-m", "-r", "-q", watchPath] | |
queue = async.queue (task, callback) -> | |
pathOnServer = path.join ftpPath, path.relative watchPath, task.file | |
if task.type is "dir" | |
switch task.action | |
when "create" | |
console.log "Creating directory #{pathOnServer}" | |
conn.mkdir pathOnServer, () -> | |
console.log "Created directory #{pathOnServer}" | |
callback() | |
when "delete" | |
console.log "Deleting directory #{pathOnServer}" | |
conn.rmdir pathOnServer, () -> | |
console.log "Deleted directory #{pathOnServer}" | |
callback() | |
else callback() | |
else if task.type is "file" | |
switch task.action | |
when "modify" | |
console.log "Putting #{task.file} on #{pathOnServer}" | |
conn.put fs.createReadStream(task.file), pathOnServer, (err) -> | |
console.log "Done." | |
callback() | |
when "delete" | |
console.log "Deleting #{pathOnServer} on server." | |
conn.delete pathOnServer, () -> | |
console.log "Done." | |
callback() | |
else callback() | |
#callback() | |
, 1 | |
inotifyWatch.stdout.setEncoding "utf8" | |
inotifyWatch.stdout.on "data", (data) -> | |
for line in data.split("\n") | |
line = line.trim() | |
continue if not line.length | |
[pathRoot, flags, file] = line.split " " | |
flags = flags.split "," | |
isDir = !!flags.find("ISDIR") | |
action = | |
if !!flags.find("CREATE") then "create" | |
else if !!flags.find("CLOSE_WRITE") and not isDir then "modify" | |
else if !!flags.find("DELETE") then "delete" | |
else null | |
continue if not action | |
task = | |
type: if isDir then "dir" else "file" | |
file: path.join pathRoot, file | |
action: action | |
queue.push task | |
inotifyWatch.stderr.setEncoding "utf8" | |
inotifyWatch.stderr.on "data", (data) -> | |
console.log data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment