利用webpack的构建完成(hook.done)钩子,发个钉钉提醒到钉钉群,(包括打包执行人,打包文件大小等信息),于是写了一个webpack插件
在webpack的plugin配置里添加插件
new DingtalkMsgPlugin({
webhook:'https://oapi.dingtalk.com/robot/send?access_token=xxxx',
secret: 'xxxxx'
})
插件具体内容如下
const path = require('path')
const Server = require('axios')
const crypto = require('crypto')
const fs = require("fs")
const userName = require('os').userInfo().username
const sendMsg = async (url, secret, Msg) => {
const timestamp = new Date().getTime()
const stringToSign = timestamp + "\n" + secret
const sign = crypto.createHmac('sha256', secret).update(stringToSign).digest("base64")
const signUrlencode = encodeURIComponent(sign)
const { data } = await Server({
url: url + `×tamp=${timestamp}&sign=${signUrlencode}`,
method: "POST",
data: {
"msgtype": "text",
"text": {
"content": Msg
}
}
})
return data
}
const getdirsize = (dir, callback) => {
var size = 0
fs.stat(dir, function (err, stats) {
if (err) return callback(err)
if (stats.isFile()) return callback(null, stats.size)
fs.readdir(dir, function (err, files) {
if (err) return callback(err)
if (files.length == 0) return callback(null, 0)
var count = files.length
for (var i = 0; i < files.length; i++) {
getdirsize(path.join(dir, files[i]), function (err, _size) {
if (err) return callback(err)
size += _size
if (--count <= 0) {
callback(null, size)
}
})
}
})
})
}
class DingtalkMsgPlugin {
constructor (options) {
this.options = options
}
apply (compiler) {
compiler.hooks.done.tap('DingtalkMsgPlugin', ({ compilation }) => {
getdirsize(compilation.options.output.path, async (err, size) => {
if (err) {
console.log(err)
return
}
const kbSize = parseInt(size / 1024, 10)
const url = this.options.webhook
const secret = this.options.secret
try {
const Msg = `构建大小: ${kbSize}kb\n用户信息: ${userName}\n构建时间: ${new Date().getHours()} : ${new Date().getMinutes()}\n构建环境: ${process.env.CODE_ENV || process.env.NODE_ENV}`
const res = await sendMsg(url, secret, Msg)
console.log(res)
} catch (error) {
console.log(error)
}
})
})
}
}
module.exports = DingtalkMsgPlugin
参考node读取文件
https://www.csdn.net/gather_2...
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。