-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.js
70 lines (60 loc) · 1.9 KB
/
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
#!/usr/bin/env node
const fs = require('fs');
// const Client = require('ssh2').Client;
const glob = require('glob');
const basePath = './contents'; // path on github
const destinationPath = '/home/ntropyovzg/www/zws/contents'; // path on server
const config = {
host: process.env.FTP_HOST,
password: process.env.FTP_PASSWORD,
username: process.env.FTP_USER,
};
var numberOfOpLaunched = 0;
var numberOfOpTerminated = 0;
let Client = require('ssh2-sftp-client');
let sftp = new Client();
// Check if the path is a directory and
// either create the directory on the server
// if it is a directory, or upload the file
// if it is a file.
function handlePath(path) {
const relativeFile = path.replace(`${basePath}/`, '');
const destination = `${destinationPath}/${relativeFile}`;
if (fs.lstatSync(path).isDirectory()) {
return createDirectory(destination);
}
return uploadFile(path, destination);
}
function createDirectory(destination) {
console.log(`createDirectory=${destination}`);
numberOfOpLaunched++;
return sftp.mkdir(destination).then(() => {
manageTerminatedOperation();
}).catch((err) => {
manageTerminatedOperation();
});
}
function uploadFile(file, destination) {
console.log(`uploadFile ${file}`);
numberOfOpLaunched++;
return sftp.put(file, destination).then(() => {
manageTerminatedOperation();
}).catch((err) => {
console.log(`${err}`);
manageTerminatedOperation();
});
}
function manageTerminatedOperation() {
numberOfOpTerminated++;
if (numberOfOpTerminated >= numberOfOpLaunched) {
console.log(`Process terminated with ${numberOfOpTerminated}`);
sftp.end();
} else {
console.log(`Op terminated ; numberOfOpTerminated=${numberOfOpTerminated} ; numberOfOpLaunched=${numberOfOpLaunched}`);
}
}
sftp.connect(config).then(() => {
glob.sync(`${basePath}/**/*`).forEach(handlePath);
}).catch((err) => {
console.log(err, 'catch error');
});