Repository navigation
Expand file tree
/
Copy pathvalidate-version.cjs
More file actions
executable file
·144 lines (124 loc) · 4.45 KB
/
Copy pathvalidate-version.cjs
File metadata and controls
executable file
·144 lines (124 loc) · 4.45 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
#!/usr/bin/env node
/**
* ============================================================================
* Script: validate-version.cjs
* Location: scripts/validation/validate-version.cjs
* Description:
* - Validates VERSION file against semantic versioning format
* - Validates against version.schema.json
* - Exits with code 0 on success, 1 on failure
* Standards:
* - Follows LightSpeed Coding Standards
* - See: https://github.com/lightspeedwp/.github/blob/HEAD/instructions/coding-standards.instructions.md
* ============================================================================
* @module scripts/validation/validate-version
* @see schemas/version.schema.json
*/
const path = require('path');
const fs = require('fs');
/**
* Validate semantic version string
* @param {string} version - Version string to validate
* @returns {Object} Validation result with valid flag and error message
*/
function validateVersion(version) {
// Semantic versioning regex pattern
// Supports: MAJOR.MINOR.PATCH[-PRERELEASE][+BUILD]
const semverPattern = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;
if (!version || typeof version !== 'string') {
return {
valid: false,
error: 'Version must be a non-empty string'
};
}
const trimmed = version.trim();
if (!semverPattern.test(trimmed)) {
return {
valid: false,
error: `Invalid semantic version format: "${trimmed}". Expected format: MAJOR.MINOR.PATCH (e.g., 1.0.0, 2.1.3-alpha, 1.0.0+build)`
};
}
return {
valid: true,
version: trimmed
};
}
/**
* Parse version components
* @param {string} version - Semantic version string
* @returns {Object} Parsed version components
*/
function parseVersion(version) {
const match = version.match(/^(\d+)\.(\d+)\.(\d+)(?:-([a-zA-Z0-9.-]+))?(?:\+([a-zA-Z0-9.-]+))?$/);
if (!match) {
return null;
}
return {
major: parseInt(match[1], 10),
minor: parseInt(match[2], 10),
patch: parseInt(match[3], 10),
prerelease: match[4] || null,
build: match[5] || null,
raw: version
};
}
/**
* Main validation function
*/
function main() {
const args = process.argv.slice(2);
const versionPath = args[0] || path.join(__dirname, '../VERSION');
console.log(`Validating version file: ${versionPath}`);
try {
// Check if file exists
if (!fs.existsSync(versionPath)) {
console.error(`Error: VERSION file not found: ${versionPath}`);
console.error('Please create a VERSION file containing a semantic version (e.g., 1.0.0)');
process.exit(1);
}
// Read version file
const content = fs.readFileSync(versionPath, 'utf8');
const version = content.trim();
console.log(`Version string: "${version}"`);
// Validate version
const result = validateVersion(version);
if (result.valid) {
const parsed = parseVersion(result.version);
console.log('✓ Version is valid');
console.log('');
console.log('Version components:');
console.log(` - Major: ${parsed.major}`);
console.log(` - Minor: ${parsed.minor}`);
console.log(` - Patch: ${parsed.patch}`);
if (parsed.prerelease) {
console.log(` - Prerelease: ${parsed.prerelease}`);
}
if (parsed.build) {
console.log(` - Build: ${parsed.build}`);
}
process.exit(0);
} else {
console.error('✗ Version validation failed:');
console.error(` ${result.error}`);
console.error('');
console.error('Valid examples:');
console.error(' - 1.0.0');
console.error(' - 2.1.3');
console.error(' - 1.0.0-alpha');
console.error(' - 1.0.0-beta.1');
console.error(' - 1.0.0+20130313144700');
process.exit(1);
}
} catch (error) {
console.error(`Error: ${error.message}`);
if (error.stack) {
console.error(error.stack);
}
process.exit(1);
}
}
// Run if executed directly
if (require.main === module) {
main();
}
module.exports = { validateVersion, parseVersion, main };