-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathrsync.js
143 lines (120 loc) · 3.73 KB
/
rsync.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
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
137
138
139
140
141
142
143
'use strict';
var assert = require('better-assert');
var every = require('lodash.every');
var isString = require('lodash.isstring');
var spawn = require('child_process').spawn;
function rsync(config) {
if (!(this instanceof rsync)) {
return new rsync(config);
}
assert(typeof config === 'object');
assert(!config.options || typeof config.options === 'object');
this._options = config.options || {};
var sources = config.source;
if (!Array.isArray(sources)) {
sources = [sources];
}
assert(sources.length > 0 && every(sources, isString));
assert(sources.length === 1 || config.destination);
this._sources = sources;
assert(!config.destination || typeof config.destination === 'string');
this._destination = config.destination;
assert(!config.cwd || typeof config.cwd === 'string');
this._cwd = config.cwd;
assert(!config.stdoutHandler || typeof config.stdoutHandler === 'function');
this._stdout = config.stdoutHandler;
assert(!config.stderrHandler || typeof config.stderrHandler === 'function');
this._stderr = config.stderrHandler;
return this;
}
rsync.prototype = {
command: function() {
var args = [];
var shortOptions = [];
var longOptions = [];
for (var key in this._options) {
var value = this._options[key];
if (typeof value !== 'undefined' && value !== false) {
if (key.length === 1 && value === true) {
shortOptions.push(key);
} else {
var values = Array.isArray(value) ? value : [value];
for (var i = 0, l = values.length; i < l; i++) {
longOptions.push({key: key, value: values[i]});
}
}
}
}
if (shortOptions.length > 0) {
args.push('-' + shortOptions.join(''));
}
// "include" -argument must be applied before "exclude"
longOptions.sort(function (a, b) {
if (a.key === 'include') {
return -1;
}
if (b.key === 'include') {
return 1;
}
return 0;
})
if (longOptions.length > 0) {
args = args.concat(longOptions.map(function(option) {
var single = option.key.length === 1;
var output = (single ? '-' : '--') + option.key;
if (typeof option.value !== 'boolean') {
output += (single ? ' ' : '=') + escapeShellArg(option.value);
}
return output;
}));
}
args = args.concat(this._sources.map(escapeShellArg));
if (this._destination) {
args.push(escapeShellArg(this._destination));
}
return 'rsync ' + args.join(' ');
},
execute: function(callback) {
var command = this.command();
require('gulp-util').log('gulp-rsync:', 'Command: ' + command);
var childProcess;
if (process.platform === 'win32') {
childProcess = spawn('cmd.exe', ['/s', '/c', '"' + command + '"'], {
cwd: this._cwd,
stdio: [process.stdin, 'pipe', 'pipe'],
env: process.env,
windowsVerbatimArguments: true
});
} else {
childProcess = spawn('/bin/sh', ['-c', command], {
cwd: this._cwd,
stdio: 'pipe',
env: process.env
});
}
if (this._stdout) {
childProcess.stdout.on('data', this._stdout);
}
if (this._stderr) {
childProcess.stderr.on('data', this._stderr);
}
childProcess.on('close', function(code) {
var error = null;
if (code !== 0) {
error = new Error('rsync exited with code ' + code);
}
if (typeof callback === 'function') {
callback(error, command);
}
});
return childProcess;
}
};
function escapeShellArg(arg) {
if (!/(["'`\\\(\) ])/.test(arg)) {
return arg;
}
arg = arg.replace(/([\\])/g, '/');
return '"' + arg.replace(/(["'`\\])/g, '\\$1') + '"';
}
module.exports = rsync;