-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
164 lines (145 loc) · 5.75 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"use strict";
var execSync = require("child_process").execSync;
var fs = require("fs");
var path = require("path");
var os = require('os');
var cacheData = require('./cacheData');
// 格式化时间
var dateFormat = function dateFormat(fmt, date) {
var ret = void 0;
var opt = {
"Y+": date.getFullYear().toString(), // 年
"m+": (date.getMonth() + 1).toString(), // 月
"d+": date.getDate().toString(), // 日
"H+": date.getHours().toString(), // 时
"M+": date.getMinutes().toString(), // 分
"S+": date.getSeconds().toString() // 秒
// 有其他格式化字符需求可以继续添加,必须转化成字符串
};
for (var k in opt) {
ret = new RegExp("(" + k + ")").exec(fmt);
if (ret) {
fmt = fmt.replace(ret[1], ret[1].length === 1 ? opt[k] : opt[k].padStart(ret[1].length, "0"));
};
};
return fmt;
};
// 分支插件
var BranchPlugin = function BranchPlugin(options) {
options = options || {};
this.options = options;
};
BranchPlugin.prototype.apply = function (compiler) {
var _this = this;
compiler.hooks.afterEmit.tap("branch-plugin", function () {
// 发布信息
var branchInfo = "\n",
commandInfo = "\n",
userName = "\n",
mailName = "\n",
publishTime = "\n",
publishStr = "\n",
subBranchInfo = '';
try {
commandInfo = process.env["npm_lifecycle_script"].toString().trim();
} catch (e) {}
try {
branchInfo = execSync("git rev-parse --abbrev-ref HEAD");
} catch (e) {}
try {
subBranchInfo = execSync("git config --file .gitmodules --get-regexp '^submodule..*.branch$'");
if (subBranchInfo) {
subBranchInfo = '子模块分支:' + subBranchInfo;
}
} catch (e) {}
try {
userName = execSync("git config user.name");
} catch (e) {}
try {
mailName = execSync("git config user.email");
} catch (e) {}
try {
publishTime = dateFormat("YYYY-mm-dd HH:MM:SS", new Date());
} catch (e) {}
publishStr = "\u8D44\u6E90\u5305\u63D0\u4F9B\u8005\uFF1A" + userName + "\u6267\u884C\u811A\u672C\uFF1A" + commandInfo + "\n\u90AE\u7BB1\uFF1A" + mailName + "\u751F\u6210\u65E5\u671F\uFF1A" + publishTime + "\n\u53D1\u5E03\u5206\u652F\uFF1A" + branchInfo + subBranchInfo + new Array(80).join("*") + "\n";
// 最后一次提交记录信息
var commit = "",
name = "",
email = "",
date = "",
message = "",
versionStr = "";
try {
commit = execSync("git show -s --format=%H").toString().trim();
} catch (e) {}
try {
name = execSync("git show -s --format=%cn").toString().trim();
} catch (e) {}
try {
email = execSync("git show -s --format=%ce").toString().trim();
} catch (e) {}
try {
date = new Date(execSync("git show -s --format=%cd").toString());
} catch (e) {}
try {
message = execSync("git show -s --format=%s").toString().trim(); // 说明
} catch (e) {}
if (commit || name || email || date || message) {
versionStr = "\u6700\u8FD1\u4E00\u6B21\u63D0\u4EA4\uFF1A\n\u4F5C\u8005\uFF1A" + name + "<" + email + ">\n\u65E5\u671F\uFF1A" + (date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes()) + "\nmessage\uFF1A" + message + "\ncommitId\uFF1A" + commit + "\n" + new Array(80).join("*") + "\n";
}
// 系统信息
var nodeVersion = "",
npmVersion = "",
currentDir = "",
type = "",
platform = "",
arch = "",
hostname = "",
release = "",
envStr = "";
try {
nodeVersion = execSync("node -v").toString().trim();
} catch (e) {}
try {
npmVersion = execSync("npm -v").toString().trim();
} catch (e) {}
try {
currentDir = __dirname;
} catch (e) {}
try {
type = os.type();
} catch (e) {}
try {
platform = os.platform();
} catch (e) {}
try {
arch = os.arch();
} catch (e) {}
try {
hostname = os.hostname();
} catch (e) {}
try {
release = os.release();
} catch (e) {}
if (nodeVersion || npmVersion || currentDir || type || platform || arch || hostname || release) {
envStr = "\u73AF\u5883\u4FE1\u606F\uFF1A\nnode\u7248\u672C\uFF1A" + nodeVersion + "\nnpm\u7248\u672C\uFF1A" + npmVersion + "\n\u9879\u76EE\u6587\u4EF6\u5939\uFF1A" + currentDir + "\n\u64CD\u4F5C\u7CFB\u7EDF\u540D\u79F0\uFF1A" + type + "\n\u64CD\u4F5C\u7CFB\u7EDF\u7C7B\u578B\uFF1A" + platform + "\n\u5904\u7406\u5668\u67B6\u6784\uFF1A" + arch + "\n\u64CD\u4F5C\u7CFB\u7EDF\u4E3B\u673A\u540D\uFF1A" + hostname + "\n\u64CD\u4F5C\u7CFB\u7EDF\u7248\u672C\uFF1A" + release + "\n" + new Array(80).join("*") + "\n";
}
// 写入目录
var _ref = compiler.options.output || {},
pathUrl = _ref.path;
var _ref2 = _this.options || {},
filename = _ref2.filename;
if (!filename.startsWith("/")) {
filename = "/" + filename;
}
try {
fs.writeFileSync(path.join(pathUrl + filename), publishStr + versionStr + envStr, {
encoding: "utf-8",
mode: 438 /* =0666*/
, flag: "w"
});
} catch (e) {}
});
};
BranchPlugin.cacheData = cacheData;
module.exports = BranchPlugin;