diff --git a/.gitignore b/.gitignore index 8c7d99d..22a5efb 100644 --- a/.gitignore +++ b/.gitignore @@ -39,3 +39,5 @@ components.d.ts # 组件汇总文件,仅限开发本地浏览 component.md + +deploy.js diff --git a/deploy.js b/deploy.js new file mode 100644 index 0000000..635d1dc --- /dev/null +++ b/deploy.js @@ -0,0 +1,75 @@ +import { Client } from 'ssh2'; +import fs from 'fs'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); + +const serverConfig = { + host: '222.20.126.217', + port: 22, + username: 'admini', + password: 'zlzxb@1888', + remotePath: '/home/admini/ossrpp/base/nginx/dist/SAP/' +}; + +async function uploadDirectory(sftp, localDir, remoteDir) { + const items = fs.readdirSync(localDir); + + // 确保远程目录存在 + try { + await new Promise((res) => sftp.mkdir(remoteDir, { mode: '0755' }, () => res())); + } catch (e) {} + + for (const item of items) { + const localPath = path.join(localDir, item); + const remotePath = path.join(remoteDir, item).replace(/\\/g, '/'); // 转换为Linux路径 + + if (fs.statSync(localPath).isDirectory()) { + await uploadDirectory(sftp, localPath, remotePath); + } else { + await new Promise((resolve, reject) => { + console.log(`🚀 上传中: ${item}`); + sftp.fastPut(localPath, remotePath, (err) => { + if (err) reject(err); + else resolve(); + }); + }); + } + } +} + +async function startDeploy() { + const conn = new Client(); + console.log('✨ 开始部署流程...'); + + conn.on('ready', () => { + console.log('✅ SSH 连接成功!'); + conn.sftp(async (err, sftp) => { + if (err) throw err; + + try { + const localDist = path.join(__dirname, 'dist'); + await uploadDirectory(sftp, localDist, serverConfig.remotePath); + console.log('🎉 所有文件上传成功!'); + conn.end(); + } catch (uploadErr) { + console.error('❌ 上传失败:', uploadErr); + conn.end(); + } + }); + }).on('error', (err) => { + console.error('❌ 连接出错 (检查IP或密码):', err.message); + }).on('close', () => { + console.log('🔌 连接已关闭'); + process.exit(0); + }).connect({ + host: serverConfig.host, + port: serverConfig.port, + username: serverConfig.username, + password: serverConfig.password, + readyTimeout: 10000 // 10秒超时 + }); +} + +startDeploy(); diff --git a/package.json b/package.json index a4994dc..2525619 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,8 @@ "preview": "vite preview", "type-check": "vue-tsc --noEmit -p tsconfig.app.json --composite false", "lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore", - "format": "prettier --write src/" + "format": "prettier --write src/", + "deploy": "node deploy.js" }, "dependencies": { "@devui-design/icons": "^1.4.0",