gulpfile.js 2.84 KB
const { series, src, dest, } = require('gulp')
const babel = require('gulp-babel')
const removeUseStrict = require('gulp-remove-use-strict')
const exec = require('gulp-exec')
const del = require('del')
const path = require('path')
const { readFileSync, writeFileSync } = require('fs')

let oldTimespan = '?v=1659335275728'

/**
 * @descritpion 清除dist 文件夹
 * @param {*} cb 
 */
async function clean(cb) {
  await del(['dist'])
  await cb()
}

/**
 * @description 编译文件
 * @param {*} cb 
 */
function complieFormat(cb) {
  src('./src/main/webapp/js/grapheditor/Format.js')
    .pipe(babel({ presets: ['@babel/env'] }))
    .pipe(removeUseStrict())
    .pipe(dest('./src/main/webapp/js/grapheditor'))

  cb()
}

/**
 * @description 复制文件
 * @param {*} cb 
 */
function copyFile(cb) {
  src('./src/main/webapp/js/grapheditor/Format.js')
    .pipe(dest('./dist/'))

  cb()
}

/**
 * @description 构建war包
 * @param {*} cb 
 */
async function buildWar(cb) {
  await new Promise((resolve) => {
    src(path.resolve(__dirname, './'))
      .pipe(exec('cd etc/build && ant war', function (err) {
        resolve()
      }))
      .on('error', (error) => {
        console.log('on build war package happend error', error.message)
      })
  })
}

/**
 * @description 还原文件
 * @param {*} cb 
 */
function reductionFile(cb) {
  src(path.resolve(__dirname, './dist/Format.js'))
    .pipe(dest('./src/main/webapp/js/grapheditor'))
  cb()
}

function generatoreVersion(cb) {

  const reg = /const\s+releaseVersion\s?=\s?.*/g
  const regTimespan = /\?v=(\d+)/g
  const timespan = Date.now()
  const string = readFileSync(path.resolve(__dirname, './src/main/webapp/index.html'), { encoding: 'utf-8' })

  let newString = string.replace(reg, `const releaseVersion = '${encodeURIComponent(timespan)}'`)

  newString = newString.replace(regTimespan, `?v=${timespan}`)

  writeFileSync(path.resolve(__dirname, './src/main/webapp/index.html'), newString, { encoding: 'utf-8' })

  cb()
}

function copyFileUsageOssServer(cb) {

  const copyFileList = [
    './src/main/webapp/js/app.min.js',
    './src/main/webapp/js/shapes-14-6-5.min.js',
    './src/main/webapp/js/stencils.min.js',
    './src/main/webapp/js/extensions.min.js',
    './src/main/webapp/js/plugin/echarts/echarts.js',
    './src/main/webapp/js/plugin/crypto-js/crypto-js.js',
    './src/main/webapp/js/plugin/ace/ace.js',
    './src/main/webapp/js/plugin/ace/mode-json.js',
    './src/main/webapp/js/plugin/ace/worker-json.js',
    './src/main/webapp/js/plugin/video/video-js.min.css',
    './src/main/webapp/js/plugin/video/video.min.js',
  ]

  const outPath = './build/oss'

  for (const filePath of copyFileList) {
    src(path.resolve(__dirname, filePath))
      .pipe((dest(outPath)))
  }

  cb()
}

exports.default = series(copyFile, complieFormat, generatoreVersion, buildWar, copyFileUsageOssServer, reductionFile, clean)