forked from mattijs/node-rsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsync.js
1085 lines (959 loc) · 26.8 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var spawn = require('child_process').spawn;
var path = require('path');
/**
* Rsync is a wrapper class to configure and execute an `rsync` command
* in a fluent and convenient way.
*
* A new command can be set up by creating a new `Rsync` instance of
* obtaining one through the `build` method.
*
* @example
* // using the constructor
* var rsync = new Rsync()
* .source('/path/to/source')
* .destination('myserver:destination/');
*
* // using the build method with options
* var rsync = Rsync.build({
* source: '/path/to/source',
* destination: 'myserver:destination/'
* });
*
* Executing the command can be done using the `execute` method. The command
* is executed as a child process and three callbacks can be registered. See
* the `execute` method for more details.
*
* @example
* rsync.execute(function(error, code, cmd) {
* // function called when the child process is finished
* }, function(stdoutChunk) {
* // function called when a chunk of text is received on stdout
* }, function stderrChunk) {
* // function called when a chunk of text is received on stderr
* });
*
* @author Mattijs Hoitink <[email protected]>
* @copyright Copyright (c) 2013, Mattijs Hoitink <[email protected]>
* @license The MIT License
*
* @constructor
* @param {Object} config Configuration settings for the Rsync wrapper.
*/
function Rsync(config) {
if (!(this instanceof Rsync)) {
return new Rsync(config);
}
// Parse config
config = config || {};
if (typeof(config) !== 'object') {
throw new Error('Rsync config must be an Object');
}
// executable
this._executable = hasOP(config, 'executable') ? config.executable : 'rsync';
// shell
this._executableShell = hasOP(config, 'executableShell') ? config.executableShell : '/bin/sh';
// source(s) and destination
this._sources = [];
this._destination = '';
// ordered list of file patterns to include/exclude
this._patterns = [];
// options
this._options = {};
// output callbacks
this._outputHandlers = {
stdout: null,
stderr: null
};
this._cwd = process.cwd();
// Allow child_process.spawn env overriding
this._env = process.env;
// Debug parameter
this._debug = hasOP(config, 'debug') ? config.debug : false;
}
/**
* Build a new Rsync command from an options Object.
* @param {Object} options
* @return {Rsync}
*/
Rsync.build = function(options) {
var command = new Rsync();
// Process all options
for (var key in options) {
if (hasOP(options, key)) {
var value = options[key];
// Only allow calling methods on the Rsync command
if (typeof(command[key]) === 'function') {
command[key](value);
}
}
}
return command;
};
/**
* Set an option.
* @param {String} option
* @param mixed value
* @return Rsync
*/
Rsync.prototype.set = function(option, value) {
option = stripLeadingDashes(option);
if (option && option.length > 0) {
this._options[option] = value || null;
}
return this;
};
/**
* Unset an option.
* @param {String} option
* @return Rsync
*/
Rsync.prototype.unset = function(option) {
option = stripLeadingDashes(option);
if (option && Object.keys(this._options).indexOf(option) >= 0) {
delete this._options[option];
}
return this;
};
/**
* Set or unset one or more flags. A flag is a single letter option without a value.
*
* Flags can be presented as a single String, an Array containing Strings or an Object
* with the flags as keys.
*
* When flags are presented as a String or Array the set or unset method will be determined
* by the second parameter.
* When the flags are presented as an Object the set or unset method will be determined by
* the value corresponding to each flag key.
*
* @param {String|Array|Object} flags
* @param {Boolean} set
* @return Rsync
*/
Rsync.prototype.flags = function(flags, set) {
// Do some argument handling
if (!arguments.length) {
return this;
}
else if (arguments.length === 1) {
set = true;
}
else {
// There are more than 1 arguments, assume flags are presented as strings
flags = Array.prototype.slice.call(arguments);
// Check if the last argument is a boolean
if (typeof(flags[flags.length - 1]) === 'boolean') {
set = flags.pop();
}
else {
set = true;
}
// Join the remainder of the arguments to treat them as flags
flags = flags.join('');
}
// Split multiple flags
if (typeof(flags) === 'string') {
flags = stripLeadingDashes(flags).split('');
}
// Turn array into an object
if (isArray(flags)) {
var obj = {};
flags.forEach(function(f) {
obj[f] = set;
});
flags = obj;
}
// set/unset each flag
for (var key in flags) {
if (hasOP(flags, key)) {
var method = (flags[key]) ? 'set' : 'unset';
this[method](stripLeadingDashes(key));
}
}
return this;
};
/**
* Check if an option is set.
* @param {String} option
* @return {Boolean}
*/
Rsync.prototype.isSet = function(option) {
option = stripLeadingDashes(option);
return Object.keys(this._options).indexOf(option) >= 0;
};
/**
* Get an option by name.
* @param {String} name
* @return mixed
*/
Rsync.prototype.option = function(name) {
name = stripLeadingDashes(name);
return this._options[name];
};
/**
* Register a list of file patterns to include/exclude in the transfer. Patterns can be
* registered as an array of Strings or Objects.
*
* When registering a pattern as a String it must be prefixed with a `+` or `-` sign to
* signal include or exclude for the pattern. The sign will be stripped of and the
* pattern will be added to the ordered pattern list.
*
* When registering the pattern as an Object it must contain the `action` and
* `pattern` keys where `action` contains the `+` or `-` sign and the `pattern`
* key contains the file pattern, without the `+` or `-` sign.
*
* @example
* // on an existing rsync object
* rsync.patterns(['-docs', { action: '+', pattern: '/subdir/*.py' }]);
*
* // using Rsync.build for a new rsync object
* rsync = Rsync.build({
* ...
* patterns: [ '-docs', { action: '+', pattern: '/subdir/*.py' }]
* ...
* })
*
* @param {Array} patterns
* @return Rsync
*/
Rsync.prototype.patterns = function(patterns) {
if (arguments.length > 1) {
patterns = Array.prototype.slice.call(arguments, 0);
}
if (!isArray(patterns)) {
patterns = [ patterns ];
}
patterns.forEach(function(pattern) {
var action = '?';
if (typeof(pattern) === 'string') {
action = pattern.charAt(0);
pattern = pattern.substring(1);
}
else if (
typeof(pattern) === 'object' &&
hasOP(pattern, 'action') &&
hasOP(pattern, 'pattern')
) {
action = pattern.action;
pattern = pattern.pattern;
}
// Check if the pattern is an include or exclude
if (action === '-') {
this.exclude(pattern);
}
else if (action === '+') {
this.include(pattern);
}
else {
throw new Error('Invalid pattern: ' + pattern);
}
}, this);
return this;
};
/**
* Exclude a file pattern from transfer. The pattern will be appended to the ordered list
* of patterns for the rsync command.
*
* @param {String|Array} patterns
* @return Rsync
*/
Rsync.prototype.exclude = function(patterns) {
if (arguments.length > 1) {
patterns = Array.prototype.slice.call(arguments, 0);
}
if (!isArray(patterns)) {
patterns = [ patterns ];
}
patterns.forEach(function(pattern) {
this._patterns.push({ action: '-', pattern: pattern });
}, this);
return this;
};
/**
* Include a file pattern for transfer. The pattern will be appended to the ordered list
* of patterns for the rsync command.
*
* @param {String|Array} patterns
* @return Rsync
*/
Rsync.prototype.include = function(patterns) {
if (arguments.length > 1) {
patterns = Array.prototype.slice.call(arguments, 0);
}
if (!isArray(patterns)) {
patterns = [ patterns ];
}
patterns.forEach(function(pattern) {
this._patterns.push({ action: '+', pattern: pattern });
}, this);
return this;
};
/**
* Get the command that is going to be executed.
* @return {String}
*/
Rsync.prototype.command = function() {
return this.executable() + ' ' + this.args().join(' ');
};
/**
* String representation of the Rsync command. This is the command that is
* going to be executed when calling Rsync::execute.
* @return {String}
*/
Rsync.prototype.toString = Rsync.prototype.command;
/**
* Get the arguments for the rsync command.
* @return {Array}
*/
Rsync.prototype.args = function() {
// Gathered arguments
var args = [];
// Add options. Short options (one letter) without values are gathered together.
// Long options have a value but can also be a single letter.
var short = [];
var long = [];
// Split long and short options
for (var key in this._options) {
if (hasOP(this._options, key)) {
var value = this._options[key];
var noval = (value === null || value === undefined);
// Check for short option (single letter without value)
if (key.length === 1 && noval) {
short.push(key);
}
else {
if (isArray(value)) {
value.forEach(function (val) {
long.push(buildOption(key, val, escapeShellArg));
});
}
else {
long.push(buildOption(key, value, escapeShellArg));
}
}
}
}
// Add combined short options if any are present
if (short.length > 0) {
args.push('-' + short.join(''));
}
// Add long options if any are present
if (long.length > 0) {
args = args.concat(long);
}
// Add includes/excludes in order
this._patterns.forEach(function(def) {
if (def.action === '-') {
args.push(buildOption('exclude', def.pattern, escapeFileArg));
}
else if (def.action === '+') {
args.push(buildOption('include', def.pattern, escapeFileArg));
}
else {
debug(this, 'Unknown pattern action ' + def.action);
}
});
// Add sources
if (this.source().length > 0) {
args = args.concat(this.source().map(escapeFileArg));
}
// Add destination
if (this.destination()) {
args.push(escapeFileArg(this.destination()));
}
return args;
};
/**
* Get and set rsync process cwd directory.
*
* @param {string} cwd= Directory path relative to current process directory.
* @return {string} Return current _cwd.
*/
Rsync.prototype.cwd = function(cwd) {
if (arguments.length > 0) {
if (typeof cwd !== 'string') {
throw new Error('Directory should be a string');
}
this._cwd = path.resolve(cwd);
}
return this._cwd;
};
/**
* Get and set rsync process environment variables
*
* @param {string} env= Environment variables
* @return {string} Return current _env.
*/
Rsync.prototype.env = function(env) {
if (arguments.length > 0) {
if (typeof env !== 'object') {
throw new Error('Environment should be an object');
}
this._env = env;
}
return this._env;
};
/**
* Register an output handlers for the commands stdout and stderr streams.
* These functions will be called once data is streamed on one of the output buffers
* when the command is executed using `execute`.
*
* Only one callback function can be registered for each output stream. Previously
* registered callbacks will be overridden.
*
* @param {Function} stdout Callback Function for stdout data
* @param {Function} stderr Callback Function for stderr data
* @return Rsync
*/
Rsync.prototype.output = function(stdout, stderr) {
// Check for single argument so the method can be used with Rsync.build
if (arguments.length === 1 && Array.isArray(stdout)) {
stderr = stdout[1];
stdout = stdout[0];
}
if (typeof(stdout) === 'function') {
this._outputHandlers.stdout = stdout;
}
if (typeof(stderr) === 'function') {
this._outputHandlers.stderr = stdout;
}
return this;
};
/**
* Execute the rsync command.
*
* The callback function is called with an Error object (or null when there was none),
* the exit code from the executed command and the executed command as a String.
*
* When stdoutHandler and stderrHandler functions are provided they will be used to stream
* data from stdout and stderr directly without buffering.
*
* @param {Function} callback Called when rsync finishes (optional)
* @param {Function} stdoutHandler Called on each chunk received from stdout (optional)
* @param {Function} stderrHandler Called on each chunk received from stderr (optional)
*/
Rsync.prototype.execute = function(callback, stdoutHandler, stderrHandler) {
// Register output handlers
this.output(stdoutHandler, stderrHandler);
// Execute the command as a child process
// see https://github.com/joyent/node/blob/937e2e351b2450cf1e9c4d8b3e1a4e2a2def58bb/lib/child_process.js#L589
var cmdProc;
if ('win32' === process.platform) {
cmdProc = spawn('cmd.exe', ['/s', '/c', '"' + this.command() + '"'],
{ stdio: 'pipe', windowsVerbatimArguments: true, cwd: this._cwd, env: this._env });
}
else {
cmdProc = spawn(this._executableShell, ['-c', this.command()],
{ stdio: 'pipe', cwd: this._cwd, env: this._env });
}
// Capture stdout and stderr if there are output handlers configured
if (typeof(this._outputHandlers.stdout) === 'function') {
cmdProc.stdout.on('data', this._outputHandlers.stdout);
}
if (typeof(this._outputHandlers.stderr) === 'function') {
cmdProc.stderr.on('data', this._outputHandlers.stderr);
}
// Wait for the command to finish
cmdProc.on('close', function(code) {
var error = null;
// Check rsyncs error code
// @see http://bluebones.net/2007/06/rsync-exit-codes/
if (code !== 0) {
error = new Error('rsync exited with code ' + code);
}
// Check for callback
if (typeof(callback) === 'function') {
callback(error, code, this.command());
}
}.bind(this));
// Return the child process object so it can be cleaned up
// if the process exits
return(cmdProc);
};
/**
* Get or set the debug property.
*
* The property is set to the boolean provided so unsetting the debug
* property has to be done by passing false to this method.
*
* @function
* @name debug
* @memberOf Rsync.prototype
* @param {Boolean} debug the value of the debug property (optional)
* @return {Rsync|Boolean}
*/
createValueAccessor('debug');
/**
* Get or set the executable to use for the rsync process.
*
* When setting the executable path the Rsync instance is returned for
* the fluent interface. Otherwise the configured executable path
* is returned.
*
* @function
* @name executable
* @memberOf Rsync.prototype
* @param {String} executable path to the executable (optional)
* @return {Rsync|String}
*/
createValueAccessor('executable');
/**
* Get or set the shell to use on non-Windows (Unix or Mac OS X) systems.
*
* When setting the shell the Rsync instance is returned for the
* fluent interface. Otherwise the configured shell is returned.
*
* @function
* @name executableShell
* @memberOf Rsync.prototype
* @param {String} shell to use on non-Windows systems (optional)
* @return {Rsync|String}
*/
createValueAccessor('executableShell');
/**
* Get or set the destination for the transfer.
*
* When setting the destination the Rsync instance is returned for
* the fluent interface. Otherwise the configured destination path
* is returned.
*
* @function
* @name destination
* @memberOf Rsync.prototype
* @param {String} destination the destination (optional)
* @return {Rsync|String}
*/
createValueAccessor('destination');
/**
* Add one or more sources for the command or get the list of configured
* sources.
*
* The sources are appended to the list of known sources if they were not
* included yet and the Rsync instance is returned for the fluent
* interface. Otherwise the configured list of source is returned.
*
* @function
* @name source
* @memberOf Rsync.prototype
* @param {String|Array} sources the source or list of sources to configure (optional)
* @return {Rsync|Array}
*/
createListAccessor('source', '_sources');
/**
* Set the shell to use when logging in on a remote server.
*
* This is the same as setting the `rsh` option.
*
* @function
* @name shell
* @memberOf Rsync.prototype
* @param {String} shell the shell option to use
* @return {Rsync}
*/
exposeLongOption('rsh', 'shell');
/**
* Add a chmod instruction to the command.
*
* @function
* @name chmod
* @memberOf Rsync.prototype
* @param {String|Array}
* @return {Rsync|Array}
*/
exposeMultiOption('chmod', 'chmod');
/**
* Set the delete flag.
*
* This is the same as setting the `--delete` commandline flag.
*
* @function
* @name delete
* @memberOf Rsync.prototype
* @return {Rsync}
*/
exposeShortOption('delete');
/**
* Set the progress flag.
*
* This is the same as setting the `--progress` commandline flag.
*
* @function
* @name progress
* @memberOf Rsync.prototype
* @return {Rsync}
*/
exposeShortOption('progress');
/**
* Set the archive flag.
*
* @function
* @name archive
* @memberOf Rsync.prototype
* @return {Rsync}
*/
exposeShortOption('a', 'archive');
/**
* Set the compress flag.
*
* @function
* @name compress
* @memberOf Rsync.prototype
* @return {Rsync}
*/
exposeShortOption('z', 'compress');
/**
* Set the recursive flag.
*
* @function
* @name recursive
* @memberOf Rsync.prototype
* @return {Rsync}
*/
exposeShortOption('r', 'recursive');
/**
* Set the update flag.
*
* @function
* @name update
* @memberOf Rsync.prototype
* @return {Rsync}
*/
exposeShortOption('u', 'update');
/**
* Set the quiet flag.
*
* @function
* @name quiet
* @memberOf Rsync.prototype
* @return {Rsync}
*/
exposeShortOption('q', 'quiet');
/**
* Set the dirs flag.
*
* @function
* @name dirs
* @memberOf Rsync.prototype
* @return {Rsync}
*/
exposeShortOption('d', 'dirs');
/**
* Set the links flag.
*
* @function
* @name links
* @memberOf Rsync.prototype
* @return {Rsync}
*/
exposeShortOption('l', 'links');
/**
* Set the dry flag.
*
* @function
* @name dry
* @memberOf Rsync.prototype
* @return {Rsync}
*/
exposeShortOption('n', 'dry');
/**
* Set the hard links flag preserving hard links for the files transmitted.
*
* @function
* @name hardLinks
* @memberOf Rsync.prototype
* @return {Rsync}
*/
exposeShortOption('H', 'hardLinks');
/**
* Set the perms flag.
*
* @function
* @name perms
* @memberOf Rsync.prototype
* @return {Rsync}
*/
exposeShortOption('p', 'perms');
/**
* Set the executability flag to preserve executability for the files
* transmitted.
*
* @function
* @name executability
* @memberOf Rsync.prototype
* @return {Rsync}
*/
exposeShortOption('E', 'executability');
/**
* Set the group flag to preserve the group permissions of the files
* transmitted.
*
* @function
* @name group
* @memberOf Rsync.prototype
* @return {Rsync}
*/
exposeShortOption('g', 'group');
/**
* Set the owner flag to preserve the owner of the files transmitted.
*
* @function
* @name owner
* @memberOf Rsync.prototype
* @return {Rsync}
*/
exposeShortOption('o', 'owner');
/**
* Set the acls flag to preserve the ACLs for the files transmitted.
*
* @function
* @name acls
* @memberOf Rsync.prototype
* @return {Rsync}
*/
exposeShortOption('A', 'acls');
/**
* Set the xattrs flag to preserve the extended attributes for the files
* transmitted.
*
* @function
* @name xattrs
* @memberOf Rsync.prototype
* @return {Rsync}
*/
exposeShortOption('X', 'xattrs');
/**
* Set the devices flag to preserve device files in the transfer.
*
* @function
* @name devices
* @memberOf Rsync.prototype
* @return {Rsync}
*/
exposeShortOption('devices');
/**
* Set the specials flag to preserve special files.
*
* @function
* @name specials
* @memberOf Rsync.prototype
* @return {Rsync}
*/
exposeShortOption('specials');
/**
* Set the times flag to preserve times for the files in the transfer.
*
* @function
* @name times
* @memberOf Rsync.prototype
* @return {Rsync}
*/
exposeShortOption('t', 'times');
// our awesome export product
module.exports = Rsync;
/* **** */
/**
* Create a chainable function on the Rsync prototype for getting and setting an
* internal value.
* @param {String} name
* @param {String} internal
*/
function createValueAccessor(name, internal) {
var container = internal || '_' + name;
Rsync.prototype[name] = function(value) {
if (!arguments.length) return this[container];
this[container] = value;
return this;
};
}
/**
* @param {String} name
* @param {String} internal
*/
function createListAccessor(name, internal) {
var container = internal || '_' + name;
Rsync.prototype[name] = function(value) {
if (!arguments.length) return this[container];
if (isArray(value)) {
value.forEach(this[name], this);
}
else if (typeof(value) !== 'string') {
throw new Error('Value for Rsync::' + name + ' must be a String');
}
else if (this[container].indexOf(value) < 0) {
this[container].push(value);
}
return this;
};
}
/**
* Create a shorthand method on the Rsync prototype for setting and unsetting a simple option.
* @param {String} option
* @param {String} name
*/
function exposeShortOption(option, name) {
name = name || option;
Rsync.prototype[name] = function(set) {
// When no arguments are passed in assume the option
// needs to be set
if (!arguments.length) set = true;
var method = (set) ? 'set' : 'unset';
return this[method](option);
};
}
/**
* Create a function for an option that can be set multiple time. The option
* will accumulate all values.
*
* @param {String} option
* @param {[String]} name
*/
function exposeMultiOption(option, name) {
name = name || option;
Rsync.prototype[name] = function(value) {
// When not arguments are passed in assume the options
// current value is requested
if (!arguments.length) return this.option(option);
if (!value) {
// Unset the option on falsy
this.unset(option);
}
else if (isArray(value)) {
// Call this method for each array value
value.forEach(this[name], this);
}
else {
// Add the value
var current = this.option(option);
if (!current) {
value = [ value ];
}
else if (!isArray(current)) {
value = [ current, value ];
}
else {
value = current.concat(value);
}
this.set(option, value);
}
return this;
};
}
/**
* Expose an rsync long option on the Rsync prototype.
* @param {String} option The option to expose
* @param {String} name An optional alternative name for the option.
*/
function exposeLongOption(option, name) {
name = name || option;
Rsync.prototype[name] = function(value) {
// When not arguments are passed in assume the options
// current value is requested
if (!arguments.length) return this.option(option);
var method = (value) ? 'set' : 'unset';
return this[method](option, value);
};
}
/**
* Build an option for use in a shell command.
*
* @param {String} name
* @param {String} value
* @param {Function|boolean} escapeArg
* @return {String}
*/
function buildOption(name, value, escapeArg) {
if (typeof escapeArg === 'boolean') {
escapeArg = (!escapeArg) ? noop : null;
}
if (typeof escapeArg !== 'function') {
escapeArg = escapeShellArg;
}
// Detect single option key
var single = (name.length === 1) ? true : false;
// Decide on prefix and value glue
var prefix = (single) ? '-' : '--';
var glue = (single) ? ' ' : '=';
// Build the option
var option = prefix + name;
if (arguments.length > 1 && value) {
value = escapeArg(String(value));
option += glue + value;
}
return option;
}