-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_tag.js
228 lines (201 loc) · 6.96 KB
/
command_tag.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* Copyright (c) 2016-present, rainie, Inc.
* All rights reserved.
*
*/
const co = require('co');
const colors = require('colors');
const moment = require('moment');
const inquirer = require('inquirer');
const thunkify = require('thunkify');
const promptMessage = colors.blue('git-tag-generate') + ': ';
const commandChangelog = require('./command_changelog.js');
// 更新 week 设置
moment.locale('zh-cn', {
// 每年第一周的定义:
// 国内:包含1月4号的那周为每年第一周
// 美国:包含1月1号的那周为每年第一周(苹果日历也是如此)
// 更新了下 moment,现在规则是 包含1月1号的那周为每年第一周,新的一周起始于周一(比较好理解,苹果日历也可设置)
week: {
dow: 1, // Monday is the first day of the week.
doy: 7 // The week that contains Jan 1th is the first week of the year.
}
});
// 功能类库加载
const commandAdd = require('./lib/git.js');
function main() {
let currentVersion = '';
let currentVersionWithoutTag = '';
try {
currentVersion = commandAdd.getCurentVersion();
let currentVersionArr = currentVersion.split('+');
currentVersionWithoutTag = currentVersionArr[0];
console.log('!!!!!!!!!!! '.rainbow + `当前版本: ${currentVersion.white}` + ' !!!!!!!!!!!'.rainbow);
generateVersion(currentVersionWithoutTag);
} catch (e) {
console.log(`当前目录:${process.cwd()},不存在 package.json 文件,请到 package.json 文件所在目录执行命令`.red);
process.exit(1);
}
}
// 生成版本号
function generateVersion(currentVersionWithoutTag) {
let versionNextSuggest = {
major: commandAdd.generateTag({
version: currentVersionWithoutTag,
part: 'major'
}),
feature: commandAdd.generateTag({
version: currentVersionWithoutTag,
part: 'feature'
}),
patch: commandAdd.generateTag({
version: currentVersionWithoutTag,
part: 'patch'
})
};
let schema = [{
type: 'list',
name: 'version',
message: promptMessage + colors.gray('semver 规范的版本号:'),
default: versionNextSuggest.patch,
choices: [{
short: '自定义',
name: '自定义\n' +
colors.gray(' - 格式如 ${major}.${feature}.${patch}(请遵循 semver 规范)'),
value: false
}, {
short: versionNextSuggest.patch,
name: 'patch (' + versionNextSuggest.patch + ')\n' +
colors.gray(' - 递增修订版本号(用于 bug 修复)'),
value: versionNextSuggest.patch
}, {
short: versionNextSuggest.feature,
name: 'feature (' + versionNextSuggest.feature + ')\n' +
colors.gray(' - 递增特性版本号(用于向下兼容的特性新增, 递增位的右侧位需要清零)'),
value: versionNextSuggest.feature
}, {
short: versionNextSuggest.major,
name: 'major (' + versionNextSuggest.major + ')\n' +
colors.gray(' - 递增主版本号 (用于断代更新或大版本发布,递增位的右侧位需要清零)'),
value: versionNextSuggest.major
}]
}];
inquirer.prompt(schema).then(function(result) {
if (!result.version) {
reVersion();
} else {
generateNewTag(result.version);
}
});
}
// 重新生成版本号
function reVersion() {
let schema = [{
type: 'input',
name: 'version',
message: promptMessage + 'semver 规范的版本号',
validate: function(value) {
if (!/^\d+\.\d+\.\d+$/.test(value)) {
return '[X] 格式如 ${major}.${feature}.${patch} (请遵循 semver 规范)'.red;
}
let res = commandAdd.versionValidate(currentVersionWithoutTag, value);
if (res.pass) {
return true;
} else {
return '[X] '.red + res.message.red;
}
}
}];
inquirer.prompt(schema).then(function(result) {
generateNewTag(result.version);
});
}
// 生成新的tag
function generateNewTag(version) {
let schema = [{
type: 'confirm',
name: 'isNeedPublishTimesTag',
message: promptMessage + '是否添加发布次数 tag (格式如 ${year}w${weeks}${[a-z]本周第几次发布})',
default: false
}];
inquirer.prompt(schema).then(function(result) {
let newTag = commandAdd.generateTagHandInput({
version: version,
isNeedPublishTimesTag: result.isNeedPublishTimesTag
});
tagConfirm(newTag);
});
}
// tag 确认
function tagConfirm(newTag) {
console.log('!!!!!!!!!!! '.rainbow + `新版 tag: ${newTag.white}` + ' !!!!!!!!!!!'.rainbow);
co(function*() {
yield* editChangelog(newTag);
yield* editPackage(newTag);
yield* gitTagAdd(newTag);
yield* gitTagPush(newTag);
console.log('😁 Good Job!');
}).catch(err => {
console.log('😟 ' + err.message.red);
});
}
// 修改package
function *editPackage(newTag) {
yield thunkify(commandAdd.changePackage)(newTag);
console.log('>>> package.json 更改成功'.green);
}
// 修改changelog
function *editChangelog(newTag) {
let schema = [{
type: 'confirm',
name: 'confirm',
message: promptMessage + '是否记录commit信息到 CHANGELOG.md',
default: true
}];
const result = yield inquirer.prompt(schema);
if (result.confirm) {
yield commandChangelog(newTag);
console.log('>>> CHANGELOG.md 更改成功'.green);
}
}
// 执行 git tag add 命令
function *gitTagAdd(newTag) {
let schema = [{
type: 'confirm',
name: 'confirm',
message: promptMessage + '是否执行 git tag add 命令',
default: true
}];
const result = yield inquirer.prompt(schema);
if (result.confirm) {
let schema = [{
type: 'input',
name: 'message',
message: promptMessage + 'tag 描述信息',
validate: function(value) {
if (!value) {
return 'tag 描述信息不能为空';
}
return true;
}
}];
const result = yield inquirer.prompt(schema);
yield thunkify(commandAdd.gitTagAdd)(newTag, result.message);
console.log('>>> git tag 添加成功!'.green);
}
}
// tag推送到远端
function *gitTagPush(newTag) {
let schema = [{
type: 'confirm',
name: 'confirm',
message: promptMessage + '是否 push tag 到远端',
default: true
}];
const result = yield inquirer.prompt(schema);
if (result.confirm) {
yield thunkify(commandAdd.gitTagPush)(newTag);
console.log('>>> tag 成功推送到远端!'.green);
}
}
module.exports = main;