gulpfile.js 1.98 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')

/**
 * @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/Format_before_compile.js'))

  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()
      }))
  })
}

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

function generatoreVersion(cb) {
  const reg = /const\s+releaseVersion\s?=\s?.*/g
  const string = readFileSync(path.resolve(__dirname, './src/main/webapp/index.html'), { encoding: 'utf-8' })
  // const oldVersion = Number(reg.exec(string)[0].split('=')[1])
  // const newVersion = oldVersion + 1
  const newString = string.replace(reg, `const releaseVersion = '${encodeURIComponent(Date.now())}'`)
  writeFileSync(path.resolve(__dirname, './src/main/webapp/index.html'), newString, { encoding: 'utf-8' })
  cb()
}

// exports.default = series(generatoreVersion)

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