-
Notifications
You must be signed in to change notification settings - Fork 34
/
sshync.js
executable file
·88 lines (77 loc) · 1.96 KB
/
sshync.js
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
#!/usr/bin/env node
(function () {
'use strict';
var fs = require('fs'),
rsync = require('rsync'),
path = require('path'),
chalk = require('chalk'),
args = process.argv.slice(2),
edit = false;
if (args.length !== 2)
return console.log(
'sshync <' + chalk.blue('source') + '> ' +
'<user@ip[:port]:' + chalk.green('destination') + '>\n' +
'\t' + chalk.blue('source') + ':\t\tlocal source file/folder\n' +
'\t' + chalk.green('destination') + ':\tremote destination file/folder'
);
var source = path.resolve(args[0]),
exclude = path.resolve(source, '.sshyncignore'),
cmd = new rsync()
.shell('ssh')
.flags('avuz')
.delete() // This tells rsync to delete extraneous files from the receiving side
.source(source)
.destination(args[1]),
handle;
if (fs.existsSync(exclude))
cmd.set('exclude-from', exclude)
// abort rsync on process exit
function quit() {
if (handle)
handle.kill();
process.exit();
}
process
.on('SIGINT', quit)
.on('SIGTERM', quit)
.on('exit', quit);
function contains(str, substr) {
return str.indexOf(substr) !== -1;
}
function print(line) {
console.log(
contains(line, 'sent') &&
contains(line, 'received') &&
contains(line, 'bytes/sec') ?
chalk.blue(line) :
(edit ? chalk.yellow('✎ ') : chalk.green('✓ ')) + line
);
}
function sync() {
handle = cmd.execute(
function (error, code, cmd) {
return error ? console.log(chalk.red(error)) : 0;
},
function (data) {
return data
.toString()
.split('\n')
.filter(
function (line) {
return line && contains(line, '/');
}
)
.forEach(print);
}
);
}
sync();
fs.watch(
source,
{ recursive: true },
function () {
edit = true;
sync();
}
);
}());