102 lines
3.3 KiB
JavaScript
102 lines
3.3 KiB
JavaScript
|
|
const path = require('path')
|
|||
|
|
const fs = require('fs')
|
|||
|
|
const axios = require('axios')
|
|||
|
|
const crypto = require('crypto')
|
|||
|
|
const readline = require('readline')
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 生成字典插件 v1.0
|
|||
|
|
* youfool
|
|||
|
|
*/
|
|||
|
|
class constantFile {
|
|||
|
|
constructor(options) {
|
|||
|
|
this.options = options
|
|||
|
|
}
|
|||
|
|
apply(compiler) {
|
|||
|
|
compiler.hooks.environment.tap('constantFile', async (compilation, callback) => {
|
|||
|
|
// 只在开发环境
|
|||
|
|
if (process.env.ENV === 'development') {
|
|||
|
|
const projectPath = path.join(__dirname, '../../')
|
|||
|
|
const constantPath = projectPath + '/src/constant'
|
|||
|
|
fs.mkdir(constantPath, function (error) {
|
|||
|
|
if (error) {
|
|||
|
|
return false
|
|||
|
|
}
|
|||
|
|
// 获取字典参数
|
|||
|
|
axios.get(this.options.api).then((res) => {
|
|||
|
|
const { code, data } = res.data
|
|||
|
|
if (code === 0) {
|
|||
|
|
for (const module of data) {
|
|||
|
|
// 模块
|
|||
|
|
const moduleName = Object.keys(module)[0]
|
|||
|
|
const filePath = constantPath + '/' + moduleName + 'Constant.js'
|
|||
|
|
let fileStr = templateStr(module[moduleName])
|
|||
|
|
const hash = crypto.createHash('md5')
|
|||
|
|
hash.update(fileStr)
|
|||
|
|
const md5 = hash.digest('hex')
|
|||
|
|
// 判断之前的文件是否存在
|
|||
|
|
if (fs.existsSync(filePath)) {
|
|||
|
|
const oldFile = readline.createInterface({
|
|||
|
|
input: fs.createReadStream(filePath)
|
|||
|
|
})
|
|||
|
|
let oldMd5
|
|||
|
|
oldFile.on('line', function (line) { // 事件监听
|
|||
|
|
if (!oldMd5) {
|
|||
|
|
// 解析旧文件的字符md5
|
|||
|
|
oldMd5 = line.replace('// ', '')
|
|||
|
|
if (oldMd5 === md5) {
|
|||
|
|
oldFile.close()
|
|||
|
|
} else {
|
|||
|
|
// 不相同则重新生成常量文件
|
|||
|
|
fileStr = `// ${md5}\n` + fileStr
|
|||
|
|
fs.writeFileSync(filePath, fileStr, 'utf8')
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
})
|
|||
|
|
} else {
|
|||
|
|
// 不相同则重新生成常量文件
|
|||
|
|
fileStr = `// ${md5}\n` + fileStr
|
|||
|
|
fs.writeFileSync(filePath, fileStr, 'utf8')
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}).catch((e) => { })
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 生成文件字符
|
|||
|
|
*/
|
|||
|
|
function templateStr(dictTypes) {
|
|||
|
|
const moduleName = dictTypes[0].module
|
|||
|
|
const moduleDescription = dictTypes[0].moduleDescription
|
|||
|
|
// 开头
|
|||
|
|
const head = `/**
|
|||
|
|
* ${moduleName}${moduleDescription || ''}
|
|||
|
|
* 注:本文件不能手动修改,需要通过代码生成器生成
|
|||
|
|
*/\n`
|
|||
|
|
// 底部
|
|||
|
|
let bottom = 'export default {'
|
|||
|
|
// 遍历类型
|
|||
|
|
let content = ''
|
|||
|
|
for (const dictType of dictTypes) {
|
|||
|
|
content += `/**
|
|||
|
|
*${dictType.name}
|
|||
|
|
*${dictType.typeDescription || ''}
|
|||
|
|
*/`
|
|||
|
|
for (const dictCode of dictType.dictCodes) {
|
|||
|
|
const attribute = `${dictType.type.toUpperCase()}_${dictCode.constant.toUpperCase()}`
|
|||
|
|
content += `\nconst ${attribute} = '${dictCode.code}'// ${dictCode.label}${dictCode.description || ''}`
|
|||
|
|
bottom += ` ${attribute},`
|
|||
|
|
}
|
|||
|
|
content += `\n`
|
|||
|
|
}
|
|||
|
|
// 去除最后的逗号
|
|||
|
|
bottom = bottom.substr(0, bottom.length - 1) + ` }`
|
|||
|
|
return head + '\n' + content + '\n' + bottom + '\n'
|
|||
|
|
}
|
|||
|
|
module.exports = constantFile
|