100 lines
3.3 KiB
JavaScript
100 lines
3.3 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', () => {
|
|
console.log('SSH连接成功');
|
|
|
|
// 先创建目录结构
|
|
conn.exec('mkdir -p /www/huishou/dist/build/h5/assets', (err, stream) => {
|
|
if (err) {
|
|
console.error('创建目录失败:', err.message);
|
|
conn.end();
|
|
return;
|
|
}
|
|
|
|
stream.on('close', () => {
|
|
console.log('目录已创建');
|
|
|
|
// 读取index.html并上传
|
|
const indexHtml = fs.readFileSync('./dist/build/h5/index.html', 'utf8');
|
|
const escapedHtml = indexHtml.replace(/\$/g, '\\$').replace(/`/g, '\\`');
|
|
|
|
conn.exec(`cd /www/huishou/dist/build/h5 && echo '${escapedHtml}' > index.html`, (err, stream) => {
|
|
if (err) {
|
|
console.error('上传index.html失败:', err.message);
|
|
conn.end();
|
|
return;
|
|
}
|
|
|
|
stream.on('close', () => {
|
|
console.log('index.html上传成功');
|
|
|
|
// 上传首页JS文件
|
|
const jsFile = fs.readdirSync('./dist/build/h5/assets').find(f => f.includes('pages-index-index'));
|
|
if (jsFile) {
|
|
const jsContent = fs.readFileSync(`./dist/build/h5/assets/${jsFile}`, 'utf8');
|
|
const base64Content = Buffer.from(jsContent).toString('base64');
|
|
|
|
console.log(`准备上传 ${jsFile}...`);
|
|
|
|
conn.exec(`cd /www/huishou/dist/build/h5/assets && echo '${base64Content}' | base64 -d > ${jsFile}`, (err, stream) => {
|
|
if (err) {
|
|
console.error('上传JS文件失败:', err.message);
|
|
conn.end();
|
|
return;
|
|
}
|
|
|
|
stream.on('close', () => {
|
|
console.log(`${jsFile}上传成功`);
|
|
|
|
// 验证文件大小
|
|
conn.exec(`ls -la /www/huishou/dist/build/h5/assets/${jsFile}`, (err, stream) => {
|
|
let output = '';
|
|
stream.stdout.on('data', data => output += data);
|
|
stream.on('close', () => {
|
|
console.log('文件详情:', output);
|
|
|
|
// 验证内容
|
|
conn.exec(`grep -o "新商户" /www/huishou/dist/build/h5/assets/${jsFile} | head -3`, (err, stream) => {
|
|
let grepOutput = '';
|
|
stream.stdout.on('data', data => grepOutput += data);
|
|
stream.on('close', () => {
|
|
console.log('验证结果:', grepOutput);
|
|
|
|
// 重启容器
|
|
conn.exec('docker restart huishou', (err, stream) => {
|
|
stream.on('close', () => {
|
|
console.log('容器已重启');
|
|
conn.end();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
} else {
|
|
console.log('没有找到首页JS文件');
|
|
conn.end();
|
|
}
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
conn.on('error', (err) => {
|
|
console.error('SSH连接错误:', err.message);
|
|
});
|
|
|
|
conn.connect(config); |