forked from simonh1000/ftp-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftp-deploy.js
239 lines (207 loc) · 6.2 KB
/
ftp-deploy.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
"use strict";
const fs = require('fs');
const path = require('path');
const util = require('util');
const events = require('events');
const Ftp = require('jsftp');
const async = require('async');
const minimatch = require('minimatch');
const read = require('read');
// A utility function to remove lodash/underscore dependency
// Checks an obj for a specified key
function has(obj, key) {
return Object.prototype.hasOwnProperty.call(obj, key);
}
const FtpDeployer = function () {
// The constructor for the super class.
events.EventEmitter.call(this);
const thisDeployer = this;
let transferredFileCount = 0;
let ftp;
let localRoot;
let remoteRoot;
const partialDirectories = []; // Holds list of directories to check & create (excluding local root path)
const partialFilePaths = []; // Holds list of partial file paths to upload
// var parallelUploads = 1; // NOTE: this can be added in when sftp is supported
let exclude = [];
let include = [];
let continueOnError = false;
function canIncludeFile(filePath) {
console.log(filePath);
let i;
if (include.length > 0) {
for (i = 0; i < include.length; i++) {
if (minimatch(filePath, include[i], {matchBase: true})) {
return true;
}
}
// Fallthrough to exclude list
}
if (exclude.length > 0) {
for (i = 0; i < exclude.length; i++) {
if (minimatch(filePath, exclude[i], {matchBase: true})) {
return false;
}
}
}
return true;
}
// A method for parsing the source location and storing the information into a suitably formated object
function dirParseSync(startDir, result) {
let i;
let tmpPath;
let currFile;
// Initialize the `result` object if it is the first iteration
if (result === undefined) {
result = {};
result[path.sep] = [];
}
// Check if `startDir` is a valid location
if (!fs.existsSync(startDir)) {
// console.error(startDir + 'is not an existing location');
throw new Error(startDir + 'is not an existing location');
}
// Iterate throught the contents of the `startDir` location of the current iteration
const files = fs.readdirSync(startDir);
for (i = 0; i < files.length; i++) {
currFile = path.join(startDir, files[i]);
if (fs.lstatSync(currFile).isDirectory()) {
tmpPath = path.relative(localRoot, currFile);
// Check exclude rules
if (canIncludeFile(tmpPath)) {
if (!has(result, tmpPath)) {
result[tmpPath] = [];
partialDirectories.push(tmpPath);
}
dirParseSync(currFile, result);
}
} else {
tmpPath = path.relative(localRoot, startDir);
if (tmpPath.length === 0) {
tmpPath = path.sep;
}
// Check exclude rules
const partialFilePath = path.join(tmpPath, files[i]);
if (canIncludeFile(partialFilePath)) {
result[tmpPath].push(files[i]);
partialFilePaths.push(partialFilePath);
}
}
}
return result;
}
// A method for uploading a single file
function ftpPut(partialFilePath, cb) {
let remoteFilePath = remoteRoot + '/' + partialFilePath;
remoteFilePath = remoteFilePath.replace(/\\/g, '/');
const fullLocalPath = path.join(localRoot, partialFilePath);
const emitData = {
totalFileCount: partialFilePaths.length,
transferredFileCount,
percentComplete: Math.round((transferredFileCount / partialFilePaths.length) * 100),
filename: partialFilePath
};
thisDeployer.emit('uploading', emitData);
ftp.put(fullLocalPath, remoteFilePath, err => {
if (err) {
emitData.err = err;
thisDeployer.emit('error', emitData); // Error event from 0.5.x TODO: either expand error events or remove this
thisDeployer.emit('upload-error', emitData);
if (continueOnError) {
cb();
} else {
cb(err);
}
} else {
transferredFileCount++;
emitData.transferredFileCount = transferredFileCount;
thisDeployer.emit('uploaded', emitData);
cb();
}
});
}
function ftpMakeDirectoriesIfNeeded(cb) {
async.eachSeries(partialDirectories, ftpMakeRemoteDirectoryIfNeeded, err => {
cb(err);
});
}
// A method for changing the remote working directory and creating one if it doesn't already exist
function ftpMakeRemoteDirectoryIfNeeded(partialRemoteDirectory, cb) {
// Add the remote root, and clean up the slashes
let fullRemoteDirectory = remoteRoot + '/' + partialRemoteDirectory.replace(/\\/gi, '/');
// Add leading slash if it is missing
if (fullRemoteDirectory.charAt(0) !== '/') {
fullRemoteDirectory = '/' + fullRemoteDirectory;
}
// Remove double // if present
fullRemoteDirectory = fullRemoteDirectory.replace(/\/\//g, '/');
ftp.raw('cwd', fullRemoteDirectory, (err) => {
if (err) {
ftp.raw('mkd', fullRemoteDirectory, (err) => {
if (err) {
cb(err);
} else {
ftpMakeRemoteDirectoryIfNeeded(partialRemoteDirectory, cb);
}
});
} else {
cb();
}
});
}
this.deploy = function (config, cb) {
// Prompt for password if none was given
if (config.password) {
configComplete(config, cb);
} else {
read({prompt: 'Password for ' + config.username + '@' + config.host + ' (ENTER for none): ', default: '', silent: true}, (err, res) => {
if (err) {
return cb(err);
}
config.password = res;
configComplete(config, cb);
});
}
};
function configComplete(config, cb) {
// Init
ftp = new Ftp({
host: config.host,
port: config.port
});
localRoot = config.localRoot;
remoteRoot = config.remoteRoot;
if (has(config, 'continueOnError')) {
continueOnError = config.continueOnError;
}
exclude = config.exclude || exclude;
include = config.include || include;
ftp.useList = true;
dirParseSync(localRoot);
// Authentication and main processing of files
ftp.auth(config.username, config.password, err => {
if (err) {
cb(err);
} else {
ftpMakeDirectoriesIfNeeded(err => {
if (err) {
// If there was an error creating a remote directory we can't continue to upload files
cb(err);
} else {
async.eachSeries(partialFilePaths, ftpPut, err => {
if (err) {
cb(err);
} else {
ftp.raw('quit', (err, data) => {
cb(err);
});
}
});
}
});
}
});
}
};
util.inherits(FtpDeployer, events.EventEmitter);
module.exports = FtpDeployer;