forked from mateogianolio/sshync
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsshync.js
More file actions
executable file
·136 lines (112 loc) · 3.45 KB
/
sshync.js
File metadata and controls
executable file
·136 lines (112 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env node
(function() {
'use strict';
require('terminal-colors');
var fs = require('fs'),
path = require('path'),
ssh = require('./ssh.js'),
host = process.argv[2],
source = process.argv[3],
destination = process.argv[4],
dirList = [];
function help() {
var pkg = require('./package');
console.log('# sshync', pkg.version);
console.log('# by', pkg.author, '\n');
console.log('sshync <user@ip[:port]> <source> <destination>');
console.log('\tsource:\t\tlocal source folder.');
console.log('\tdestination:\tremote destination folder.');
process.exit();
}
if (!host || !source || !destination || host.indexOf('@') === -1)
help();
host = host.split('@');
var host_ip = host.pop(),
host_user = host.pop();
var ignore = source + '/.sshyncignore',
ignoreList = [];
if (fs.existsSync(ignore)) {
ignoreList = fs
.readFileSync(ignore, 'utf8')
.split('\n')
.filter(function(str) { return str !== ''; })
.map(function(str) { return destination + '/' + str; });
}
function walk(dir, done) {
var results = [];
fs.readdir(dir, function(err, list) {
if (err)
return done(err);
var pending = list.length;
if (!pending)
return done(null, results);
list.forEach(function(file) {
file = path.resolve(dir, file);
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
walk(file, function(err, res) {
results = results.concat(res);
if (!--pending)
done(null, results);
});
} else {
results.push(file);
if (!--pending)
done(null, results);
}
});
});
});
}
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);
}
client.putFile(src, dest, function(error, stdout, stderr) {
if (error)
throw error;
console.log(
(event === 'add' ? '[+]'.green : '[/]'.yellow),
src.blue,
'=>',
dest.green,
'[' + fs.statSync(src).size + ' bytes]'
);
});
});
};
}
function walker(error, results) {
if (error)
throw error;
results.forEach(function(result) {
var p = path.relative(source, result);
watch(client)('add', p);
});
}
var client = ssh(host_user, host_ip, function() {
console.log(source.blue, '=>', host_user.bold + '@'.blue + host_ip + ':' + destination.green, '\n');
if (ignoreList.length)
console.log('ignore', ignore.red);
if (fs.lstatSync(source).isDirectory())
walk(source, walker);
else
watch(client)('add');
fs.watch(source, watch(client));
});
}());