Repository navigation
Expand file tree
/
Copy pathversionDetector.js
More file actions
266 lines (229 loc) · 6.95 KB
/
Copy pathversionDetector.js
File metadata and controls
266 lines (229 loc) · 6.95 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env node
/**
* ============================================================================
* Module: versionDetector.js
* Location: scripts/agents/includes/versionDetector.js
* Description:
* - Detects semantic version bumps from changelog entries
* - Analyzes conventional commits and changelog sections
* - Determines version bump type (major, minor, patch)
* Standards:
* - Follows LightSpeed Coding Standards
* - Follows Semantic Versioning v2.0.0
* ============================================================================
*/
/**
* Parse semantic version string
* @param {string} versionString - Version string (e.g., "1.2.3", "v1.2.3")
* @returns {Object} Parsed version {major, minor, patch} or null if invalid
*/
function parseVersion(versionString) {
if (!versionString || typeof versionString !== "string") {
return null;
}
const match = versionString.trim().match(/^v?(\d+)\.(\d+)\.(\d+)$/);
if (!match) {
return null;
}
return {
major: parseInt(match[1], 10),
minor: parseInt(match[2], 10),
patch: parseInt(match[3], 10),
};
}
/**
* Format version object to string
* @param {Object} version - Version object {major, minor, patch}
* @returns {string} Formatted version string (e.g., "1.2.3")
*/
function formatVersion(version) {
if (!version || typeof version !== "object") {
return null;
}
if (
typeof version.major !== "number" ||
typeof version.minor !== "number" ||
typeof version.patch !== "number"
) {
return null;
}
return `${version.major}.${version.minor}.${version.patch}`;
}
/**
* Compare two version objects
* @param {Object} version1 - First version {major, minor, patch}
* @param {Object} version2 - Second version {major, minor, patch}
* @returns {number} -1 if v1 < v2, 0 if equal, 1 if v1 > v2
*/
function compareVersions(version1, version2) {
if (!version1 || !version2) {
return null;
}
if (version1.major !== version2.major) {
return version1.major < version2.major ? -1 : 1;
}
if (version1.minor !== version2.minor) {
return version1.minor < version2.minor ? -1 : 1;
}
if (version1.patch !== version2.patch) {
return version1.patch < version2.patch ? -1 : 1;
}
return 0;
}
/**
* Determine version bump type from changelog sections
* @param {Object} entries - Entries organized by section {added, fixed, deprecated, removed, etc}
* @param {Array} breakingChanges - Array of breaking change descriptions
* @returns {string} Bump type: 'major', 'minor', 'patch', or null if no changes
*/
function determineBumpType(entries, breakingChanges = []) {
if (!entries || typeof entries !== "object") {
return null;
}
const safeBreakingChanges = Array.isArray(breakingChanges)
? breakingChanges
: [];
// Major bump: breaking changes or removed section has items
if (safeBreakingChanges.length > 0) {
return "major";
}
if (
entries.removed &&
Array.isArray(entries.removed) &&
entries.removed.length > 0
) {
return "major";
}
// Minor bump: added features or deprecated items
if (
entries.added &&
Array.isArray(entries.added) &&
entries.added.length > 0
) {
return "minor";
}
if (
entries.deprecated &&
Array.isArray(entries.deprecated) &&
entries.deprecated.length > 0
) {
return "minor";
}
// Patch bump: fixed, security, or other changes
if (
(entries.fixed &&
Array.isArray(entries.fixed) &&
entries.fixed.length > 0) ||
(entries.security &&
Array.isArray(entries.security) &&
entries.security.length > 0) ||
(entries.changed &&
Array.isArray(entries.changed) &&
entries.changed.length > 0) ||
(entries.documentation &&
Array.isArray(entries.documentation) &&
entries.documentation.length > 0) ||
(entries.performance &&
Array.isArray(entries.performance) &&
entries.performance.length > 0)
) {
return "patch";
}
return null;
}
/**
* Calculate next version based on current version and bump type
* @param {string} currentVersion - Current version string (e.g., "1.2.3")
* @param {string} bumpType - Bump type: 'major', 'minor', or 'patch'
* @returns {string} Next version string or null if invalid input
*/
function calculateNextVersion(currentVersion, bumpType) {
const parsed = parseVersion(currentVersion);
if (!parsed) {
return null;
}
const nextVersion = { ...parsed };
switch (bumpType) {
case "major":
nextVersion.major += 1;
nextVersion.minor = 0;
nextVersion.patch = 0;
break;
case "minor":
nextVersion.minor += 1;
nextVersion.patch = 0;
break;
case "patch":
nextVersion.patch += 1;
break;
default:
return null;
}
return formatVersion(nextVersion);
}
/**
* Detect version bump from changelog entries
* @param {Object} entries - Entries organized by section
* @param {Array} breakingChanges - Array of breaking change descriptions
* @returns {Object} Bump detection result {bumpType, hasBreakingChanges, hasFeaturesOrDeprecations, hasModifications}
*/
function detectBump(entries, breakingChanges = []) {
const bumpType = determineBumpType(entries, breakingChanges);
const safeBreakingChanges = Array.isArray(breakingChanges)
? breakingChanges
: [];
return {
bumpType,
hasBreakingChanges: safeBreakingChanges.length > 0,
hasFeaturesOrDeprecations:
entries?.added?.length > 0 || entries?.deprecated?.length > 0,
hasModifications:
entries?.fixed?.length > 0 ||
entries?.security?.length > 0 ||
entries?.changed?.length > 0 ||
entries?.documentation?.length > 0 ||
entries?.performance?.length > 0,
};
}
/**
* Get suggestion for next release based on version history
* @param {string} currentVersion - Current version
* @param {Array} versionHistory - Array of previous versions
* @returns {string} Suggested next version
*/
function suggestNextVersion(currentVersion, versionHistory = []) {
const parsed = parseVersion(currentVersion);
if (!parsed) {
return null;
}
const safeHistory = Array.isArray(versionHistory) ? versionHistory : [];
// Check if we're skipping patch versions (common when jumping minor/major)
const allVersions = [currentVersion, ...safeHistory]
.map(parseVersion)
.filter(Boolean);
if (allVersions.length === 0) {
return null;
}
// Sort versions in descending order
allVersions.sort((a, b) => compareVersions(b, a));
// If the highest version is greater than current, increment accordingly
const highest = allVersions[0];
if (compareVersions(highest, parsed) > 0) {
const nextVersion = { ...highest };
nextVersion.patch += 1;
return formatVersion(nextVersion);
}
// Default: increment patch
const nextVersion = { ...parsed };
nextVersion.patch += 1;
return formatVersion(nextVersion);
}
module.exports = {
parseVersion,
formatVersion,
compareVersions,
determineBumpType,
calculateNextVersion,
detectBump,
suggestNextVersion,
};