zoukankan      html  css  js  c++  java
  • 打包文件复制脚本

    ||版本||更细时间||更新内容||
    || v1.0|| 2020.7.27||上传脚本 ||
    || v1.1|| 2020.7.30||修复build.js报错问题 ||
    || v1.2|| 2020.7.31||修复development环境下执行复制指令的问题 ||

    出于安全考虑,目前代码仓库和部署仓库是分开的。部署时需要手动将打包的文件复制到部署文件夹,可添加打包文件复制脚本减少操作:

    • 插件方式:运行打包指令即可
    • 添加指令方式:需运行打包指令,且等待打包完成,运行打包复制文件指令
    1. 以用户中心代码为例:package.json 添加指令:
    "scripts": {
    	"copy:staging": "node build.js staging user-center",
    	"copy:pro": "node build.js pro user-center"
    }
    

    或安装webpack-hooks-shellscripts插件并修改vue.config.js配置:

    -----------------------------------------------------------------------------------------
    npm install webpack-hooks-shellscripts --save-dev
    -----------------------------------------------------------------------------------------
    plugins: [
          // Ignore all locale files of moment.js
          new webpack.IgnorePlugin(/^./locale$/, /moment$/),
          hooksScriptPlugin({
       	    afterEmit: process.env.VUE_APP_OUTPUT_DIR ? [
                `node build.js ${process.env.VUE_APP_OUTPUT_DIR} user-center`
              ] : []
        })
     ]
    -----------------------------------------------------------------------------------------
    
    
    
    1. 新建与package.json同级文件build.js,代码如下:
    const fs = require('fs')
    
    /**
     * 拷贝文件(源文件和目标文件在同一级目录)
     * @param {*} src 源文件地址
     * @param {*} dist 目标文件地址
     * @param {*} firstDir 是否一级目录
     */
    function copyDir (src, dist, firstDir = true) {
      fs.access(dist, function (err) {
        if (err) {
          // 目录不存在时创建目录
          fs.mkdirSync(dist)
        }
        _copy(null, src, dist)
      })
    
      function _copy (err, src, dist) {
        if (err) {
          throw new Error(err)
        } else {
          fs.readdir(src, function (err, paths) {
            if (err) {     
              throw new Error(err)
            } else {
              let lastestFile = ''
              let lastestDate = ''
              // 找最新创建的文件夹
              paths.forEach(function (path) {
                const _src = src + '/' + path
                var stat = fs.statSync(_src)
                const createTime = new Date(stat.ctime).getTime()
                if (!stat.isFile() && firstDir) {
                  if (createTime > lastestDate) {
                    lastestDate = createTime
                    lastestFile = path
                  }
                }
              })
              // 复制文件
              paths.forEach(function (path) {
                if (firstDir && path !== lastestFile) {
                  return
                }
                const _src = src + '/' + path
                const _dist = dist + '/' + (firstDir && path === lastestFile ? '' : path)
                if (firstDir) {
                  console.log('======================== copy file ========================')
                  console.log('soure fileName: ', _src)
                  console.log('target fileName: ', _dist)
                  console.log('======================== copy file ========================')
                }
                fs.stat(_src, function (err, stat) {
                  if (err) {        
    		          throw new Error(err)
                  } else {
                    // 判断是文件还是目录
                    if (stat.isFile()) {
                      fs.writeFileSync(_dist, fs.readFileSync(_src))
                    } else if (stat.isDirectory()) {
                      // 当是目录是,递归复制
                      copyDir(_src, _dist, false)
                    }
                  }
                })
              })
            }
          })
        }
      }
    }
    
    const env = process.argv[2]
    const dir = `../${process.argv[3]}`
    if (!env || !dir) {
      throw new Error('请添加配置参数')
    }
    copyDir(`./dist/${env}`, `../${dir}`)
    

    若安装插件最后一行代码改为

    copyDir(`./dist/${env}`, `./${dir}`)
    
    1. 打包完后,执行指令(若安装插件,直接打包即可)
    npm run copy:staging
    npm run copy:pro
    
  • 相关阅读:
    OSG-提示“error reading file e:1.jpg file not handled”
    OSG-加载地球文件报0x00000005错误,提示error reading file simple.earth file not handled
    QT-找开工程后,最上方提示the code model could not parse an included file, which might lead to incorrect code completion and highlighting, for example.
    我的书《Unity3D动作游戏开发实战》出版了
    java中无符号类型的第三方库jOOU
    Windows批处理备份mysql数据
    使用 DevTools 时,通用Mapper经常会出现 class x.x.A cannot be cast to x.x.A
    Java版本,Java版本MongoDB驱动,驱动与MongoDB数据库,Spring之间的兼容性
    Jrebel本地激活方法
    wget下载指定网站目录下的所有内容
  • 原文地址:https://www.cnblogs.com/dhjy123/p/15475395.html
Copyright © 2011-2022 走看看