forked from dcloudio/uni-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
222 lines (205 loc) · 4.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
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
const path = require('path')
const fs = require('fs')
const modulesPath = path.join(__dirname, 'uni_modules')
let componentsPackageJsons = getAllComponentsList(modulesPath)
const syncVersions = path.join(__dirname, 'temps', 'sync-version.json')
let syncVersionsData = fs.readFileSync(syncVersions).toString()
syncVersionsData = JSON.parse(syncVersionsData)
let syncVersion = {}
let uniuimd = ''
componentsPackageJsons.forEach(item => {
let {
id,
version
} = item
if (version !== syncVersionsData[id]) {
let changelog = path.join(modulesPath, id, 'changelog.md')
// let = readChangelogFile(changelog)
let changeMd = fs.readFileSync(changelog).toString()
const mds = versionAll(changeMd,syncVersionsData[id])
let content = ''
mds.forEach(md=>{
md = md.replace(/- /g,`- ${id} `)
content += (md +'\n')
})
uniuimd += (content +'\n')
}
syncVersion[id] = version
})
let uniuiPackage = path.join(modulesPath, 'uni-ui', 'package.json')
uniuiPackage = JSON.parse(fs.readFileSync(uniuiPackage).toString())
console.log(uniuiPackage.version);
const uniuiChangelog = updateChangelogFile(
path.join(modulesPath, 'uni-ui', 'changelog.md'),
uniuiPackage.id,
uniuimd
)
console.log(uniuiChangelog)
// // 将最新的个组件写入缓存区域
// fs.writeFileSync(path.join(__dirname,'temps','sync-version.json'),JSON.stringify(syncVersion,null,2))
// ------ 写入最新的缓存版本号 end------
/**
* 获取所有组件
*/
function getAllComponentsList(modulesPath) {
const exists = fs.existsSync(modulesPath)
if (!exists) {
return []
}
const fileLists = fs.readdirSync(modulesPath);
let content = []
fileLists.forEach(name => {
if (name === 'uni-ui') {
return
}
const packagePath = path.join(modulesPath, name, 'package.json')
let data = fs.readFileSync(packagePath).toString()
content.push(JSON.parse(data))
})
return content
}
/**
* 读取 changelog
*/
function readVersions(str) {
const versionRE = /##\s+([0-9\.]+).*/g
const curVersionMatch = versionRE.exec(str)
if (!curVersionMatch) {
return []
}
const curVersion = {
version: curVersionMatch[1].trim(),
line: curVersionMatch[0],
}
const lastVersionMatch = versionRE.exec(str)
if (!lastVersionMatch) {
return [curVersion]
}
return [
curVersion,
{
version: lastVersionMatch[1].trim(),
line: lastVersionMatch[0],
},
]
}
function padZero(val) {
return val > 9 ? String(val) : '0' + val
}
function generateVersionLog(version, log, date = new Date()) {
return `## ${version}(${date.getFullYear()}-${padZero(
date.getMonth() + 1
)}-${padZero(date.getDate())})
${log}
`
}
function updateChangelogFile(
dir,
newVersion,
newLog,
date
) {
let changelogPath = dir
if (path.extname(dir) !== '.md') {
changelogPath = path.resolve(dir, 'changelog.md')
}
if (!fs.existsSync(changelogPath)) {
return generateVersionLog(newVersion, newLog, date)
}
return updateChangelog(
fs.readFileSync(changelogPath).toString(),
newVersion,
newLog,
date
)
}
function updateChangelog(
md,
newVersion,
newLog,
date
) {
const {
version,
loc
} = readChangelog(md)
if (version === newVersion) {
return (
md.substring(0, loc.start) +
generateVersionLog(newVersion, newLog, date) +
md.substring(loc.end, md.length)
)
} else {
md = generateVersionLog(newVersion, newLog, date) + md
}
return md
}
function readChangelog(md) {
const [curVersion, lastVersion] = readVersions(md)
if (!curVersion) {
return {
version: '',
log: '',
loc: {
start: 0,
end: 0
}
}
}
const start = md.indexOf(curVersion.line)
const curVersionIndex = start + curVersion.line.length
const end = lastVersion ? md.indexOf(lastVersion.line) : md.length
return {
version: curVersion.version,
log: md.substring(curVersionIndex, end).trim(),
loc: {
start,
end,
},
}
}
function readChangelogFile(dir) {
let changelogPath = dir
if (path.extname(dir) !== '.md') {
changelogPath = path.resolve(dir, 'changelog.md')
}
if (!fs.existsSync(changelogPath)) {
return {
version: '',
log: '',
loc: {
start: 0,
end: 0
}
}
}
return readChangelog(fs.readFileSync(changelogPath).toString())
}
function versionAll(md,oldVersion,mds=[]){
let data = readChangelog(md)
let {loc,log,version} = data
if(compareVersion(version , oldVersion)){
const newMd = md.substring(loc.end,md.length).trim()
mds.push(log)
versionAll(newMd,oldVersion,mds)
}
return mds
}
function compareVersion (a,b){
a = a.split('.')
b = b.split('.')
if(a.length !== b.length){
console.error('版本号格式不正确')
return false
}
if(a[0] !== b[0]){
return a[0] >= b[0]
}
if(a[1] !== b[1]){
return a[1] >= b[1]
}
if(a[2] !== b[2]){
return a[2] >= b[2]
}
return false
}