ZaiZai 6 сар өмнө
parent
commit
10aeecf29c

+ 49 - 4
README.md

@@ -66,11 +66,56 @@ js-fast-way 文档
 
 ### 打包说明
 
-`build:zip` 为一键打包为zip的方式
-`build:zip:wgt` 为一键打包为zip的方式(不包含 `/public/plugins` 文件夹)
+`build:all` 为一键打包为zip的方式,会打包出来两个文件,一个是常规的,一个是本地模式,本地模式通常用于第三方
+`build:wgt` 为一键打包为zip的方式(不包含 `/public/plugins` 文件夹)
+
+`server:xxx:all` 一键上传到服务器上并部署,其中,xxx,代表名字,对应 `/scripts/server.js` 里的配置,请保证已经编译打包过,并且zip文件存在,否则会出现异常
+`server:xxx:wgt` 一键上传到服务器上并部署,跟上面一样,这个是不包含 `/public/plugins` 文件夹
+
+如果要一键上传到服务器并解压,需要安装插件
+
+MacOS上,需要安装 `sshpass`
+
+```shell
+#因特尔芯片
+
+brew install hudochenkov/sshpass/sshpass
+
+#m系列芯片
+
+arch -arm64 brew install hudochenkov/sshpass/sshpass
+```
+
+Windows上,需要安装 [PuTTY](https://www.putty.org/), 并将其添加到系统 PATH 中
+
+1.下载 PuTTY:
+- 访问 PuTTY 官方网站: https://www.putty.org/
+- 点击 "Download PuTTY" 按钮
+- 选择适合你系统的安装包 (通常是 64-bit x86)
+
+2.安装 PuTTY:
+- 运行下载的安装文件
+- 按照安装向导的提示进行安装
+- 建议选择 "Full installation" 选项,确保安装所有组件
+
+3.将 PuTTY 添加到系统 PATH:
+- 右键点击 "此电脑" 或 "我的电脑",选择 "属性"
+- 点击 "高级系统设置"
+- 在 "系统属性" 窗口中,点击 "环境变量" 按钮
+- 在 "系统变量" 部分,找到并选中 "Path" 变量,然后点击 "编辑"
+- 点击 "新建",添加 PuTTY 的安装路径 (通常是 C:\Program Files\PuTTY)
+- 点击 "确定" 保存所有更改
+
+4.验证安装:
+- 打开一个新的命令提示符窗口
+- 输入 putty -version 并按回车
+- 如果显示 PuTTY 的版本信息,说明安装成功并且已经添加到 PATH 中
+
+5.确认 pscp 和 plink 可用:
+- 在命令提示符中输入 pscp 并按回车
+- 再输入 plink 并按回车
+- 如果两个命令都能显示使用说明,说明它们已经可以使用了
 
-`build:test` 为一键打包,然后自动上传到测试服务器上并自动部署的方式
-`build:test:wgt` 为一键打包,然后自动上传到测试服务器上并自动部署的方式(不包含 `/public/plugins` 文件夹)
 
 ### 其它说明
 

+ 7 - 4
package.json

@@ -2,13 +2,16 @@
     "name": "client",
     "private": true,
     "version": "3.5.0",
+    "buildName": "client",
     "scripts": {
         "dev": "vite",
         "build": "vite build",
-        "build:zip": "sh ./scripts/build.sh all",
-        "build:zip:wgt": "sh ./scripts/build.sh wgt",
-        "build:test": "sh ./scripts/build.sh all test",
-        "build:test:wgt": "sh ./scripts/build.sh wgt test",
+        "build:all": "node ./scripts/build.js all",
+        "build:wgt": "node ./scripts/build.js wgt",
+        "server:all:all": "node ./scripts/server.js all all",
+        "server:all:wgt": "node ./scripts/server.js all wgt",
+        "server:test:all": "node ./scripts/server.js test all",
+        "server:test:wgt": "node ./scripts/server.js test wgt",
         "lint": "eslint --ext .js,.vue src",
         "lint:fix": "eslint . --fix"
     },

+ 1 - 1
public/version.json

@@ -1,3 +1,3 @@
 {
-  "value": "20240826172150"
+  "value": "20240827155010"
 }

+ 20 - 70
scripts/build.js

@@ -1,76 +1,26 @@
-const path = require('path');
-const fs = require('fs');
+const { execSync} = require('child_process');
+const config = require('../package.json');
+
+const args = process.argv[2]
+
+// 执行命令并打印输出
+function runCommand(name, local) {
+    try {
+        execSync(`node ./scripts/build/nodeBuild.js ${args} ${name}-${args}.zip ${local?'local':''}`, { stdio: 'inherit' });
+    } catch (error) {
+        console.error(`命令执行失败: ${error.message}`);
+        process.exit(1);
+    }
+}
 
-// 获取当前命令行上下文路径
-const currentDirectory = process.cwd();
 console.log(`----------------------------`)
-console.log(`正在处理编译打包前的准备...`)
+console.log(`准备开始打包: ${config.buildName}`);
 
-//修改版本更新信息
-console.log(`更新版本更新信息...`)
-const versionPath = path.join(currentDirectory, '/public/version.json');
-const versionContent = fs.readFileSync(versionPath, 'utf8');
-//修改配置文件
-const versionJson = JSON.parse(versionContent);
-versionJson.value = dateFormat(new Date());         //版本号
+//打包官方版本
+runCommand(config.buildName, false);
 
-//更新版本更新信息
-fs.writeFileSync(versionPath, JSON.stringify(versionJson, null, 2));
+//打包本地模式版本
+runCommand(config.buildName + '-local', true);
 
 console.log(`----------------------------`)
-
-//删除上次打包相关的文件
-console.log(`正在删除上次打包相关的文件...`)
-const distZipPath = path.join(currentDirectory, '/zip/client.zip');
-if(fs.existsSync(distZipPath)) {
-    fs.unlinkSync(distZipPath);
-}
-
-// 获取配置文件
-console.log(`获取当前的配置文件...`)
-const indexJsonPath = path.join(currentDirectory, 'src/config/index.json');
-const indexJsonContent = fs.readFileSync(indexJsonPath, 'utf8');
-
-// 检测上次打包异常中断的缓存文件是否存在
-console.log(`检测上次打包异常中断的缓存文件是否存在...`)
-const cacheFilePath = path.join(currentDirectory, 'scripts/cache.json');
-if(!fs.existsSync(cacheFilePath)) {
-    //创建缓存文件
-    console.log(`创建配置缓存文件...`)
-    const cacheJsonPath = path.join(currentDirectory, 'scripts/cache.json');
-    fs.writeFileSync(cacheJsonPath, indexJsonContent, 'utf8');
-}
-
-//修改配置文件
-const indexJson = JSON.parse(indexJsonContent);
-indexJson.version = dateFormat(new Date());         //版本号
-indexJson.target = "http://127.0.0.1:8090";         //接口地址
-indexJson.smsPhone = "";                            //短信接口手机号
-indexJson.vite = {};                                //vite配置
-
-//更新配置文件
-console.log(`修改配置文件为生产环境的配置...`)
-fs.writeFileSync(indexJsonPath, JSON.stringify(indexJson, null, 2));
-
-//时间格式化
-function dateFormat(date) {
-    let format = 'yyyyMMddhhmmss';
-    let o = {
-        "M+": date.getMonth() + 1, //month
-        "d+": date.getDate(), //day
-        "h+": date.getHours(), //hour
-        "m+": date.getMinutes(), //minute
-        "s+": date.getSeconds(), //second
-        "q+": Math.floor((date.getMonth() + 3) / 3), //quarter
-        "S": date.getMilliseconds() //millisecond
-    }
-    if (/(y+)/.test(format)) {
-        format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
-    }
-    for (let k in o) {
-        if (new RegExp("(" + k + ")").test(format)) {
-            format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
-        }
-    }
-    return format;
-}
+console.log('全部编译打包完成')

+ 0 - 104
scripts/build.sh

@@ -1,104 +0,0 @@
-#!/bin/bash
-
-#本地压缩文件名
-file_name="client.zip"
-
-#测试服器上的目录地址
-file_path="user.hcxxy.com"
-
-#测试服务器上的演示地址
-demo_url="http://192.168.0.109:5173/"
-
-#测试服务器上的登录密码
-passwd="admin123@"
-
-# 打包前的准备
-node ./scripts/build.js
-# 执行打包命令
-vite build
-# 恢复配置文件
-node ./scripts/restore.js
-# 排除打包文件
-if [ $1 == "wgt" ]; then
-    node ./scripts/public.js
-fi
-# 执行打包为zip
-node ./scripts/zip.js
-
-current_time=$(date "+%Y%m%d%H%M%S")
-
- # 上传到测试服务器
-function testServer() {
-    expect -c "
-        spawn scp ./zip/${file_name} root@192.168.0.109:/www/wwwroot/${file_path}
-        expect {
-            \"yes/no\" {send \"yes\r\";exp_continue;}
-            \"*password\" {set timeout 500;send \"${passwd}\r\";}
-        }
-    expect eof"
-
-#服务器上的相关操作
-/usr/bin/expect << EOF
-    set time 30
-    spawn ssh root@192.168.0.109
-    expect {
-        "*yes/no" { send "yes\r"; exp_continue }
-        "*password:" { send "${passwd}\r" }
-    }
-
-    # 进入当前项目的目录
-    expect "*]#"
-    send "cd /www/wwwroot/${file_path}\r"
-
-    # 删除 static 目录
-    expect "*]#"
-    send "rm -rf static\r"
-
-    # 解压上传的文件压缩包
-    expect "*]#"
-    send "unzip -o ${file_name}\r"
-
-    # 退出
-    send "exit\r"
-    expect eof
-EOF
-    echo "编译打包后自动部署到测试服务器上完成"
-    echo "测试服务器地址:${demo_url}"
-}
-
-# 删除 plugins 等目录
-function delPublic() {
-    echo "准备移除 plugins 等目录"
-#服务器上的相关操作
-/usr/bin/expect << EOF
-    set time 30
-    spawn ssh root@192.168.0.109
-    expect {
-        "*yes/no" { send "yes\r"; exp_continue }
-        "*password:" { send "${passwd}\r" }
-    }
-
-    # 进入当前项目的目录
-    expect "*]#"
-    send "cd /www/wwwroot/${file_path}\r"
-
-    # 删除 plugins 目录
-    expect "*]#"
-    send "rm -rf plugins\r"
-
-    # 退出
-    send "exit\r"
-    expect eof
-EOF
-    echo "plugins 等目录移除完成"
-}
-
- # 上传到测试服务器
-if [ $2 == "test" ]; then
-    if [ $1 == "all" ]; then
-        delPublic
-    fi
-    testServer
-else
-    echo "编译完成"
-fi

+ 37 - 0
scripts/build/nodeBuild.js

@@ -0,0 +1,37 @@
+const { execSync } = require('child_process');
+
+//获取参数
+const args = process.argv[2] //参数
+const buildName = process.argv[3] //打包后的名字
+const local = process.argv[4] //是否本地模式
+
+// 执行命令并打印输出
+function runCommand(command) {
+    console.log(`执行命令: ${command}`);
+    try {
+        execSync(command, { stdio: 'inherit' });
+    } catch (error) {
+        console.error(`命令执行失败: ${error.message}`);
+        process.exit(1);
+    }
+}
+
+// 打包前的准备
+runCommand(`node ./scripts/build/config.js ${buildName} ${local}`);
+
+// 执行打包命令
+runCommand('vite build');
+
+// 恢复配置文件
+runCommand('node ./scripts/build/restore.js');
+
+// 排除打包文件
+if (args === 'wgt') {
+    runCommand('node ./scripts/build/public.js');
+}
+
+// 执行打包为zip
+runCommand(`node ./scripts/build/zip.js ${buildName}`);
+
+console.log(`----------------------------`)
+console.log(`${buildName} 编译打包完成`)

+ 0 - 0
scripts/public.js → scripts/build/public.js


+ 0 - 0
scripts/restore.js → scripts/build/restore.js


+ 5 - 3
scripts/zip.js → scripts/build/zip.js

@@ -2,6 +2,8 @@ const fs = require('fs');
 const path = require("path");
 const archiver = require('archiver')
 
+const buildName = process.argv[2] //打包后的名字
+
 // 获取当前命令行上下文路径
 const currentDirectory = process.cwd();
 
@@ -22,7 +24,7 @@ if(!fs.existsSync(distJoinPath)) {
 
 // 创建文件输出流
 console.log(`初始化zip文件流...`)
-const distZipPath = path.join(currentDirectory, '/zip/client.zip');
+const distZipPath = path.join(currentDirectory, `/zip/${buildName}`);
 let output = fs.createWriteStream(distZipPath)
 let archive = archiver('zip', {
     zlib: { level: 9 } // 设置压缩级别
@@ -30,7 +32,7 @@ let archive = archiver('zip', {
 
 // 文件输出流结束
 output.on('close', function() {
-    console.log('打包完成,zip文件位于zip文件夹下,/zip/client.zip')
+    console.log(`打包完成,zip文件位于zip文件夹下,/zip/${buildName}`)
 })
 
 // 数据源是否耗尽
@@ -57,9 +59,9 @@ archive.pipe(output)
 
 console.log(`正在将dist文件夹到zip中...`)
 const distPath = path.join(currentDirectory, '/dist/');
+
 //打包dist里面的所有文件和目录
 archive.directory(distPath, false)
 
 //完成归档
 archive.finalize()
-

+ 86 - 0
scripts/server.js

@@ -0,0 +1,86 @@
+const { execSync} = require('child_process');
+const { getObjValue, arrIndex, isNullES } = require('js-fast-way')
+const fs = require('fs');
+
+// 配置
+const config = [
+    {
+        name: 'test',           //测试服
+        ip: '192.168.0.109',    //服务器地址
+        username: 'root',       //登录账号
+        password: 'admin123@',  //登录密码
+        fileName: 'client',     //本地文件名
+        path: '/www/wwwroot/user.hcxxy.com',    //服务器上的文件路径
+        url: 'http://192.168.0.109:5173/',      //demo地址
+    },
+    {
+        name: 'user',           //正式服
+        ip: '39.108.216.210',   //服务器地址
+        username: 'root',       //登录账号
+        password: '%CX0&qPRk2BitWPH^tCa5jxZKPYuST', //登录密码
+        fileName: 'client',     //本地文件名
+        path: '/www/wwwroot/user.hcxxy.com',        //服务器上的文件路径
+        url: 'https://user.hcxxy.com/',             //demo地址
+    },
+];
+
+const argv = process.argv[2]
+const args = process.argv[3]
+
+// 执行命令并打印输出
+function runCommand(command) {
+    console.log(`执行命令: ${command}`);
+    try {
+        execSync(command, { stdio: 'inherit' });
+    } catch (error) {
+        console.error(`命令执行失败: ${error.message}`);
+        process.exit(1);
+    }
+}
+
+//处理数据
+function setConfigData() {
+    if (argv === 'undefined' || isNullES(argv)) {
+        process.exit(1);
+        return
+    }
+
+    console.log(`----------------------------`)
+    console.log(`准备开始上传到服务器...`);
+    if (argv === 'all') {
+        //全部上传
+        for (let i = 0; i < config.length; i++) {
+            const {name, ip, username, password, fileName, path, url} = getObjValue(config[i])
+            const file_name = `${fileName}-${args}.zip`
+            //判断本地文件是否存在
+            if (!fs.existsSync(`./zip/${file_name}`)) {
+                console.error(`文件 ./zip/${file_name} 不存在`);
+                process.exit(1);
+            }
+            console.log(`----------------------------`)
+            console.log(`准备上传到服务器:${name}`);
+            runCommand(`node ./scripts/upload/upload.js ${args} ${ip} ${username} ${password} ${file_name} ${path} ${url}`);
+        }
+    } else {
+        const index = arrIndex(config, 'name', argv)
+        if (index === -1) {
+            console.log('此平台不存在')
+            process.exit(1);
+            return
+        }
+        const {name, ip, username, password, fileName, path, url} = getObjValue(config[index])
+        const file_name = `${fileName}-${args}.zip`
+        //判断本地文件是否存在
+        if (!fs.existsSync(`./zip/${file_name}`)) {
+            console.error(`文件 ./zip/${file_name} 不存在`);
+            process.exit(1);
+        }
+        console.log(`----------------------------`)
+        console.log(`准备上传到服务器:${name}`);
+        runCommand(`node ./scripts/upload/upload.js ${args} ${ip} ${username} ${password} ${file_name} ${path} ${url}`);
+    }
+    console.log(`----------------------------`)
+    console.log(`全部上传并部署完成`);
+}
+
+setConfigData()

+ 105 - 0
scripts/upload/upload.js

@@ -0,0 +1,105 @@
+const { execSync } = require('child_process');
+const { isNullES } = require('js-fast-way')
+
+//获取参数
+const args = process.argv[2]
+const serverIp = process.argv[3]
+const username = process.argv[4]
+const password = process.argv[5]
+const fileName = process.argv[6]
+const filePath = process.argv[7]
+const url = process.argv[8]
+
+// 检查命令是否可用
+function commandExists(command) {
+    try {
+        execSync(`which ${command}`, { stdio: 'ignore' });
+        return true;
+    } catch (e) {
+        return false;
+    }
+}
+
+// 执行命令并打印输出
+function runCommand(command) {
+    console.log(`执行命令: ${command}`);
+    try {
+        if (command.startsWith('sshpass') || command.startsWith('ssh')) {
+            // 为SSH命令设置LC_ALL
+            command = `LC_ALL=C ${command}`;
+        }
+        execSync(command, { stdio: 'inherit' });
+    } catch (error) {
+        console.error(`命令执行失败: ${error.message}`);
+        process.exit(1);
+    }
+}
+
+// 上传到服务器
+function uploadServer() {
+    const sshCommand = `cd ${filePath} && rm -rf static && unzip -o ${fileName}`;
+    if (process.platform === 'win32') {
+        // Windows
+        runCommand(`pscp -pw ${password} ./zip/${fileName} ${username}@${serverIp}:${filePath}`);
+        runCommand(`plink -ssh ${username}@${serverIp} -pw ${password} -batch "${sshCommand}"`);
+    } else {
+        // Mac/Linux
+        runCommand(`sshpass -p "${password}" scp ./zip/${fileName} ${username}@${serverIp}:${filePath}`);
+        runCommand(`sshpass -p "${password}" ssh ${username}@${serverIp} "${sshCommand}"`);
+    }
+    console.log('编译打包后自动部署到服务器上完成');
+    console.log(`服务器上的地址:${url}`);
+}
+
+// 删除 plugins 等目录
+function delPublic() {
+    console.log('准备移除 plugins 等目录');
+    const sshCommand = `cd ${filePath} && rm -rf plugins`;
+    if (process.platform === 'win32') {
+        // Windows
+        runCommand(`plink -ssh ${username}@${serverIp} -pw ${password} -batch "${sshCommand}"`);
+    } else {
+        // Mac/Linux
+        runCommand(`sshpass -p "${password}" ssh ${username}@${serverIp} "${sshCommand}"`);
+    }
+    console.log('plugins 等目录移除完成');
+}
+
+//执行
+function runCode() {
+    //参数是否为空
+    if (args === 'undefined' || serverIp === 'undefined' || username === 'undefined' || password === 'undefined' || fileName === 'undefined' || filePath === 'undefined') {
+        console.log('参数异常,终止执行:', { args,serverIp, username, password, fileName, filePath });
+        process.exit(1);
+        return
+    }
+    if (isNullES(args) || isNullES(serverIp) || isNullES(username) || isNullES(password) || isNullES(fileName) || isNullES(filePath)) {
+        console.log('参数异常,终止执行:', { args,serverIp, username, password, fileName, filePath });
+        process.exit(1);
+        return
+    }
+
+    //判断命令是否支持
+    if (process.platform === 'win32') {
+        if (!commandExists('putty')) {
+            console.log('putty 未安装,请先安装 putty');
+            process.exit(1);
+            return
+        }
+    } else {
+        if (!commandExists('sshpass')) {
+            console.log('sshpass 未安装,请先安装 sshpass');
+            process.exit(1);
+            return
+        }
+    }
+
+    //如果是上传全部,就删除不常动的目录
+    if (args === 'all') {
+        delPublic();
+    }
+    //上传到服务器
+    uploadServer();
+}
+
+runCode()

+ 0 - 1
src/config/index.js

@@ -25,7 +25,6 @@ export default {
     //线上还没有,先关掉,防止控制他一直连接失败
     socketLimit: 10, //连接失败后,最大重连次数
     socketInterval: 10000, //连接失败后,每次重连的间隔时间,单位毫秒。
-    localModel: false, //本地模式,开启后,将关闭https强转、截图等相关功能
     ...config,
     //这里不再支持配置请求地址,请在 src/config/index.json 文件中配置。
     //相关参数,可查阅 src/config/index.md 文件说明

+ 1 - 0
src/config/index.json

@@ -5,6 +5,7 @@
     "target3": "http://192.168.0.125:8090",
     "target4": "http://183.247.216.148:28090",
     "socket": "wss://measure.hczcxx.cn/websocket",
+    "localModel": false,
     "smsPhone": "",
     "vite": {
         "port": 5174,