-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsftploy.js
More file actions
117 lines (105 loc) · 3.11 KB
/
sftploy.js
File metadata and controls
117 lines (105 loc) · 3.11 KB
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
var fs = require('fs');
var path = require('path');
var glob = require('glob');
var scp2 = require('scp2');
var merge = require('lodash.merge');
var isArray = require('lodash.isarray');
var Promise = require('bluebird');
module.exports = function (opts) {
var options = merge({
port: 22,
files: '**/*',
localRoot: process.cwd(),
remoteRoot: '/',
}, opts);
var client;
function startConnection () {
return new Promise(function (resolve, reject) {
client = new scp2.Client({
host: options.host,
port: options.port,
username: options.username,
password: options.password,
privateKey: options.privateKey ? fs.readFileSync(options.privateKey) : undefined
});
resolve(client);
});
}
function getFiles () {
return new Promise(function (resolve, reject) {
if (isArray(options.files)) {
resolve(options.files);
} else {
glob(options.files, {
cwd: options.localRoot,
ignore: options.exclude,
dot: true
}, function (err, files) {
if (err) {
reject(err);
} else {
resolve(files);
}
});
}
});
}
function getDirectoriesForCreation () {
return getFiles().then(function (files) {
return new Promise(function (resolve) {
resolve(files.filter(function (file) {
var localPath = path.resolve(process.cwd(), options.localRoot, file);
var stats = fs.statSync(localPath);
return stats && stats.isDirectory();
}));
});
});
}
function createRemoteDirectories (directories) {
return Promise.each(directories, function (directory) {
return new Promise(function (resolve, reject) {
var remotePath = path.resolve(options.remoteRoot, directory);
client.mkdir(remotePath, function (err) {
if(err) {
reject(err);
} else {
resolve();
}
});
});
});
}
function getFilesForUpload () {
return getFiles().then(function (files) {
return new Promise(function (resolve) {
resolve(files.filter(function (file) {
var localPath = path.resolve(process.cwd(), options.localRoot, file);
var stats = fs.statSync(localPath);
return stats && !stats.isDirectory();
}));
});
});
}
function uploadFiles (files) {
return Promise.each(files, function (file) {
return new Promise(function (resolve, reject) {
var localPath = path.resolve(process.cwd(), options.localRoot, file);
var remotePath = path.resolve(options.remoteRoot, file);
client.upload(localPath, remotePath, function (err) {
if (err) {
reject(err);
} else {
resolve(remotePath);
}
});
});
});
}
function quitConnection () {
return new Promise(function (resolve) {
client.close();
resolve();
});
}
return startConnection().then(getDirectoriesForCreation).then(createRemoteDirectories).then(getFilesForUpload).then(uploadFiles).then(quitConnection);
};