-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·57 lines (44 loc) · 1.26 KB
/
app.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
'use strict';
var fs = require('fs')
, path = require('path')
, FTPClient = require('ftp')
, Watcher = require('./watcher').Watcher
, ftpClient = new FTPClient()
, cfg;
var watchFiles = function (rules, ftp) {
rules.forEach(function (data) {
var pattern = data.cwd + data.pattern;
console.log('Start watching for %s', pattern);
new Watcher(pattern, function (event, filepath) {
console.log('\n%s %s', event, filepath);
console.time('Upload file');
ftp.put(filepath, data.remoteDir + path.relative(data.cwd, filepath), function (err) {
if (err) throw err;
console.timeEnd('Upload file');
console.log('File successfully uploaded');
});
});
});
};
ftpClient.on('ready', function () {
console.log('FTP connection is opened\n');
watchFiles(cfg.rules, this);
});
ftpClient.on('error', function (err) {
if (err) throw err;
});
ftpClient.on('end', function () {
console.log('FTP connection has ended');
});
ftpClient.on('close', function () {
console.log('FTP connection has closed');
});
fs.readFile(path.resolve(__dirname, './config.json'), function (err, stream) {
if (err) throw err;
try {
cfg = JSON.parse(stream);
ftpClient.connect(cfg.ftp);
} catch (e) {
throw e;
}
});