fe-build-cli 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +430 -0
- package/package.json +41 -0
- package/src/cli.js +492 -0
- package/src/config-template.js +83 -0
- package/src/deploy-core.js +326 -0
- package/src/dingtalk.js +238 -0
- package/src/git-branch.js +259 -0
- package/src/index.d.ts +330 -0
- package/src/index.js +47 -0
- package/src/ssh-client.js +153 -0
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import { Client } from 'ssh2';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* SSH 客户端类,用于连接服务器并执行命令、上传文件
|
|
6
|
+
*/
|
|
7
|
+
export class SSHClient {
|
|
8
|
+
constructor(config) {
|
|
9
|
+
this.config = config;
|
|
10
|
+
this.client = new Client();
|
|
11
|
+
this.sftp = null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 建立 SSH 连接
|
|
16
|
+
* @returns {Promise<void>}
|
|
17
|
+
*/
|
|
18
|
+
async connect() {
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
this.client.connect({
|
|
21
|
+
host: this.config.sshHost,
|
|
22
|
+
port: this.config.sshPort || 22,
|
|
23
|
+
username: this.config.sshUser,
|
|
24
|
+
privateKey: fs.readFileSync(this.config.sshKeyPath),
|
|
25
|
+
readyTimeout: 15000
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
this.client.on('ready', () => {
|
|
29
|
+
console.log('✅ SSH 连接成功');
|
|
30
|
+
resolve();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
this.client.on('error', err => {
|
|
34
|
+
console.error('❌ SSH 连接失败:', err.message);
|
|
35
|
+
reject(err);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 在远程服务器执行命令
|
|
42
|
+
* @param {string} command - 要执行的命令
|
|
43
|
+
* @returns {Promise<string>} 命令输出
|
|
44
|
+
*/
|
|
45
|
+
async execCommand(command) {
|
|
46
|
+
return new Promise((resolve, reject) => {
|
|
47
|
+
this.client.exec(command, (err, stream) => {
|
|
48
|
+
if (err) {
|
|
49
|
+
reject(err);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
let output = '';
|
|
54
|
+
let errorOutput = '';
|
|
55
|
+
|
|
56
|
+
stream.on('data', data => {
|
|
57
|
+
const chunk = data.toString();
|
|
58
|
+
output += chunk;
|
|
59
|
+
process.stdout.write(chunk);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
stream.stderr.on('data', data => {
|
|
63
|
+
const chunk = data.toString();
|
|
64
|
+
errorOutput += chunk;
|
|
65
|
+
process.stderr.write(chunk);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
stream.on('close', code => {
|
|
69
|
+
if (code === 0) {
|
|
70
|
+
resolve(output);
|
|
71
|
+
} else {
|
|
72
|
+
reject(new Error(`命令执行失败,退出码: ${code}, 错误信息: ${errorOutput}`));
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 上传文件到远程服务器(带进度条)
|
|
81
|
+
* @param {string} localPath - 本地文件路径
|
|
82
|
+
* @param {string} remotePath - 远程文件路径
|
|
83
|
+
* @returns {Promise<void>}
|
|
84
|
+
*/
|
|
85
|
+
async uploadFile(localPath, remotePath) {
|
|
86
|
+
const stats = fs.statSync(localPath);
|
|
87
|
+
const totalBytes = stats.size;
|
|
88
|
+
const startTime = Date.now();
|
|
89
|
+
|
|
90
|
+
// 格式化字节数为可读格式
|
|
91
|
+
function formatBytes(bytes) {
|
|
92
|
+
if (bytes < 1024) return bytes + ' B';
|
|
93
|
+
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB';
|
|
94
|
+
return (bytes / (1024 * 1024)).toFixed(2) + ' MB';
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// 渲染进度条
|
|
98
|
+
function renderBar(transferred, total) {
|
|
99
|
+
const percent = Math.round((transferred / total) * 100);
|
|
100
|
+
const barWidth = 30;
|
|
101
|
+
const filled = Math.round((percent / 100) * barWidth);
|
|
102
|
+
const bar = '█'.repeat(filled) + '░'.repeat(barWidth - filled);
|
|
103
|
+
const elapsed = (Date.now() - startTime) / 1000;
|
|
104
|
+
const speed = elapsed > 0 ? transferred / elapsed : 0;
|
|
105
|
+
process.stdout.write(
|
|
106
|
+
`\r上传进度: [${bar}] ${percent}% ${formatBytes(transferred)}/${formatBytes(total)} ${formatBytes(speed)}/s`
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return new Promise((resolve, reject) => {
|
|
111
|
+
this.client.sftp((err, sftp) => {
|
|
112
|
+
if (err) {
|
|
113
|
+
reject(err);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
sftp.fastPut(
|
|
118
|
+
localPath,
|
|
119
|
+
remotePath,
|
|
120
|
+
{
|
|
121
|
+
step: (transferred, _chunk, total) => {
|
|
122
|
+
renderBar(transferred, total);
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
err => {
|
|
126
|
+
if (err) {
|
|
127
|
+
reject(err);
|
|
128
|
+
} else {
|
|
129
|
+
renderBar(totalBytes, totalBytes);
|
|
130
|
+
process.stdout.write('\n');
|
|
131
|
+
console.log(`✅ 上传成功: ${localPath} -> ${remotePath}`);
|
|
132
|
+
resolve();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* 断开 SSH 连接
|
|
142
|
+
* @returns {Promise<void>}
|
|
143
|
+
*/
|
|
144
|
+
async disconnect() {
|
|
145
|
+
return new Promise(resolve => {
|
|
146
|
+
this.client.end();
|
|
147
|
+
console.log('✅ SSH 连接已关闭');
|
|
148
|
+
resolve();
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export default SSHClient;
|