/
remoteCopy.js
65 lines (50 loc) · 1.82 KB
/
remoteCopy.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
/**
* @license mcdrop Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/jrburke/mcdrop for details
*/
/*jslint strict: false, regexp: false */
/*global define: false, console: false */
define(function (require) {
var path = require('path'),
fs = require('fs'),
spawn = require('child_process').spawn;
function remoteCopy(name, password, host, localDir, remoteDir, callback) {
//Build up args for the scp command. Grab all the top level files/directories
//in localDir and pass, since just passing localDir creates a directory
//named for the last part of localDir on the remote host, but instead,
//we just want the contents of the localDir copied up.
var args = ['-r'],
scp;
fs.readdirSync(localDir).forEach(function (name) {
args.push(path.join(localDir, name));
});
//Add the final args
args = args.concat([name + '@' + host + ':' + remoteDir]);
scp = spawn('scp', args);
scp.stdout.on('data', function (data) {
console.log('stdout: ' + data);
if (data.indexOf('assword:') !== -1) {
//Password is needed, pass it.
scp.stdin.write(password);
} else {
//Some sort of error
callback({
error: data
});
//Stop the command
scp.stdin.end();
}
});
scp.stderr.on('data', function (data) {
console.log('stderr: ' + data);
callback({
error: data
});
});
scp.on('exit', function (code) {
callback(undefined, code);
});
}
return remoteCopy;
});