cloudcc-cli 1.3.3 → 1.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,239 +1,258 @@
1
- const { execSync, exec } = require('child_process');
2
- const chalk = require("chalk")
3
- // 文件编辑对象
4
- const fs = require("fs")
5
- // 配置信息
6
- const projectConfig = JSON.parse(fs.readFileSync('package.json', 'utf8'))
7
- // 网页转换器
8
- const { change } = require("../utils/md2html")
9
- // 检查版本更新
10
- const { checkUpdate } = require("../utils/checkVersion")
11
- // 控制台交互
12
- const { askType, askTypeOther, askBuildType } = require("../utils/askTool")
13
- // 提交代码,设置Tag
14
- const { getNewVersionName, pushCodeAndTags } = require("../utils/pushCode")
15
- // 触发构建器
16
- const { jenkins } = require("../utils/trigger")
17
- // 通知飞书
18
- const { notifyFeishu, notifyDingDing } = require("../utils/notifyIM")
19
- // 时间库
20
- const dayjs = require("dayjs")
21
- // 多语言更新脚本
22
- const { updatei18n, askI18n } = require("../utils/updatei18n")
23
- // 部署脚本
24
- const { deploy } = require("../utils/deploy")
25
- /**
26
- * 项目打包发布脚本
27
- */
28
- class Publish {
29
- constructor() {
30
- /**
31
- * 控制台参数:key=value
32
- * type:发布的tag
33
- * branch:使用的分支
34
- * language:是否更新多语言,'1'更新,'0'不更新
35
- * buildType:打包方式,online-云打包,local-本地打包
36
- * user:打包用户
37
- * update:代码不同步的时候,是否强制更新代码
38
- */
39
- this.args = new Map();
40
- this.arguments = process.argv.splice(2).map((item) => {
41
- let arr = item.split("=");
42
- this.args.set(arr[0], arr[1]);
43
- });
44
- this.user = this.args.get("user") || execSync("git config user.name").toString("utf8").trim();
45
- }
46
- /**
47
- * 初始化
48
- */
49
- async init() {
50
- // 检查cli版本,提示用户更新:false:不更新,true:更新
51
- let update = await checkUpdate();
52
- if (!update) {
53
- // 1:版本发布信息,包含发布版本类型信息
54
- let condition = {};
55
- // 检查命令行是否输入了发布版本信息,如果没有包含,那么询问发布类型
56
- if (this.args.get("type")) {
57
- condition.type = this.args.get("type")
58
- } else {
59
- // 询问发布类型
60
- condition = await askType();
61
- if ("other" == condition.type) {
62
- condition = await askTypeOther();
63
- }
64
- }
65
-
66
- // 1.1:询问打包方式
67
- let buildType = null;
68
- if (this.args.get("buildType")) {
69
- buildType = this.args.get("buildType")
70
- } else {
71
- buildType = await askBuildType();
72
- }
73
- if ("online" == buildType.buildType) {
74
- deploy(projectConfig.config["deploy-" + condition.type] + "," + encodeURI(this.user))
75
- console.log(chalk.green("云发布已开始,请稍后查看通知"))
76
- return
77
- }
78
- // 2:如果命令行包含了分支信息,那么git切换到命令行的分支
79
- if (this.args.get("branch")) {
80
- execSync(`git checkout ${this.args.get("branch")}`);
81
- console.log(chalk.green("切换分支:" + this.args.get("branch")))
82
- console.log()
83
- }
84
-
85
- // 3:获取新的发布版本号
86
- let version = getNewVersionName([condition.type])
87
- console.log();
88
- console.log(chalk.green('待发布版本:' + version));
89
- console.log();
90
- // 3.1:将版本信息写入env文件中,公其他业务使用
91
- try {
92
- fs.writeFileSync(".env.production", "VUE_APP_PROJECT_VERSION = " + condition.type + "-" + version, 'utf-8')
93
- } catch (error) {
94
- }
95
-
96
- // 4:如果存在多语言配置,则更新多语言
97
- if (projectConfig.config && projectConfig.config["language-config"]) {
98
- // 获得命令行多语言参数
99
- let i18n = this.args.get("language")
100
- // 如果存在命令行参数,那么获取,否则询问
101
- if (!i18n) {
102
- i18n = await askI18n()
103
- }
104
- // 如果选择为1,那么下载多语言
105
- if ('1' === i18n || '1' === i18n.checked) {
106
- let laguage = projectConfig.config["language-config"]
107
- // 执行更新多语言文件
108
- await updatei18n(laguage)
109
- console.log(chalk.blue("多语言文件更新完毕,准备开始打包"));
110
- console.log();
111
- }
112
- }
113
-
114
- // 5:开始打包
115
- this.build(condition, version)
116
- }
117
- }
118
- /**
119
- * 编译成功回调
120
- */
121
- buildSucess(condition, version) {
122
- // 6:将readme生成为html,并复制到dist中
123
- let outPath = "dist"
124
- // 读取配置,是否改变了输出路径
125
- if (projectConfig.config && projectConfig.config["doc-path"]) {
126
- outPath = projectConfig.config["doc-path"];
127
- }
128
- try {
129
- // 设置readme文件位置
130
- change("README", outPath);
131
- } catch (error) {
132
- console.log(chalk.red("README写入异常:" + error))
133
- }
134
-
135
- try {
136
- // 7:写入版本信息
137
- this.writeVersion(version,condition.type);
138
- } catch (error) {
139
- console.log(chalk.red("版本信息写入异常:" + error))
140
- }
141
-
142
- // 8:发布代码并设置tags,触发发布,飞书提醒
143
- if (pushCodeAndTags([condition.type], this.args.get("update"))) {
144
- if (projectConfig && projectConfig.config) {
145
- jenkins(projectConfig.config["jenkins-" + condition.type])
146
- }
147
- notifyFeishu(condition.type, version, projectConfig, this.user)
148
- notifyDingDing(condition.type, version, projectConfig, this.user)
149
- console.log();
150
- console.log(chalk.green('发布完成'));
151
- console.log();
152
- }
153
- }
154
- /**
155
- * 编译代码
156
- * @param {发布类型} types:Dev,uat,Release,GA,RC
157
- * @param {发布版本} versions:V12.0.0
158
- * @returns
159
- */
160
- build(condition, version) {
161
- console.log(chalk.green('开始编译,请稍后...'));
162
- console.log();
163
- let that = this;
164
- // 打包命令
165
- exec('npm run build', (error, stdout, stderr) => {
166
- if (error) {
167
- exec('npx vue-cli-service build', (error, stdout, stderr) => {
168
- if (error) {
169
- console.log('编译失败:', error);
170
- console.log(chalk.red('编译失败:' + stdout));
171
- } else {
172
- console.log(chalk.green('编译成功!'));
173
- console.log();
174
- that.buildSucess(condition, version);
175
- }
176
- })
177
- } else {
178
- console.log(chalk.green('编译成功!'));
179
- console.log();
180
- that.buildSucess(condition, version);
181
- }
182
- })
183
-
184
- }
185
- /**
186
- * 将版本信息写入文件
187
- * @param {版本信息} version
188
- */
189
- writeVersion(version,type) {
190
- try {
191
- // 获得分支信息
192
- let branch = execSync('git rev-parse --abbrev-ref HEAD');
193
- branch = branch.toString("utf8").trim();
194
- // 获得提交版本信息
195
- var gitHEAD = fs.readFileSync('.git/HEAD', 'utf-8').trim()
196
- var ref = gitHEAD.split(': ')[1]
197
- var develop = gitHEAD.split('/')[2]
198
- var gitVersion = fs.readFileSync('.git/' + ref, 'utf-8').trim()
199
- var gitCommitVersion = '"' + develop + ': ' + gitVersion + '"'
200
-
201
- let versionInfo =
202
- `
203
- <!DOCTYPE html>
204
- <html lang="en">
205
-
206
- <head>
207
- <meta charset="UTF-8">
208
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
209
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
210
- <title>版本信息</title>
211
- </head>
212
-
213
- <body>
214
- <div style="font-size: 20px">
215
- <div>${projectConfig.name}</div>
216
- <div style="margin-left:48px">
217
- <div>分支:${branch}</div>
218
- <div>标签:${type}</div>
219
- <div>版本号:${version}</div>
220
- <div>提交Hash:${gitCommitVersion}</div>
221
- <div>发布时间:${dayjs().format('YYYY-MM-DD HH:mm:ss')}</div>
222
- <div>发布人员:${this.user}</div>
223
- </div>
224
- </div>
225
- </body>
226
-
227
- </html>
228
- `
229
- fs.writeFileSync("dist/version.html", versionInfo, 'utf-8');
230
- return true
231
- } catch (error) {
232
- return false
233
- }
234
- };
235
- }
236
-
237
- module.exports = Publish;
238
-
239
-
1
+ const { execSync, exec } = require('child_process');
2
+ const chalk = require("chalk")
3
+ // 文件编辑对象
4
+ const fs = require("fs")
5
+ // 配置信息
6
+ const projectConfig = JSON.parse(fs.readFileSync('package.json', 'utf8'))
7
+ // 网页转换器
8
+ const { change } = require("../utils/md2html")
9
+ // 检查版本更新
10
+ const { checkUpdate } = require("../utils/checkVersion")
11
+ // 控制台交互
12
+ const { askType, askTypeOther, askBuildType } = require("../utils/askTool")
13
+ // 提交代码,设置Tag
14
+ const { getNewVersionName, pushCodeAndTags } = require("../utils/pushCode")
15
+ // 触发构建器
16
+ const { jenkins } = require("../utils/trigger")
17
+ // 通知飞书
18
+ const { notifyFeishu, notifyDingDing } = require("../utils/notifyIM")
19
+ // 时间库
20
+ const dayjs = require("dayjs")
21
+ // 多语言更新脚本
22
+ const { updatei18n, askI18n } = require("../utils/updatei18n")
23
+ // 部署脚本
24
+ const { deploy } = require("../utils/deploy")
25
+ /**
26
+ * 项目打包发布脚本
27
+ */
28
+ class Publish {
29
+ constructor() {
30
+ /**
31
+ * 控制台参数:key=value
32
+ * type:发布的tag
33
+ * branch:使用的分支
34
+ * language:是否更新多语言,'1'更新,'0'不更新
35
+ * buildType:打包方式,online-云打包,local-本地打包
36
+ * user:打包用户
37
+ * update:代码不同步的时候,是否强制更新代码
38
+ */
39
+ this.args = new Map();
40
+ this.arguments = process.argv.splice(2).map((item) => {
41
+ let arr = item.split("=");
42
+ this.args.set(arr[0], arr[1]);
43
+ });
44
+ this.user = this.args.get("user") || execSync("git config user.name").toString("utf8").trim();
45
+ }
46
+ /**
47
+ * 初始化
48
+ */
49
+ async init() {
50
+ // 检查cli版本,提示用户更新:false:不更新,true:更新
51
+ let update = await checkUpdate();
52
+ if (!update) {
53
+ // 1:版本发布信息,包含发布版本类型信息
54
+ let condition = {};
55
+ // 检查命令行是否输入了发布版本信息,如果没有包含,那么询问发布类型
56
+ if (this.args.get("type")) {
57
+ condition.type = this.args.get("type")
58
+ } else {
59
+ // 询问发布类型
60
+ condition = await askType();
61
+ if ("other" == condition.type) {
62
+ condition = await askTypeOther();
63
+ }
64
+ }
65
+
66
+ // 1.1:询问打包方式
67
+ let buildType = null;
68
+ if (this.args.get("buildType")) {
69
+ buildType = this.args.get("buildType")
70
+ } else {
71
+ buildType = await askBuildType();
72
+ }
73
+ if ("online" == buildType.buildType) {
74
+ deploy(projectConfig.config["deploy-" + condition.type] + "," + encodeURI(this.user))
75
+ console.log(chalk.green("云发布已开始,请稍后查看通知"))
76
+ return
77
+ }
78
+ // 2:如果命令行包含了分支信息,那么git切换到命令行的分支
79
+ if (this.args.get("branch")) {
80
+ execSync(`git checkout ${this.args.get("branch")}`);
81
+ console.log(chalk.green("切换分支:" + this.args.get("branch")))
82
+ console.log()
83
+ }
84
+
85
+ // 3:如果存在多语言配置,则更新多语言
86
+ if (projectConfig.config && projectConfig.config["language-config"]) {
87
+ // 获得命令行多语言参数
88
+ let i18n = this.args.get("language")
89
+ // 如果存在命令行参数,那么获取,否则询问
90
+ if (!i18n) {
91
+ i18n = await askI18n()
92
+ }
93
+ // 如果选择为1,那么下载多语言
94
+ if ('1' === i18n || '1' === i18n.checked) {
95
+ let laguage = projectConfig.config["language-config"]
96
+ // 执行更新多语言文件
97
+ await updatei18n(laguage)
98
+ console.log(chalk.blue("多语言文件更新完毕,准备开始打包"));
99
+ console.log();
100
+ }
101
+ }
102
+
103
+
104
+ // 4:开始打包
105
+ this.build(condition)
106
+ }
107
+ }
108
+ /**
109
+ * 编译成功回调
110
+ */
111
+ buildSucess(condition) {
112
+ // 5:获取新的发布版本号
113
+ let version = getNewVersionName([condition.type])
114
+ // 5.1:将版本信息写入env文件中,公其他业务使用
115
+ try {
116
+ fs.writeFileSync(".env.production", "VUE_APP_PROJECT_VERSION = " + condition.type + "-" + version, 'utf-8')
117
+ } catch (error) {
118
+ }
119
+
120
+ // 5.2:更新日志文档添加发布日期
121
+ // 获得分支信息
122
+ let branch = execSync('git rev-parse --abbrev-ref HEAD');
123
+ branch = branch.toString("utf8").trim();
124
+ const datas = fs.readFileSync("README.md", 'utf-8').split("\n")
125
+ let data = ""
126
+ datas.map((item, index) => {
127
+ if (0 == index) {
128
+ data = data + `#### 发布版本:${version}` + "\n"
129
+ data = data + `#### 发布时间:${dayjs().format('YYYY-MM-DD HH:mm:ss')}` + "\n"
130
+ data = data + `#### 发布人员:${this.user}` + "\n"
131
+ data = data + `#### 发布分支:${branch}` + "\n"
132
+ data = data + `#### 发布标签:${condition.type}` + "\n"
133
+ if (!item.startsWith("#### 发布版本")) {
134
+ data = data + item + "\n"
135
+ }
136
+ } else if (1 == index) {
137
+ if (!item.startsWith("#### 发布时间")) {
138
+ data = data + item + "\n"
139
+ }
140
+ } else if (2 == index) {
141
+ if (!item.startsWith("#### 发布人员")) {
142
+ data = data + item + "\n"
143
+ }
144
+ } else if (3 == index) {
145
+ if (!item.startsWith("#### 发布分支")) {
146
+ data = data + item + "\n"
147
+ }
148
+ } else if (4 == index) {
149
+ if (!item.startsWith("#### 发布标签")) {
150
+ data = data + item + "\n"
151
+ }
152
+ } else {
153
+ data = data + item + "\n"
154
+ }
155
+ })
156
+ fs.writeFileSync("README.md", data)
157
+ // 6:将readme生成为html,并复制到dist中
158
+ let outPath = "dist"
159
+ // 读取配置,是否改变了输出路径
160
+ if (projectConfig.config && projectConfig.config["doc-path"]) {
161
+ outPath = projectConfig.config["doc-path"];
162
+ }
163
+ try {
164
+ // 设置readme文件位置
165
+ change("README", outPath,"version");
166
+ } catch (error) {
167
+ console.log(chalk.red("README写入异常:" + error))
168
+ }
169
+ // 8:发布代码并设置tags,触发发布,飞书提醒
170
+ if (pushCodeAndTags(version, [condition.type], this.args.get("update"))) {
171
+ if (projectConfig && projectConfig.config) {
172
+ jenkins(projectConfig.config["jenkins-" + condition.type])
173
+ }
174
+ notifyFeishu(condition.type, version, projectConfig, this.user)
175
+ notifyDingDing(condition.type, version, projectConfig, this.user)
176
+ console.log();
177
+ console.log(chalk.green('发布完成'));
178
+ console.log();
179
+ }
180
+ }
181
+ /**
182
+ * 编译代码
183
+ * @param {发布类型} types:Dev,uat,Release,GA,RC
184
+ * @param {发布版本} versions:V12.0.0
185
+ * @returns
186
+ */
187
+ build(condition, version) {
188
+ console.log(chalk.green('开始编译,请稍后...'));
189
+ console.log();
190
+ let that = this;
191
+ // 打包命令
192
+ exec('npm run build', (error, stdout, stderr) => {
193
+ if (error) {
194
+ exec('npx vue-cli-service build', (error, stdout, stderr) => {
195
+ if (error) {
196
+ console.log('编译失败:', error);
197
+ console.log(chalk.red('编译失败:' + stdout));
198
+ } else {
199
+ console.log(chalk.green('编译成功!'));
200
+ console.log();
201
+ that.buildSucess(condition);
202
+ }
203
+ })
204
+ } else {
205
+ console.log(chalk.green('编译成功!'));
206
+ console.log();
207
+ that.buildSucess(condition);
208
+ }
209
+ })
210
+
211
+ }
212
+ /**
213
+ * 将版本信息写入文件
214
+ * @param {版本信息} version
215
+ */
216
+ writeVersion(version, type) {
217
+ try {
218
+ // 获得分支信息
219
+ let branch = execSync('git rev-parse --abbrev-ref HEAD');
220
+ branch = branch.toString("utf8").trim();
221
+ let versionInfo =
222
+ `
223
+ <!DOCTYPE html>
224
+ <html lang="en">
225
+
226
+ <head>
227
+ <meta charset="UTF-8">
228
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
229
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
230
+ <title>版本信息</title>
231
+ </head>
232
+
233
+ <body>
234
+ <div style="font-size: 20px">
235
+ <div>${projectConfig.name}</div>
236
+ <div style="margin-left:48px">
237
+ <div>版本:${version}</div>
238
+ <div>时间:${dayjs().format('YYYY-MM-DD HH:mm:ss')}</div>
239
+ <div>人员:${this.user}</div>
240
+ <div>分支:${branch}</div>
241
+ <div>标签:${type}</div>
242
+ </div>
243
+ </div>
244
+ </body>
245
+
246
+ </html>
247
+ `
248
+ fs.writeFileSync("dist/version.html", versionInfo, 'utf-8');
249
+ return true
250
+ } catch (error) {
251
+ return false
252
+ }
253
+ };
254
+ }
255
+
256
+ module.exports = Publish;
257
+
258
+