-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
127 lines (117 loc) · 3.32 KB
/
index.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* webpack plugin
* 记录当前构建中的信息
* 并且在console中输出起信息
*/
const _get = require('lodash.get')
const fs = require('fs-extra')
const getGitInfo = require('./utils/generate-git-info')
const {
resolveWebpackEntry,
resolveApp,
readFile,
resolve,
replaceStr,
join,
} = require('./utils')
const NAME = 'last-release-git-info-plugin'
class LastRelease {
constructor (options) {
this.options = Object.assign(
{
buildLog: true, // 是否在构建的时候输出最新commit构建信息
log: false, // 是否在浏览器控制台输出构建信息
generateFile: true, // 是否生成构建信息文件
releaseFileName: 'release_git_info.txt', // 最新构建信息
showBuildTime: true, // 显示构建时间
showBuildBranch: true, // 显示构建分支
showBuildCommitId: true, // 显示构建commitID
showDeveloperName: false, // 显示开发者名称
showDeveloperEmail: false, // 显示开发者邮箱
showBuildCommitDate: true, // 显示commit日期
showBuildCommitInfo: true, // 显示commit信息
externalTxt: '', // 扩展的显示的字符串
},
options
)
}
apply(compiler) {
const {
showBuildTime,
showBuildBranch,
showBuildCommitId,
showDeveloperName,
showDeveloperEmail,
showBuildCommitInfo,
showBuildCommitDate,
log,
releaseFileName,
externalTxt,
buildLog,
} = this.options
// common
if (process.env.NODE_ENV === 'development') return
// v4
if (_get(compiler, 'options.mode') === 'development') return
// 生成git info信息
const gitInfoText = getGitInfo({
showBuildTime,
showBuildBranch,
showBuildCommitId,
showDeveloperName,
showDeveloperEmail,
showBuildCommitInfo,
showBuildCommitDate,
externalTxt,
})
// 在构建的时候输出构建信息,会输出所有信息
if (buildLog) {
const buildLogText = getGitInfo({
showBuildTime: true,
showBuildBranch: true,
showBuildCommitId: true,
showDeveloperName: true,
showDeveloperEmail: true,
showBuildCommitInfo: true,
showBuildCommitDate: true,
externalTxt,
})
console.log(buildLogText)
}
// 修改 webpack 入口文件
if (log) {
compiler.options.entry = resolveWebpackEntry(compiler.options.entry, {
NAME,
filePath: resolveApp('main.js')
})
// 生成写入版本号的文件到 app
compiler.hooks.beforeRun.tap(NAME, () => {
// 清空缓存文件夹
fs.emptyDirSync(resolveApp())
this.generateFile(
resolveApp('main.js'),
readFile(resolve('src', 'main.js')),
{
LAST_RELEASE_GIT_INFO: gitInfoText
}
)
})
}
// 复制文件到 webpack 输出目录
compiler.hooks.done.tap(NAME, () => {
const outputPath = _get(compiler, 'outputPath', '')
// release git信息
fs.outputFileSync(
join(outputPath, releaseFileName),
gitInfoText
)
})
}
generateFile(dest = '', content = '', extraReplacement = {}) {
fs.outputFileSync(
dest,
replaceStr(content, extraReplacement)
)
}
}
module.exports = LastRelease;