Files
huishou/minimal-deploy.js
2026-07-27 14:07:26 +08:00

109 lines
3.5 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();
const localH5Dir = path.join(__dirname, 'dist', 'build', 'h5');
const execCmd = (cmd) => {
return new Promise((resolve, reject) => {
conn.exec(cmd, (err, stream) => {
if (err) {
reject(err);
return;
}
let output = '';
stream.stdout.on('data', (data) => {
output += data.toString();
});
stream.stderr.on('data', (data) => {
output += data.toString();
});
stream.on('close', (code) => {
resolve({ code, output });
});
});
});
};
conn.on('ready', async () => {
console.log('SSH连接成功');
try {
console.log('1. 创建临时目录...');
await execCmd('mkdir -p /tmp/huishou-h5/assets');
console.log('2. 上传index.html...');
const indexHtml = fs.readFileSync(`${localH5Dir}/index.html`, 'utf8');
const indexBase64 = Buffer.from(indexHtml).toString('base64');
await execCmd(`echo '${indexBase64}' | base64 -d > /tmp/huishou-h5/index.html`);
console.log(' ✓ index.html');
console.log('3. 上传称重页面JS...');
const assetFiles = fs.readdirSync(`${localH5Dir}/assets`);
const weighJs = assetFiles.find(f => f.includes('pages-weigh'));
if (weighJs) {
const content = fs.readFileSync(`${localH5Dir}/assets/${weighJs}`);
const base64 = content.toString('base64');
await execCmd(`echo '${base64}' | base64 -d > /tmp/huishou-h5/assets/${weighJs}`);
console.log(`${weighJs}`);
}
console.log('4. 上传首页JS...');
const indexJs = assetFiles.find(f => f.includes('pages-index'));
if (indexJs) {
const content = fs.readFileSync(`${localH5Dir}/assets/${indexJs}`);
const base64 = content.toString('base64');
await execCmd(`echo '${base64}' | base64 -d > /tmp/huishou-h5/assets/${indexJs}`);
console.log(`${indexJs}`);
}
console.log('5. 上传其他JS文件...');
const jsFiles = assetFiles.filter(f => f.endsWith('.js'));
for (const file of jsFiles) {
if (file === weighJs || file === indexJs) continue;
const content = fs.readFileSync(`${localH5Dir}/assets/${file}`);
const base64 = content.toString('base64');
await execCmd(`echo '${base64}' | base64 -d > /tmp/huishou-h5/assets/${file}`);
console.log(`${file}`);
}
console.log('6. 上传CSS文件...');
const cssFiles = assetFiles.filter(f => f.endsWith('.css'));
for (const file of cssFiles) {
const content = fs.readFileSync(`${localH5Dir}/assets/${file}`);
const base64 = content.toString('base64');
await execCmd(`echo '${base64}' | base64 -d > /tmp/huishou-h5/assets/${file}`);
console.log(`${file}`);
}
console.log('7. 复制到Docker容器...');
await execCmd('docker cp /tmp/huishou-h5/. huishou:/usr/share/nginx/html/h5/');
console.log(' ✓ 文件已复制到容器');
console.log('8. 验证...');
const result = await execCmd('docker exec huishou grep -c "inputMode" /usr/share/nginx/html/h5/assets/pages-weigh-*.js');
console.log(' inputMode出现次数:', result.output.trim());
console.log('\n✅ 部署完成!');
} catch (err) {
console.error('部署失败:', err.message);
} finally {
conn.end();
}
});
conn.on('error', (err) => {
console.error('SSH连接错误:', err.message);
});
conn.connect(config);