From 978821fe19bb92b92e8f53560f93a359fd2d109e Mon Sep 17 00:00:00 2001 From: Jhonton Chen <504071088@qq.com> Date: Tue, 7 Apr 2026 14:57:26 +0800 Subject: [PATCH] test2 --- deploy.js | 64 +++++++++++++++++++++++++-------------- src/pages/login/index.tsx | 2 +- 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/deploy.js b/deploy.js index a273657..f978fdc 100644 --- a/deploy.js +++ b/deploy.js @@ -4,9 +4,12 @@ const path = require('node:path'); // --- 【配置区】 --- const CONFIG = { - localProjectRoot: path.join(__dirname, '..'), - localReleasesDir: path.join(__dirname, '../releases'), - deployRemote: true, + // 1. 开发源码所在的根目录 + devProjectRoot: path.join(__dirname, './'), + // 2. phpStudy 实际运行网站的目录 (目标位置) + phpStudyPath: 'D:/phpstudy_pro/WWW/alert', + + deployRemote: false, remote: { host: '1.2.3.4', // 服务器IP user: 'root', // 用户名 @@ -14,6 +17,11 @@ const CONFIG = { }, }; +// 预定义几个常用路径 +const DEV_DIST = path.join(CONFIG.devProjectRoot, 'dist'); +const PHPSTUDY_RELEASES = path.join(CONFIG.phpStudyPath, 'releases'); +const PHPSTUDY_CURRENT = path.join(CONFIG.phpStudyPath, 'current'); + // 颜色工具 const log = { info: (msg) => console.log(`\x1b[36m>>> ${msg}\x1b[0m`), @@ -66,11 +74,11 @@ async function start() { // 4. 清理并本地归档 try { log.info('正在清理.map 文件并执行本地归档...'); - cleanSourceMaps(path.join(CONFIG.localProjectRoot, 'dist')); + cleanSourceMaps(DEV_DIST); deployToLocal(version); log.success('本地归档与软链接更新完成'); } catch (_e) { - log.error(`本地归档失败: ${e.message}`); + log.error(`本地归档失败: ${_e.message}`); process.exit(1); } @@ -81,7 +89,7 @@ async function start() { deployToRemote(version); log.success('远程部署任务全部完成'); } catch (_e) { - log.error(`远程部署失败!错误详情: ${e.message}`); + log.error(`远程部署失败!错误详情: ${_e.message}`); log.error( '请检查:1.服务器IP/路径是否正确;2.是否配置了 SSH 免密登录;3.服务器磁盘空间是否充足。', ); @@ -95,28 +103,40 @@ async function start() { // --- 逻辑封装函数 --- function deployToLocal(version) { - const distDir = path.join(CONFIG.localProjectRoot, 'dist'); - const targetDir = path.join(CONFIG.localReleasesDir, version); - const currentLink = path.join(CONFIG.localProjectRoot, 'current'); + const targetDir = path.join(PHPSTUDY_RELEASES, version); - if (!fs.existsSync(CONFIG.localReleasesDir)) - fs.mkdirSync(CONFIG.localReleasesDir); - if (fs.existsSync(targetDir)) + // A. 确保 phpStudy 下的 releases 文件夹存在 + if (!fs.existsSync(PHPSTUDY_RELEASES)) { + fs.mkdirSync(PHPSTUDY_RELEASES, { recursive: true }); + } + + // B. 如果该版本已存在,先删除旧的 + if (fs.existsSync(targetDir)) { fs.rmSync(targetDir, { recursive: true, force: true }); + } - // 移动 dist 到 releases - fs.renameSync(distDir, targetDir); + // C. 【关键】将开发目录下的 dist 复制到 phpStudy 对应的版本目录 + // fs.cpSync 需要 Node.js v16.7.0+ + try { + fs.cpSync(DEV_DIST, targetDir, { recursive: true }); + log.success(`已将产物复制到: ${targetDir}`); + } catch (_e) { + throw new Error(`复制产物到 phpStudy 目录失败: ${_e.message}`); + } - // 更新本地软链接 (Junction) - if (fs.existsSync(currentLink)) { + // D. 更新 phpStudy 目录下的软链接 (Junction) + if (fs.existsSync(PHPSTUDY_CURRENT)) { try { - fs.rmSync(currentLink, { recursive: true, force: true }); + // Windows 下删除目录链接 + fs.rmSync(PHPSTUDY_CURRENT, { recursive: true, force: true }); } catch (_e) { - throw new Error(`无法删除旧的本地软链接: ${e.message}`); + throw new Error(`无法删除 phpStudy 旧的软链接: ${_e.message}`); } } + + // 在 phpStudy 目录下创建链接,指向刚复制过去的版本 const linkType = process.platform === 'win32' ? 'junction' : 'dir'; - fs.symlinkSync(targetDir, currentLink, linkType); + fs.symlinkSync(targetDir, PHPSTUDY_CURRENT, linkType); } function deployToRemote(version) { @@ -131,7 +151,7 @@ function deployToRemote(version) { timeout: 10000, }); } catch (_e) { - throw new Error(`无法连接服务器或创建目录: ${e.message}`); + throw new Error(`无法连接服务器或创建目录: ${_e.message}`); } // B. 使用 scp 上传文件夹 @@ -142,7 +162,7 @@ function deployToRemote(version) { { timeout: 60000 }, ); } catch (_e) { - throw new Error(`文件传输失败 (SCP): ${e.message}`); + throw new Error(`文件传输失败 (SCP): ${_e.message}`); } // C. 更新远程软链接 @@ -150,7 +170,7 @@ function deployToRemote(version) { const sshCmd = `ln -snf ${remoteVersionDir} ${remoteCurrentLink}`; execSync(`ssh ${user}@${host} "${sshCmd}"`); } catch (_e) { - throw new Error(`远程软链接更新失败: ${e.message}`); + throw new Error(`远程软链接更新失败: ${_e.message}`); } } diff --git a/src/pages/login/index.tsx b/src/pages/login/index.tsx index e90bf53..deb523b 100644 --- a/src/pages/login/index.tsx +++ b/src/pages/login/index.tsx @@ -85,7 +85,7 @@ const Login: React.FC = () => {