Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patch-6 #10

Merged
merged 3 commits into from
Sep 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sshync",
"version": "0.1.4",
"version": "0.1.5",
"description": "auto-sync files over ssh",
"main": "sshync.js",
"bin": {
Expand Down
4 changes: 4 additions & 0 deletions ssh.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@
this.ssh.kill('SIGHUP');
};

SSHClient.prototype.cd = function(dir, cb) {
this.exec("cd " + dir, cb);
};

SSHClient.prototype.mkdir = function(dir, cb) {
this.exec("mkdir -p " + dir, cb);
};
Expand Down
95 changes: 48 additions & 47 deletions sshync.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@
ignoreList = fs
.readFileSync(ignore, 'utf8')
.split('\n')
.filter(function(str) { return str !== ''; })
.map(function(str) { return destination + '/' + str; });
.filter(function(str) {
return str !== '';
})
.map(function(str) {
if (fs.lstatSync(str).isDirectory())
return str + '/';
return str;
});
}

function walk(dir, done) {
Expand Down Expand Up @@ -68,53 +74,48 @@
});
}

function put(event, file, remoteFile) {
client.putFile(file, remoteFile, function(error, stdout, stderr) {
if (error) {
console.log('[!]'.red + ' failed to put ' + file);
console.log(stderr.red);
return;
}

console.log(
(event === 'add' ? '[+]'.green : '[/]'.yellow),
file.blue,
'=>',
remoteFile.green,
'[' + fs.statSync(file).size + ' bytes]'
);
});
}

function watch(client) {
return function(event, file) {
file = file !== undefined ? file : '';

var src = source.indexOf(file) === -1 ? source + '/' + file : source,
dest = source.indexOf(file) === -1 ? destination + '/' + file : destination,
dir = dest.split('/');

if(dir.length > 1)
dir = dir.slice(0, -1);

dir = dir.join('/');

for (var i = 0; i < ignoreList.length; i++)
if (ignoreList[i] === dest.substring(0, ignoreList[i].length))
return;

client.mkdir(dir, function(error, stdout, stderr) {
if (error)
throw error;

if (dirList.indexOf(dir) === -1) {
console.log('mkdir -p', dir.green);
dirList.push(dir);
}

if (fs.lstatSync(src).isDirectory()) {
walk(src, walker);
return;
}

client.putFile(src, dest, function(error, stdout, stderr) {
if (error) {
console.log('[!]'.red + ' failed to put ' + src);
console.log(stderr.red);
return;
}

console.log(
(event === 'add' ? '[+]'.green : '[/]'.yellow),
src.blue,
'=>',
dest.green,
'[' + fs.statSync(src).size + ' bytes]'
);
});
});
var remoteFile = destination + '/' + file,
remotedir = remoteFile.split('/');

if(remotedir.length > 1)
remotedir = remotedir.slice(0, -1);

for (var i = 0; i < ignoreList.length; i++)
if (ignoreList[i] === file.substring(0, ignoreList[i].length))
return;

remotedir = remotedir.join('/');
client.cd(remotedir, function(error) {
if (error) {
client.mkdir(remotedir, function(error) {
put(file, remoteFile);
});
return;
}

put(event, file, remoteFile);
});
};
}

Expand All @@ -137,7 +138,7 @@
if (fs.lstatSync(source).isDirectory())
walk(source, walker);
else
watch(client)('add');
watch(client)('add', source);

fs.watch(source, watch(client));
});
Expand Down