85 lines
2.3 KiB
JavaScript
85 lines
2.3 KiB
JavaScript
import { Client } from 'ssh2';
|
|
import fs from 'fs';
|
|
|
|
const conn = new Client();
|
|
|
|
conn.on('ready', () => {
|
|
console.log('=== 部署管理后台代码 ===');
|
|
|
|
console.log('Step 1: 上传 index.html');
|
|
fs.readFile('./admin/index.html', (err, data) => {
|
|
if (err) {
|
|
console.log('读取文件失败:', err);
|
|
conn.end();
|
|
return;
|
|
}
|
|
|
|
conn.sftp((err, sftp) => {
|
|
if (err) {
|
|
console.log('SFTP连接失败:', err);
|
|
conn.end();
|
|
return;
|
|
}
|
|
|
|
const writeStream = sftp.createWriteStream('/www/huishou/admin/index.html');
|
|
writeStream.on('close', () => {
|
|
console.log('Step 1完成');
|
|
|
|
console.log('Step 2: 上传图标文件');
|
|
const iconsDir = './admin/icons/';
|
|
fs.readdir(iconsDir, (err, files) => {
|
|
if (err) {
|
|
console.log('读取图标目录失败:', err);
|
|
conn.end();
|
|
return;
|
|
}
|
|
|
|
let uploaded = 0;
|
|
files.forEach(file => {
|
|
if (file.endsWith('.png') || file.endsWith('.jpg') || file.endsWith('.jpeg') || file.endsWith('.gif')) {
|
|
fs.readFile(iconsDir + file, (err, iconData) => {
|
|
if (err) {
|
|
console.log('读取图标文件失败:', file, err);
|
|
uploaded++;
|
|
if (uploaded >= files.length) conn.end();
|
|
return;
|
|
}
|
|
|
|
const iconStream = sftp.createWriteStream('/www/huishou/admin/icons/' + file);
|
|
iconStream.on('close', () => {
|
|
uploaded++;
|
|
if (uploaded >= files.length) {
|
|
console.log('Step 2完成');
|
|
conn.end();
|
|
}
|
|
});
|
|
iconStream.write(iconData);
|
|
iconStream.end();
|
|
});
|
|
} else {
|
|
uploaded++;
|
|
}
|
|
});
|
|
|
|
if (files.length === 0) {
|
|
console.log('Step 2完成');
|
|
conn.end();
|
|
}
|
|
});
|
|
});
|
|
writeStream.write(data);
|
|
writeStream.end();
|
|
});
|
|
});
|
|
});
|
|
|
|
conn.on('error', (err) => {
|
|
console.log('连接错误:', err.message);
|
|
});
|
|
|
|
conn.connect({
|
|
host: 'hs.yifenbao.cn',
|
|
port: 22,
|
|
username: 'root',
|
|
password: 'Torch1112'
|
|
}); |