Skip to content

Commit 1359521

Browse files
committed
feat: support multi-language with EN and zh-CN
1 parent 86d4ad6 commit 1359521

File tree

8 files changed

+160
-54
lines changed

8 files changed

+160
-54
lines changed

config.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"lang": "zh",
23
"authors": [
34
"程沛权",
45
"chengpeiquan"
@@ -9,11 +10,13 @@
910
],
1011
"repos": [
1112
"D:\\Project\\npm-project\\vue-picture-cropper",
12-
"D:\\Project\\npm-project\\vite-plugin-banner"
13+
"D:\\Project\\npm-project\\vue-baidu-analytics",
14+
"D:\\Project\\npm-project\\vue-cnzz-analytics"
1315
],
1416
"format": {
1517
"vue-picture-cropper": "Vue Picture Cropper",
16-
"vite-plugin-banner": "Vite Plugin Banner"
18+
"vue-baidu-analytics": "Vue Baidu Analytics",
19+
"vue-cnzz-analytics": "Vue CNZZ Analytics"
1720
},
1821
"includes": [
1922
"feat",

src/index.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,32 @@ function start() {
1010
try {
1111
const config = getConfig()
1212
if (!config) return
13-
const { authors, dateRange, repos, format, includes, excludes } = config
13+
const { lang, authors, dateRange, repos, format, includes, excludes } =
14+
config
15+
const isEN = lang === 'en'
1416
const startTime = dateRange[0]
1517
const endTime = dateRange[1]
1618
const result = {}
17-
console.log(`正在分析 ${repos.length} 个仓库的 Log ,请耐心等待…`)
1819

19-
// 创建正则
20+
console.log(
21+
isEN
22+
? `Analyzing the Log of ${repos.length} repo${
23+
repos.length > 1 ? 's' : ''
24+
}, please be patient...`
25+
: `正在分析 ${repos.length} 个仓库的 Log ,请耐心等待…`
26+
)
27+
28+
// Create regular expression
2029
const reg = {
2130
author: new RegExp(authors.join('|'), 'gim'),
2231
include: new RegExp(includes.join('|'), 'gim'),
2332
exclude: new RegExp(excludes.join('|'), 'gim'),
2433
}
2534

26-
// 遍历仓库
35+
// The reports use repo to split paragraphs
2736
const allLogs = []
2837
repos.forEach((repo) => {
29-
// 获取仓库名称
38+
// Get the repo name
3039
const repoName = getRepoName(repo, format)
3140
if (!result[repoName]) {
3241
result[repoName] = {
@@ -40,15 +49,15 @@ function start() {
4049
}
4150
}
4251

43-
// 要执行的命令
52+
// Create CMDs
4453
const cmds = [
4554
`cd ${resolve(repo)}`,
4655
'git pull',
4756
`git log --pretty=format:"%an|||%ae|||%s|||'%h|||%ad"`,
4857
]
4958
const cmd = cmds.join(' && ')
5059

51-
// 获取Git操作记录
60+
// Get commit records from git repo
5261
const res = execSync(cmd)
5362
const str = String(res)
5463
const logs = str
@@ -57,16 +66,16 @@ function start() {
5766
.filter((log) => reg.include.test(log))
5867
.filter((log) => !reg.exclude.test(log))
5968

60-
// 合并记录
69+
// Meger all logs
6170
logs.forEach((log) => allLogs.push(`${repoName}|||${log}`))
6271
})
6372

64-
// 去重
73+
// Deduplicate
6574
const uniqueLogs = [...new Set(allLogs)]
6675

67-
// 提取目标数据
76+
// Get target data
6877
const targetList = uniqueLogs
69-
.map((log) => formatLog(log))
78+
.map((log) => formatLog({ log, isEN }))
7079
.filter((log) => {
7180
const { unix } = log
7281
return unix >= startTime && unix <= endTime
@@ -75,14 +84,14 @@ function start() {
7584
return a.unix - b.unix
7685
})
7786

78-
// 归类
87+
// Classify
7988
targetList.forEach((item) => {
8089
const { repo, type } = item
8190
result[repo][type].push(item)
8291
})
8392

84-
// 写入报告
85-
saveReport(result)
93+
// Save
94+
saveReport({ result, isEN })
8695
} catch (e) {
8796
confirmExit(e)
8897
}

src/libs/confirmExit.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
/**
2-
* 退出确认
2+
* Confirm Exit
3+
* @param {string} msg - The message to displayed on console.
4+
* @param {boolean} isEN - Check whether the language is English.
35
*/
4-
module.exports = function (msg) {
6+
module.exports = function ({ msg, isEN }) {
57
console.log('')
68
console.log(msg)
7-
console.log(`按下任意键退出…`)
9+
console.log(isEN ? `Press any key to exit…` : `按下任意键退出…`)
810
process.stdin.setRawMode(true)
911
process.stdin.resume()
1012
process.stdin.on('data', process.exit.bind(process, 0))

src/libs/formatCommit.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,42 @@
1-
module.exports = function (commit) {
2-
// 提交类型
1+
/**
2+
* Format Commit
3+
* @param {object} commit - A commit object
4+
* @param {boolean} isEN - Check whether the language is English.
5+
* @returns
6+
* type: the commit type
7+
* category: the text about commit type
8+
* msg: the commit message
9+
*/
10+
module.exports = function ({ commit, isEN }) {
11+
// Get the text according to the type
312
let type = 'chore'
4-
let category = '代码优化'
13+
let category = isEN ? 'Chores' : '其他优化'
514
if (commit.startsWith('feat')) {
615
type = 'feat'
7-
category = '功能开发'
16+
category = isEN ? 'Features' : '功能开发'
817
}
918
if (commit.startsWith('fix')) {
1019
type = 'fix'
11-
category = 'BUG修复'
20+
category = isEN ? 'Bug Fixes' : 'BUG修复'
1221
}
1322
if (commit.startsWith('docs')) {
1423
type = 'docs'
15-
category = '完善文档'
24+
category = isEN ? 'Documentation' : '完善文档'
1625
}
1726
if (commit.startsWith('style')) {
1827
type = 'style'
19-
category = '优化样式'
28+
category = isEN ? 'Optimized Style' : '优化样式'
2029
}
2130
if (commit.startsWith('refactor')) {
2231
type = 'refactor'
23-
category = '代码重构'
32+
category = isEN ? 'Refactored' : '代码重构'
2433
}
2534
if (commit.startsWith('test')) {
2635
type = 'test'
27-
category = '测试用例'
36+
category = isEN ? 'Test Cases' : '测试用例'
2837
}
2938

30-
// 截取提交内容
39+
// Extract messages from the commit log
3140
let msg = commit
3241
const index = commit.indexOf(':')
3342
if (index > -1) {

src/libs/formatLog.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
const dayjs = require('dayjs')
22
const formatCommit = require('./formatCommit')
33

4-
module.exports = function (log) {
4+
/**
5+
* Format Log
6+
* @param {string} log - The line content from log
7+
* @param {boolean} isEN - Check whether the language is English.
8+
* @returns An object about log
9+
*/
10+
module.exports = function ({ log, isEN }) {
511
const arr = String(log).split('|||')
612
return {
713
repo: arr[0] || '',
814
author: arr[1] || '',
915
email: arr[2] || '',
1016
commit: arr[3] || '',
11-
...formatCommit(arr[3]),
17+
...formatCommit({
18+
commit: arr[3],
19+
isEN,
20+
}),
1221
hash: arr[4].replace(/'/, '#') || '',
1322
time: dayjs(arr[5]).format('YYYY-MM-DD HH:mm:ss'),
1423
unix: dayjs(arr[5]).unix() * 1000,

src/libs/getConfig.js

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,49 @@ const { cwd } = require('process')
44
const dayjs = require('dayjs')
55
const confirmExit = require('./confirmExit')
66

7+
/**
8+
* Get Config
9+
* @description Format content from configuration file and check validity.
10+
* @tips The `dateRange` will be transformed from `string[]` to `number[]`
11+
* @returns An object from the configuration file
12+
*/
713
module.exports = function () {
14+
let isEN = true
15+
816
try {
17+
// Read content from configuration file
918
const configFile = resolve(`${cwd()}/config.json`)
1019
const configStr = readFileSync(configFile)
1120
if (!configStr) {
1221
return null
1322
}
1423

15-
// 格式化配置
1624
const config = JSON.parse(configStr)
1725
const { dateRange } = config
1826

19-
// 校验数据
27+
// Check validity
2028
const keys = Object.keys(config)
2129
for (let i = 0; i < keys.length; i++) {
2230
const key = keys[i]
2331
const value = config[key]
2432

25-
// 格式化是个对象
33+
// Check language, Set the default language to English
34+
if (key === 'lang') {
35+
if (!['en', 'zh'].includes(value)) {
36+
config['lang'] = 'en'
37+
}
38+
}
39+
isEN = config['lang'] === 'en'
40+
41+
// Check object
2642
if (key === 'format') {
2743
if (Object.prototype.toString.call(value) !== '[object Object]') {
28-
confirmExit(`${key} 必须是一个 { [key: string]: string } 对象`)
44+
confirmExit({
45+
msg: isEN
46+
? `${key} must be an object as { [key: string]: string }`
47+
: `${key} 必须是一个 { [key: string]: string } 对象`,
48+
isEN,
49+
})
2950
return null
3051
}
3152

@@ -34,35 +55,61 @@ module.exports = function () {
3455
Object.hasOwnProperty.call(value, k) &&
3556
typeof value[k] !== 'string'
3657
) {
37-
confirmExit(`${key}${k} 的值必须是一个 string 字符串`)
58+
confirmExit({
59+
msg: isEN
60+
? `The value of ${k} of ${key} must be a string`
61+
: `${key}${k} 的值必须是一个 string 字符串`,
62+
isEN,
63+
})
3864
return null
3965
}
4066
}
4167
}
42-
// 其他都是数组
43-
else {
68+
69+
// Check array
70+
if (
71+
['authors', 'dateRange', 'repos', 'includes', 'excludes'].includes(key)
72+
) {
4473
if (!Array.isArray(value)) {
45-
confirmExit(`${key} 必须是一个 string[] 数组`)
74+
confirmExit({
75+
msg: isEN
76+
? `${key} must be a string[] array`
77+
: `${key} 必须是一个 string[] 数组`,
78+
isEN,
79+
})
4680
return null
4781
}
4882
if (['authors', 'repos'].includes(key) && !value.length) {
49-
confirmExit(`${key} 不能为空`)
83+
confirmExit({
84+
msg: isEN ? `${key} cannot be empty` : `${key} 不能为空`,
85+
isEN,
86+
})
5087
return null
5188
}
5289
if (value.length) {
5390
for (let e = 0; e < value.length; e++) {
5491
if (typeof value[e] !== 'string') {
55-
confirmExit(`${key} 的每个 item 都必须是 string 格式`)
92+
confirmExit({
93+
msg: isEN
94+
? `Each item of ${key} must be a string`
95+
: `${key} 的每个 item 都必须是 string 格式`,
96+
isEN,
97+
})
5698
return null
5799
}
58100
}
59101
}
60102
}
61103
}
62104

63-
// 处理起止日期
105+
// Handle start date and end date, transform to timestamp
64106
if (dateRange.length && dateRange.length !== 2) {
65-
confirmExit(`dateRange 只能有 2 个值,[开始日期, 结束日期]`)
107+
confirmExit({
108+
msg: isEN
109+
? `dateRange can only have 2 values, [start date, end date]`
110+
: `dateRange 只能有 2 个值,[开始日期, 结束日期]`,
111+
isEN,
112+
})
66113
return null
67114
}
68115
let startTime = dayjs().startOf('day').unix() * 1000
@@ -75,7 +122,10 @@ module.exports = function () {
75122

76123
return config
77124
} catch (e) {
78-
confirmExit(e)
125+
confirmExit({
126+
msg: e,
127+
isEN,
128+
})
79129
return null
80130
}
81131
}

src/libs/getRepoName.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
const { resolve, sep } = require('path')
22

3+
/**
4+
* Get Repo Name
5+
* @param {*} repoDir - The item in configuration `config.repos`
6+
* @param {*} format - The configuration `config.format`
7+
* @returns The repository name
8+
*/
39
module.exports = function (repoDir, format) {
410
const arr = resolve(repoDir).split(sep)
511
if (!arr.length) {

0 commit comments

Comments
 (0)