-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
88 lines (82 loc) · 2.69 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* 获取git信息
* @returns {{commitMessage: *, buildTime: Date, commitAuthor: *, branch: *, hash: *, commitDate}}
*/
function gitVersion() {
try {
const execSync = require('child_process').execSync
const dayjs = require('dayjs')
const vName = execSync('git name-rev --name-only HEAD').toString().trim()
const commitHash = execSync('git show -s --format=%H').toString().trim()
const name = execSync('git show -s --format=%cn').toString().trim()
const date = dayjs(execSync('git show -s --format=%cd').toString()).format('YYYY-MM-DD HH:mm:ss')
const message = execSync('git show -s --format=%s').toString().trim()
const buildUserName = execSync('git config user.name').toString().trim()
const buildUserEmail = execSync('git config user.email').toString().trim()
const gitUrl = execSync('git config --get remote.origin.url').toString().trim()
return {
gitUrl: gitUrl,
branch: vName,
commitHash: commitHash,
commitAuthor: name,
commitTime: date,
commitMessage: message,
buildTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
buildUserName: buildUserName,
buildUserEmail: buildUserEmail
}
} catch (e) {
return {
error: '获取git信息错误',
message: e && e.message || ''
}
}
}
// const fs = require('fs')
class PackInfoWebpackPlugin {
constructor(options = {
excludeKeys: [],
isBase64: false
}) {
if (options && (options.excludeKeys || options.isBase64 !== undefined)) {
this.options = options
} else {
this.options = {
excludeKeys: [],
isBase64: false
}
}
}
apply(compiler) {
// const outPath = compiler.options.output.path
// compiler.hooks.done.tap('BuildVersionPlugin', compilation => {
// const version = gitVersion()
// fs.writeFileSync(outPath + '/gitInfo.json', JSON.stringify(version))
// })
// const version = JSON.stringify(gitVersion())
const version = gitVersion()
const versionResult = {}
if (!this.options.excludeKeys) {
this.options.excludeKeys = []
}
Object.keys(version).filter(key => !this.options.excludeKeys.includes(key)).forEach(k => {
versionResult[k] = version[k]
})
let versionStr = JSON.stringify(versionResult)
if(this.options.isBase64 === true) {
versionStr = Buffer.from(versionStr).toString('base64')
}
compiler.hooks.emit.tapAsync('BuildVersionPlugin', (compilation, cb) => {
compilation.assets['buildInfo.json'] = {
source: function() {
return versionStr
},
size: function() {
return versionStr.length
}
}
cb()
})
}
}
module.exports = PackInfoWebpackPlugin