-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
149 lines (139 loc) · 5.2 KB
/
Copy pathindex.js
File metadata and controls
149 lines (139 loc) · 5.2 KB
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
const fs = require('fs');
const path = require('path');
const Client = require('ssh2').Client;
const archiver = require('archiver');
const conn = new Client();
class sshUploadPlugin {
constructor(options) {
this.options = options;
this.fileName;
this.remotePath;
this.outputFirPath;
}
apply(compiler) {
compiler.hooks.done.tap('sshUploadPlugin', async (stats) => {
const outputPath = stats.compilation.outputOptions.path || this.options.outputPath;
this.fileName = path.basename(outputPath);
await this.buildZip(outputPath);
this.connect(outputPath);
});
}
// 项目打成tar包
buildZip = async (outputPath) => {
await new Promise((resolve, reject) => {
console.log(`打包 ${outputPath} Tar.gz`)
const archive = archiver('tar', {
gzip: true,
gzipOptions: {
level: 1
}
}).on('error', (e) => {
console.error(e)
})
const output = fs
.createWriteStream(`${outputPath}.tar.gz`)
.on('close', (e) => {
if (e) {
console.error(`打包tar.gz出错: ${e}`)
reject(e)
process.exit(1)
} else {
console.log(`${outputPath}.tar.gz打包成功`)
resolve()
}
})
archive.pipe(output)
archive.directory(outputPath, `${this.fileName}`)
archive.finalize()
})
}
// 创建SSH连接
connect(outputPath) {
const privateKeyPath = this.options.config.privateKeyPath;
// 监听ready事件
conn.on('ready', () => {
console.log('SSH连接成功');
this.remoteFirPath = `${this.options.config.remotePath}/${this.fileName}.tar.gz`;
this.outputFirPath = `${outputPath}.tar.gz`;
conn.sftp((err, sftp) => {
if (err) throw err;
const readStream = fs.createReadStream(this.outputFirPath);
readStream.on('error', err => {
console.log('error');
});
const writeStream = sftp.createWriteStream(this.remoteFirPath);
writeStream.on('close', () => {
console.log(this.remoteFirPath, '文件上传完成');
// 判断是否要先删除远程文件夹,默认清除远程文件夹
if (this.options.clearRemoteDir !== false) {
this.clearRemoteDir()
} else {
this.tarRemoteFile();
}
});
writeStream.on('error', err => {
console.log('error');
});
readStream.pipe(writeStream);
});
}).connect({
timeout: this.options.config.timeout || 10000, // 10s
host: this.options.config.host,
port: this.options.config.port || 22,
username: this.options.config.username,
password: this.options.config.password,
remotePath: this.options.config.remotePath,
privateKey: privateKeyPath?fs.readFileSync(privateKeyPath):null
});
// 监听error事件
conn.on('error', (err) => {
console.error('SSH连接失败', err);
});
// 结束SSH连接
conn.on('end', () => {
console.log('SSH连接已断开');
});
}
// 清理远程目录
clearRemoteDir() {
const command = `rm -rf ${this.options.config.remotePath}/${this.fileName}`;
conn.exec(command, (err, stream) => {
if (err) throw err;
stream.on('close', (code, signal) => {
console.log(`Remote command exited with code ${code}.`);
this.tarRemoteFile()
}).on('data', (data) => {
console.log(`STDOUT: ${data}`);
}).stderr.on('data', (data) => {
console.log(`STDERR: ${data}`);
});
});
}
// 解压缩项目
tarRemoteFile() {
const command = `tar -xzf ${this.remoteFirPath} -C ${this.options.config.remotePath} && rm -rf ${this.remoteFirPath}`;
conn.exec(command, (err, stream) => {
if (err) throw err;
stream.on('close', (code, signal) => {
console.log(`Remote command exited with code ${code}.`);
this.removeLocalFile()
}).on('data', (data) => {
console.log(`STDOUT: ${data}`);
}).stderr.on('data', (data) => {
console.log(`STDERR: ${data}`);
});
});
}
// 删除本地打包文件
removeLocalFile = () => {
const localPath = `${this.outputFirPath}`
console.log('删除本地压缩包...');
// 删除本地文件
fs.unlink(localPath, (err) => {
if (err) throw err;
console.log(`Local file ${localPath} deleted.`);
conn.end();
});
}
}
module.exports = sshUploadPlugin;