-
Notifications
You must be signed in to change notification settings - Fork 8
/
deploy-ftp.js
76 lines (64 loc) · 1.87 KB
/
deploy-ftp.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
const ftp = require('basic-ftp');
const { readdirSync, statSync } = require('fs');
const path = require('path');
// ESM: import * as ftp from "basic-ftp"
copyBuildDirToServerFTP();
async function copyBuildDirToServerFTP() {
const client = new ftp.Client();
let files = [];
let totalFilesSent = 0;
const countTotalFiles = recursivelyCountFiles('./build').length;
client.trackProgress((info) => {
process.stdout.clearLine();
process.stdout.cursorTo(0);
if (info.bytes > 0 && !files.includes(info.name)) {
files.push(info.name);
console.log(info.name);
totalFilesSent++;
}
process.stdout.write(
`###### Total bytes: ${formatBytes(
info.bytesOverall
)} | Total Files: ${totalFilesSent}/${countTotalFiles}`
);
});
try {
await client.access({
host: process.env.FTP_HOST,
user: process.env.FTP_USER,
password: process.env.FTP_PASSWORD,
secure: false
});
await client.uploadFromDir('./build/', 'public_html/');
client.trackProgress();
} catch (err) {
console.log(err);
}
client.close();
}
/* *
*
* Auxiliar functions
*
* */
function formatBytes(bytes, decimals = 2) {
if (!+bytes) return '0 Bytes';
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
}
function recursivelyCountFiles(dirPath, files) {
const filesCurrentDir = readdirSync(dirPath);
let allFiles = files || [];
for (const fileName of filesCurrentDir) {
const filePath = path.join(dirPath, fileName);
if (statSync(filePath).isDirectory()) {
allFiles = recursivelyCountFiles(filePath, allFiles);
} else {
allFiles.push(fileName);
}
}
return allFiles;
}