-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
97 lines (87 loc) · 2.83 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
var path = require('path');
var fs = require('fs');
var colors = require('colors');
var shell = require('shelljs');
const VERSION_REG_EXP = /(\d\.+){2,3}\d+/;
exports.name = 'tag <tagname>';
exports.desc = 'Tagging your project';
exports.options = {
'-c <config>': 'path of the config file',
'-m <message>': 'add additional message to the git tag',
'-h, --help': 'print this help message'
};
exports.run = function(argv, cli, env) {
// 显示帮助信息
if (argv.h || argv.help) {
return cli.help(exports.name, exports.options);
}
// 检查git
if (!shell.which('git')) {
return fis.log.error(colors.red('Sorry, this script requires git!'));
}
// 检查tag是否符合规范
var version = argv['_'][1];
if (!VERSION_REG_EXP.test(version)) {
return fis.log.error(colors.red(`Sorry, the tag must match with the pattern "${VERSION_REG_EXP}"!`));
}
var tagConfigPath = typeof argv.c === 'string' ? argv.c : '/tag.json';
var tagConfig = fis.util.readJSON(path.join(process.cwd(), tagConfigPath));
// 写入文件
var filePaths = [];
for (var key in tagConfig) {
if (tagConfig.hasOwnProperty(key)) {
var file = tagConfig[key];
file.path = path.join(process.cwd(), file.path);
filePaths.push(file.path);
tagFile(file.path, new RegExp(file.regExp, 'gim'), version);
}
}
if (!filePaths.length) {
return fis.log.error(colors.red('Config file is invalid!\n'));
}
// git 添加文件
if (shell.exec('git add ' + filePaths.join(' ')).code !== 0) {
return fis.log.error(colors.red('Git add failed!\n'));
}
// git 提交文件
if (shell.exec(`git commit -m "chore[ALL][ALL](version) release v${version}" -n`).code !== 0) {
return fis.log.error(colors.red('Git commit failed!\n'));
}
// git add tag
var message = argv.m ? argv.m : ('v' + version);
var gitTagCmd = `git tag -a v${version} -m "${message}"`;
if (shell.exec(gitTagCmd).code !== 0) {
return fis.log.error(colors.red('Git tag failed!\n'));
}
fis.log.info(colors.green('Git tag successfully!'));
fis.log.info(colors.green('Current version: v' + version));
};
/**
* 为文件打tag
* @param filePath 文件路径
* @param regExp 匹配的正则
* @param tag 需要打的tag
*/
function tagFile(filePath, regExp, tag) {
try {
var fileContent = fs.readFileSync(filePath, 'utf8').toString();
var versionContent = fileContent.match(regExp)[0]
.replace(VERSION_REG_EXP, tag);
var newFileContent = fileContent.replace(regExp, versionContent);
try {
fs.writeFileSync(filePath, newFileContent, 'utf8');
} catch (err) {
fis.log.error(
colors.red('A Failure occured while writting file %s.\n'),
filePath,
err
);
}
} catch (err) {
fis.log.error(
colors.red('A Failure occured while read %s, %s.\n'),
filePath,
err
);
}
}