123 lines
3.5 KiB
JavaScript
123 lines
3.5 KiB
JavaScript
const { Client } = require('ssh2');
|
|
const fs = require('fs');
|
|
|
|
const config = {
|
|
host: 'hs.yifenbao.cn',
|
|
port: 22,
|
|
username: 'root',
|
|
password: 'Torch1112'
|
|
};
|
|
|
|
const conn = new Client();
|
|
|
|
conn.on('ready', async () => {
|
|
console.log('SSH连接成功');
|
|
|
|
try {
|
|
console.log('\n=== 1. 检查宿主机上的JS文件时间 ===');
|
|
await executeCommand('ls -la /www/huishou/dist/build/h5/assets/index-CFIPh5xV.js');
|
|
|
|
console.log('\n=== 2. 重启整个容器环境 ===');
|
|
await executeCommand('cd /www/huishou && docker-compose down');
|
|
|
|
console.log('\n=== 3. 清理旧的H5文件 ===');
|
|
await executeCommand('rm -rf /www/huishou/dist/build/h5/*');
|
|
|
|
console.log('\n=== 4. 重新复制新的H5文件 ===');
|
|
await uploadH5Files();
|
|
|
|
console.log('\n=== 5. 重新构建并启动容器 ===');
|
|
await executeCommand('cd /www/huishou && docker-compose build --no-cache && docker-compose up -d');
|
|
|
|
console.log('\n=== 6. 等待服务启动 ===');
|
|
await sleep(15000);
|
|
|
|
console.log('\n=== 7. 测试H5访问 ===');
|
|
await executeCommand('curl -s http://localhost:4001/h5/');
|
|
|
|
console.log('\n=== 8. 测试JS资源 ===');
|
|
await executeCommand('curl -s -o /dev/null -w "JS: %{http_code}, Size: %{size_download} bytes\n" http://localhost:4001/h5/assets/index-CFIPh5xV.js');
|
|
|
|
} catch (error) {
|
|
console.error('部署失败:', error.message);
|
|
} finally {
|
|
conn.end();
|
|
}
|
|
});
|
|
|
|
async function uploadH5Files() {
|
|
const h5Dir = './dist/build/h5';
|
|
const files = [];
|
|
|
|
function collect(dir, prefix = '') {
|
|
const items = fs.readdirSync(dir);
|
|
for (const item of items) {
|
|
const fullPath = require('path').join(dir, item);
|
|
const stats = fs.statSync(fullPath);
|
|
const relPath = prefix ? require('path').join(prefix, item) : item;
|
|
if (stats.isDirectory()) {
|
|
collect(fullPath, relPath);
|
|
} else {
|
|
files.push({ local: fullPath, remote: `/www/huishou/dist/build/h5/${relPath}` });
|
|
}
|
|
}
|
|
}
|
|
|
|
collect(h5Dir);
|
|
console.log(`准备上传 ${files.length} 个文件...`);
|
|
|
|
return new Promise((resolve) => {
|
|
conn.sftp((err, sftp) => {
|
|
if (err) {
|
|
console.error('SFTP错误:', err.message);
|
|
resolve();
|
|
return;
|
|
}
|
|
|
|
let uploaded = 0;
|
|
for (const file of files) {
|
|
const dir = require('path').dirname(file.remote);
|
|
sftp.mkdir(dir, { recursive: true }, () => {
|
|
const readStream = fs.createReadStream(file.local);
|
|
const writeStream = sftp.createWriteStream(file.remote);
|
|
writeStream.on('close', () => {
|
|
uploaded++;
|
|
if (uploaded === files.length) {
|
|
console.log('所有文件上传完成');
|
|
sftp.end();
|
|
resolve();
|
|
}
|
|
});
|
|
readStream.pipe(writeStream);
|
|
});
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
function executeCommand(cmd) {
|
|
return new Promise((resolve) => {
|
|
conn.exec(cmd, (err, stream) => {
|
|
if (err) {
|
|
console.error('命令执行错误:', err.message);
|
|
resolve();
|
|
return;
|
|
}
|
|
|
|
stream.stdout.on('data', (data) => process.stdout.write(data));
|
|
stream.stderr.on('data', (data) => process.stdout.write(data));
|
|
|
|
stream.on('close', () => resolve());
|
|
});
|
|
});
|
|
}
|
|
|
|
function sleep(ms) {
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
}
|
|
|
|
conn.on('error', (err) => {
|
|
console.error('SSH连接错误:', err.message);
|
|
});
|
|
|
|
conn.connect(config); |