-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
75 lines (46 loc) · 1.39 KB
/
index.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
'use strict';
const path = require('path');
const chalk = require('chalk');
const client = require('scp2');
class SftpUpload {
constructor(props = {}) {
// default port is 22
this.options = Object.assign({ port: 22 }, props);
}
apply(compiler) {
compiler.plugin('done', () => {
// options = { host, username, password, localPath, path, title }
let list = ['host', 'username', 'password', 'localPath', 'path'], // required character
options = this.options,
start = Date.now(),
empty = null,
end = null;
for( let i = 0, len = list.length; i < len; i++ ) {
if( !options[ list[i] ] ) {
empty = list[i];
break;
}
};
if( empty ) {
console.log(chalk.red(`Error: ${ empty } is required character`));
// if required character is empty, and it exit in process
process.exit();
};
client.scp(options.localPath, options, (err) => {
if( err ) {
// console error
console.log(chalk.red(err));
} else {
// close connect
client.close();
end = Date.now();
// console success
console.log(
`Upload successly folder in ${ options.localPath } to ${ options.title ? ( chalk.underline(options.title) + ' in server' ) : chalk.underline( 'sftp://' + options.host + ':' + options.port ) },it spent ${ end - start }ms`
);
}
})
})
}
}
module.exports = SftpUpload