82 lines
2.4 KiB
JavaScript
82 lines
2.4 KiB
JavaScript
const { Client } = require('ssh2');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const config = {
|
|
host: 'hs.yifenbao.cn',
|
|
port: 22,
|
|
username: 'root',
|
|
password: 'Torch1112'
|
|
};
|
|
|
|
const conn = new Client();
|
|
|
|
conn.on('ready', () => {
|
|
console.log('SSH连接成功');
|
|
|
|
conn.exec('mkdir -p /www/huishou/dist/build/h5/assets', (err, stream) => {
|
|
stream.on('close', () => {
|
|
console.log('目录创建完成');
|
|
|
|
conn.sftp((err, sftp) => {
|
|
if (err) {
|
|
console.error('SFTP连接失败:', err.message);
|
|
conn.end();
|
|
return;
|
|
}
|
|
|
|
const localAssetsDir = path.join(__dirname, 'dist', 'build', 'h5', 'assets');
|
|
const remoteAssetsDir = '/www/huishou/dist/build/h5/assets';
|
|
|
|
fs.readdir(localAssetsDir, (err, files) => {
|
|
if (err) {
|
|
console.error('读取本地目录失败:', err.message);
|
|
conn.end();
|
|
return;
|
|
}
|
|
|
|
let uploaded = 0;
|
|
|
|
files.forEach(file => {
|
|
const localPath = path.join(localAssetsDir, file);
|
|
const remotePath = `${remoteAssetsDir}/${file}`;
|
|
|
|
console.log(`上传: ${file}`);
|
|
sftp.fastPut(localPath, remotePath, (err) => {
|
|
if (err) {
|
|
console.error(`上传失败 ${file}:`, err.message);
|
|
}
|
|
uploaded++;
|
|
if (uploaded === files.length) {
|
|
console.log('\n所有assets文件上传完成');
|
|
|
|
// 上传index.html
|
|
console.log('上传index.html...');
|
|
sftp.fastPut('./dist/build/h5/index.html', '/www/huishou/dist/build/h5/index.html', (err) => {
|
|
if (err) {
|
|
console.error('index.html上传失败:', err.message);
|
|
} else {
|
|
console.log('index.html上传成功');
|
|
}
|
|
|
|
conn.exec('docker restart huishou', (err, stream) => {
|
|
stream.on('close', () => {
|
|
console.log('容器已重启');
|
|
conn.end();
|
|
});
|
|
});
|
|
});
|
|
}
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
conn.on('error', (err) => {
|
|
console.error('SSH连接错误:', err.message);
|
|
});
|
|
|
|
conn.connect(config); |