This repository has been archived by the owner on Oct 8, 2021. It is now read-only.
/
rsync.js
122 lines (105 loc) · 2.38 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
/*
* Adapted from https://github.com/maxdaten/grunt-rsync
*
* Copyright (c) 2012 Jan-Philip Loos
* Licensed under the MIT license.
*/
module.exports = function ( grunt ) {
'use strict';
var _ = grunt.util._,
async = grunt.util.async,
path = require( "path" );
grunt.registerMultiTask( "rsync", "Copy files to a (remote) machine with rsync.", function () {
var done = this.async(),
options = _.clone( this.options({
user: process.env.USER,
host: "localhost",
cwd: ".",
remoteBase: "~",
verbose: false,
preserveTimes: false,
preservePermissions: true,
compression: true,
createDirs: true,
recursive: true,
additionalOptions: []
}) ),
dry = grunt.option( "no-write" ),
args = [],
url = ( options.user ? options.user + "@" : "" ) + options.host + ":" + options.remoteBase;
// these flags must be set before the src/dest args
if ( options.recursive ) {
args.push( "-r" );
}
if ( options.verbose ) {
args.push( "-v" );
}
if ( options.preserveTimes ) {
args.push( "-t" );
}
if ( options.preservePermissions ) {
args.push( "-p" );
}
if ( options.compression ) {
args.push( "-z" );
}
if ( options.createDirs ) {
args.push( "--dirs" );
}
if ( dry ) {
args.push( "--dry-run" );
}
if ( options.additionalOptions.length ) {
args = args.concat( options.additionalOptions );
}
var detectDestType = function(dest) {
if ( _.endsWith( dest, '/' ) ) {
return 'directory';
} else {
return 'file';
}
};
var unixifyPath = function( filepath ) {
if (process.platform === 'win32') {
return filepath.replace(/\\/g, '/');
} else {
return filepath;
}
};
if ( dry ) {
grunt.log.writeln( "Running in dry mode" );
}
async.forEach(
this.files,
function( filePair, nextFilePair ) {
var isExpandedPair = filePair.orig.expand || false;
var src = filePair.src[0],
dest;
async.forEach(
filePair.src,
function(src, next) {
dest = url + filePair.dest;
grunt.log.writeln( "Copying '" + src + "' to '" + dest + "'" );
grunt.util.spawn(
{
cmd: "rsync",
args: args.concat([
src,
dest
])
},
next
);
}, nextFilePair
);
},
function( err ) {
if ( err ) {
grunt.log.error( err );
done( false );
}
done( true );
}
);
});
};