oms/frontend/scripts/rollback.mjs
liufumingren 441659d303 feat(frontend): 新增蓝绿发布脚本与生产 API 地址
发布:
- 新增 scripts/deploy.mjs、rollback.mjs、init-config.mjs 与 scripts/remote/{deploy,rollback}.sh
  · 服务器上按 releases/<版本号> 存放,current 软链原子切换,发布/回滚都不中断服务
  · 切换前校验 index.html 与引用的 /assets 资源完整,并补齐历史版本静态资源(旧 chunk 不 404)
  · 真实配置 scripts/deploy.config.json 已加入忽略清单,模板 deploy.config.example.json 入库
- package.json 增加 deploy / rollback / deploy:init

接口地址:
- 新增 .env.production(VITE_API_BASE_URL=https://api.oms.gztbyc.com/api)与 .env.development
  (/api,继续走 vite 代理到本地 6090)
- request.ts 的 baseURL 改为读 VITE_API_BASE_URL;env.d.ts 声明 ImportMetaEnv
- 新增 utils/media.ts#mediaUrl:跨域部署时把后端返回的相对地址(/api/files/raw/…)
  补全为 API 站点绝对地址,用于 ImageUploader、商品列表图片、主体账号头像渲染

工程:
- tsconfig 去掉已弃用的 baseUrl(TS 7 将移除;paths 自 TS 4.1 起不再依赖它)
- .gitignore 增加 dist.tar.gz 与真实部署配置
2026-09-14 22:14:27 +08:00

67 lines
2.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env node
/**
* OMS 前端蓝绿回滚脚本(Windows / macOS / Linux 通用)
*
* 回滚原理:把服务器上的 `current` 软链接原子切回 releases 里按时间排序的上一个版本。
* 不做构建、不重新上传,秒级完成。
*
* 依赖本机已安装:ssh(Windows 10 1803+ 自带 OpenSSH 客户端)。
*
* 用法:
* npm run rollback
*/
import { spawnSync } from 'node:child_process';
import { existsSync, readFileSync } from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// --- 配置(读取独立配置文件,该文件不纳入版本控制) ---
const CONFIG_PATH = path.join(__dirname, 'deploy.config.json');
if (!existsSync(CONFIG_PATH)) {
console.error('❌ 缺少配置文件 scripts/deploy.config.json');
console.error(' 请复制 scripts/deploy.config.example.json 为 scripts/deploy.config.json 并修改其中参数。');
process.exit(1);
}
const config = JSON.parse(readFileSync(CONFIG_PATH, 'utf8'));
const REQUIRED_KEYS = ['user', 'ip', 'basePath'];
const missingKeys = REQUIRED_KEYS.filter((k) => !config[k]);
if (missingKeys.length) {
console.error(`❌ scripts/deploy.config.json 缺少必填项:${missingKeys.join(', ')}`);
process.exit(1);
}
const USER = config.user;
const IP = config.ip; // 服务器 IP
const BASE_PATH = config.basePath; // 站点在服务器上的存放路径
const SSH_KEY = resolveSshKey(config.sshKey);
const SSH_OPTS = ['-o', 'StrictHostKeyChecking=no', ...(SSH_KEY ? ['-i', SSH_KEY] : [])];
const HOST = `${USER}@${IP}`;
/** sshKey 支持「绝对路径」与「~/.ssh 下的文件名」;留空则用 ssh 默认密钥/agent */
function resolveSshKey(value) {
if (!value) return '';
if (value.startsWith('~')) return path.join(os.homedir(), value.slice(1));
return path.isAbsolute(value) ? value : path.join(os.homedir(), '.ssh', value);
}
console.log('⏪ 正在回滚 OMS 前端到上一个版本...');
const remoteScript = readFileSync(path.join(__dirname, 'remote', 'rollback.sh'), 'utf8');
const res = spawnSync('ssh', [...SSH_OPTS, HOST, `BASE='${BASE_PATH}' bash -s`], {
input: remoteScript,
stdio: ['pipe', 'inherit', 'inherit'],
});
if (res.error) {
console.error(`❌ 无法执行 ssh: ${res.error.message}`);
if (res.error.code === 'ENOENT') {
console.error(' 请确认本机已安装 OpenSSH 客户端(Windows 10 1803+ 自带)。');
}
process.exit(1);
}
process.exit(res.status ?? 1);