首页 > 其他分享 >2022.10.18 - 前端Vue项目部署文件上线

2022.10.18 - 前端Vue项目部署文件上线

时间:2022-10-18 23:25:34浏览次数:74  
标签:Vue const log 18 await publicPath 2022.10 path config

在terminal终端连接Linux服务器

mac通过scp特定端口上传文件到linux服务器

命令:

scp -r -P 10017 /Users/yehudalee/Desktop/多测/portal-ui/dist/* [email protected]:/home/dchy/apache-tomcat-8.5.82/webapps/dchy-ui

命令说明:

scp -r -P 【端口号】【文件目录在本地的地址】【/*】(可选,意为表示当前目录下的所有文件) 【用户名】@【Linux服务器ip地址】:【服务器上传的目标目录路径地址】

注意:确认之后通过输入服务器密码就可以上传了!

在终端连接Linux服务器进行操作

命令:

ssh -p 10017 [email protected]

命令说明:

ssh -p 【端口号】 【用户名】@【服务器地址】

常用Linux命令:

pwd —— 显示当前目录

cd —— 改变目录

ls —— 列出目录内容

dir和vdir —— 列出目录内容

Vue项目自动部署上线脚本

目录层级

PORTAL-UI
├─build
├─config
├─dist ++[读取项目打包文件]
├─node_modules
├─src
├─.gitignore
├─deploy.js ++ [部署文件脚本]
├─deployConfig.js ++ [部署文件配置脚本]
├─package-lock.json
├─package.json
└README.md

deploy.js

const path = require('path');
const fs = require('fs');
const chalk = require('chalk');
const ora = require('ora');
const NodeSSH = require('node-ssh').NodeSSH;
const shell = require('shelljs');
const compressing = require('compressing');
const CONFIG = require('./deployConfig');

const SSH = new NodeSSH();
let config = CONFIG.servers;

const needBuild = process.argv[2] === '-b';

//用于显示日志的几种方式
const defaultLog = (log) =>
  console.log(chalk.blue(`---------------- ${log} ----------------`));
const errorLog = (log) =>
  console.log(chalk.red(`---------------- ${log} ----------------`));
const successLog = (log) =>
  console.log(chalk.green(`---------------- ${log} ----------------`));

//编译项目
const compileDist = async () => {
  const loading = ora(defaultLog('开始打包!')).start();
  shell.cd(path.resolve(__dirname, './'));
  const res = await shell.exec(`npm run ${config[0].script}`); //执行shell 打包命令
  console.log(res);
  loading.clear();
  if (res.code === 0) {
    successLog('项目打包成功!');
  } else {
    errorLog('项目打包失败, 请重试!');
    process.exit(); //退出流程
  }
};

const zipDist = async () => {
  defaultLog('项目开始压缩');
  try {
    const distDir = path.resolve(__dirname, `./${config[0].publicPath}`);
    const distZipPath = path.resolve(
      __dirname,
      `./${config[0].publicPath}.zip`
    );
    console.log(distZipPath);
    await compressing.zip.compressDir(distDir, distZipPath);
    successLog('压缩成功!');
  } catch (error) {
    errorLog(error);
    errorLog('压缩失败, 退出程序!');
    process.exit(); //退出流程
  }
};

//创建ssh连接
const connectSSH = async () => {
  const loading = ora(defaultLog('正在连接服务器')).start();
  try {
    const conn = await SSH.connect({
      host: config[0].host,
      username: config[0].username,
      password: config[0].password,
      port: config[0].port || 22
    });
    successLog('SSH连接成功!');
    loading.clear();
    return conn;
  } catch (error) {
    errorLog(error);
    errorLog('SSH连接失败!');
    process.exit(); //退出流程
  }
};

// console.log(SSH.isConnected());
const distZipPath = path.resolve(__dirname, `./${config[0].publicPath}.zip`);
console.log(distZipPath);

const runCommand = async (command) => {
  await SSH.exec(command, [], { cwd: config[0].path });
};

const clearOldFile = async () => {
  //删除旧的文件夹
  await runCommand(`rm -rf ${config[0].serverProjectPath}`);
};

const uploadZipBySSH = async () => {
  const loading = ora(defaultLog('准备上传文件')).start();
  try {
    const distZipPath = path.resolve(
      __dirname,
      `./${config[0].publicPath}.zip`
    );
    await SSH.putFiles([
      {
        local: distZipPath,
        remote: config[0].path + `/${config[0].publicPath}.zip`
      }
    ]); //local 本地 ; remote 服务器 ;
    successLog('上传成功!');
    loading.text = '正在解压文件';
    await runCommand(`unzip ./${config[0].publicPath}.zip`); //解压
    if (config[0].publicPath !== config[0].serverProjectPath) {
      await runCommand(
        `mv ${config[0].publicPath} ${config[0].serverProjectPath}`
      );
    }
    //解压
    await runCommand(`rm -rf ./${config[0].publicPath}.zip `);

    successLog('解压成功!');
    SSH.dispose(); //断开连接
    loading.stop();
  } catch (error) {
    errorLog(error);
    errorLog('上传失败!');
    process.exit(); //退出流程
  }
  loading.clear();
};

const clearZipDist = async () => {
  const distZipPath = path.resolve(__dirname, `./${config[0].publicPath}.zip`);
  fs.unlink(distZipPath, () => {});
};

const runUploadTask = async () => {
  const conn = connectSSH();
  conn.then(async () => {
    //打包
    if (needBuild) {
      await compileDist();
    }
    //压缩
    await zipDist();
    //将打包上传的dist文件改名为uums-ui,并删除之前的uums-ui的文件。
    await clearOldFile();

    //连接服务器上传文件
    await uploadZipBySSH();

    //删除本地的打包zip
    await clearZipDist();
    successLog('部署成功!');
    process.exit();
  });
};
runUploadTask();

deployConfig

module.exports = Object.freeze({
  servers: [
    {
      publicPath: 'dist', // 项目打包之后的文件夹名称,一般都是dist文件夹,如果你的项目打包成别的文件夹名称,填写打包之后文件夹名称即可
      name: '测试环境', // 部署环境的名称
      username: 'root', // 部署服务器的账号
      host: 'proxy.esconsoft.com', //服务器ip
      port: '10017', //端口
      password: 'iIl1o0O',
      path: '/home/dchy/apache-tomcat-8.5.82/webapps',
      script: 'build', //打包命令
      serverProjectPath: 'dchy-ui' //服务器上的项目名称地址
    }
  ]
});

// scp -r -P 10017 /Users/yehudalee/Desktop/多测/portal-ui/dist [email protected]:/home/dchy/apache-tomcat-8.5.82/webapps/dchy-ui

Package.json

"scripts": {
    "dev": "node build/dev-server.js",
    "start": "node build/dev-server.js",
    "build": "node build/build.js",
    "publish": "npm run build && node ./deploy.js" // 先打包之后再运行脚本
  },

参考资料

阮一峰:【npm scripts 使用指南

标签:Vue,const,log,18,await,publicPath,2022.10,path,config
From: https://www.cnblogs.com/yehuda/p/16804577.html

相关文章

  • Vue 插件:VueRouter
    VueRouter是一个Vue插件,用于实现SPA(singlepagewebapplication)应用。SPA(singlepagewebapplication)应用,即单页面应用。整个应用只有一个.html文件,通常命名为......
  • 2022.10.18-代码大全-10月读后感1
    近期,我阅读了这本书的什么是软件构造这一部分。我了解到了开发计算机软件已是一个复杂的过程。已经认识到在软件开发中的各种不同的活动:定义问题、需求分析、规划构建、软......
  • vue 双向数据绑定
    vue.js是采用数据劫持结合发布者-订阅者模式的方式,通过Object.defineProperty()来劫持各个属性的setter,getter,在数据变动时发布消息给订阅者,触发相应的监听回调。具体步骤......
  • 10.18小测(流量人数统计)
    题目要求:给出result.txt文件,导入到mysql中,清洗日期格式,统计视频流量,可视化展示。需要数据分析的内容:(1)统计最受欢迎的视频/文章的Top10访问次数(video/article)(2)按照地市......
  • P4588 [TJOI2018]数学计算
    线段树板子题。#include<iostream>#include<cstring>usingnamespacestd;#defineintlonglongconstintN=8e5+1;intmod;intq,m;namespacest{inttree[......
  • 10.18
    [COCI2021-2022#4]Autobus题目描述在一个国家里有\(n\)座城市。这些城市由\(m\)条公交线路连接,其中第\(i\)条线路从城市\(a_i\)出发,到\(b_i\)停止,路程中耗时......
  • Vuex状态管理-mapState的基本用法详细介绍
    Vuex状态管理-mapState的基本用法详细介绍:https://blog.csdn.net/chenjie9230/article/details/108883055?spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.......
  • C语言学习2--10/18
    常量: 不会变化的数据1.“hello”字符串常量,‘A’字符常量,-10整型常量,3.14浮点常量2.#definePI3.14,宏定义,推荐3.consta=10 const关键字,被该关键字......
  • 2022.10.18 CSP2022 模拟赛五
    旅行路线Source:CF459E。憨憨题。按\(w\)排序后,考虑DP,设\(f_u\)表示目前在点\(u\),可以走出的最长路线。按阶段转移的时候稍微注意一下相同边权的处理,具体的,开一个......
  • 归档 221018 | 做题记录
    K.Differencehttps://loj.ac/p/2161好耶我会打\(n^3\)!这说明这道题\(n\)一定等于\(10^3\)!我超,是\(10^6\)????寄,,,,,枚举出现次数最多的字符(假设为\(x\))和出现次数最少......