6
6
*/
7
7
8
8
import { spawn } from 'child_process' ;
9
- import { dirname , join } from 'path' ;
10
- import { fileURLToPath } from 'url' ;
11
-
12
- const __filename = fileURLToPath ( import . meta. url ) ;
13
- const __dirname = dirname ( __filename ) ;
14
9
15
10
/**
16
11
* Execute a command and return the output
@@ -37,7 +32,9 @@ function execCommand(command, args = [], cwd = process.cwd()) {
37
32
if ( code === 0 ) {
38
33
resolve ( stdout . trim ( ) ) ;
39
34
} else {
40
- reject ( new Error ( `Command failed: ${ command } ${ args . join ( ' ' ) } \n${ stderr } ` ) ) ;
35
+ reject (
36
+ new Error ( `Command failed: ${ command } ${ args . join ( ' ' ) } \n${ stderr } ` )
37
+ ) ;
41
38
}
42
39
} ) ;
43
40
} ) ;
@@ -50,8 +47,12 @@ function execCommand(command, args = [], cwd = process.cwd()) {
50
47
*/
51
48
async function getGitTags ( repoPath = process . cwd ( ) ) {
52
49
try {
53
- const output = await execCommand ( 'git' , [ 'tag' , '--list' , '--sort=-version:refname' ] , repoPath ) ;
54
- return output ? output . split ( '\n' ) . filter ( tag => tag . trim ( ) ) : [ ] ;
50
+ const output = await execCommand (
51
+ 'git' ,
52
+ [ 'tag' , '--list' , '--sort=-version:refname' ] ,
53
+ repoPath
54
+ ) ;
55
+ return output ? output . split ( '\n' ) . filter ( ( tag ) => tag . trim ( ) ) : [ ] ;
55
56
} catch ( error ) {
56
57
throw new Error ( `Failed to get git tags: ${ error . message } ` ) ;
57
58
}
@@ -76,12 +77,16 @@ async function isValidTag(version, repoPath = process.cwd()) {
76
77
* Validate multiple version tags
77
78
* @param {string[] } versions - Array of version strings to validate
78
79
* @param {string } repoPath - Path to the git repository
79
- * @returns {Promise<{valid: boolean, errors: string[], availableTags: string[]}> }
80
+ * @returns {Promise<{
81
+ * valid: boolean,
82
+ * errors: string[],
83
+ * availableTags: string[]
84
+ * }>} Validation result
80
85
*/
81
86
async function validateTags ( versions , repoPath = process . cwd ( ) ) {
82
87
const errors = [ ] ;
83
88
const availableTags = await getGitTags ( repoPath ) ;
84
-
89
+
85
90
for ( const version of versions ) {
86
91
if ( version && version !== 'local' && ! availableTags . includes ( version ) ) {
87
92
errors . push ( `Version '${ version } ' is not a valid git tag` ) ;
@@ -91,7 +96,7 @@ async function validateTags(versions, repoPath = process.cwd()) {
91
96
return {
92
97
valid : errors . length === 0 ,
93
98
errors,
94
- availableTags : availableTags . slice ( 0 , 10 ) // Return top 10 most recent tags
99
+ availableTags : availableTags . slice ( 0 , 10 ) , // Return top 10 most recent tags
95
100
} ;
96
101
}
97
102
@@ -101,26 +106,32 @@ async function validateTags(versions, repoPath = process.cwd()) {
101
106
* @param {string } previousVersion - Previous version (optional)
102
107
* @param {string } repoPath - Path to the git repository
103
108
*/
104
- async function validateVersionInputs ( version , previousVersion = null , repoPath = process . cwd ( ) ) {
109
+ async function validateVersionInputs (
110
+ version ,
111
+ previousVersion = null ,
112
+ repoPath = process . cwd ( )
113
+ ) {
105
114
const versionsToCheck = [ version ] ;
106
115
if ( previousVersion ) {
107
116
versionsToCheck . push ( previousVersion ) ;
108
117
}
109
118
110
119
const validation = await validateTags ( versionsToCheck , repoPath ) ;
111
-
120
+
112
121
if ( ! validation . valid ) {
113
122
console . error ( '\n❌ Version validation failed:' ) ;
114
- validation . errors . forEach ( error => console . error ( ` - ${ error } ` ) ) ;
115
-
123
+ validation . errors . forEach ( ( error ) => console . error ( ` - ${ error } ` ) ) ;
124
+
116
125
if ( validation . availableTags . length > 0 ) {
117
126
console . error ( '\n📋 Available tags (most recent first):' ) ;
118
- validation . availableTags . forEach ( tag => console . error ( ` - ${ tag } ` ) ) ;
127
+ validation . availableTags . forEach ( ( tag ) => console . error ( ` - ${ tag } ` ) ) ;
119
128
} else {
120
129
console . error ( '\n📋 No git tags found in repository' ) ;
121
130
}
122
-
123
- console . error ( '\n💡 Tip: Use "local" for development/testing with local containers' ) ;
131
+
132
+ console . error (
133
+ '\n💡 Tip: Use "local" for development/testing with local containers'
134
+ ) ;
124
135
process . exit ( 1 ) ;
125
136
}
126
137
@@ -134,10 +145,16 @@ async function validateVersionInputs(version, previousVersion = null, repoPath =
134
145
*/
135
146
async function getRepositoryRoot ( startPath = process . cwd ( ) ) {
136
147
try {
137
- const output = await execCommand ( 'git' , [ 'rev-parse' , '--show-toplevel' ] , startPath ) ;
148
+ const output = await execCommand (
149
+ 'git' ,
150
+ [ 'rev-parse' , '--show-toplevel' ] ,
151
+ startPath
152
+ ) ;
138
153
return output ;
139
154
} catch ( error ) {
140
- throw new Error ( `Not a git repository or git not available: ${ error . message } ` ) ;
155
+ throw new Error (
156
+ `Not a git repository or git not available: ${ error . message } `
157
+ ) ;
141
158
}
142
159
}
143
160
@@ -146,24 +163,26 @@ export {
146
163
isValidTag ,
147
164
validateTags ,
148
165
validateVersionInputs ,
149
- getRepositoryRoot
166
+ getRepositoryRoot ,
150
167
} ;
151
168
152
169
// CLI usage when run directly
153
170
if ( import . meta. url === `file://${ process . argv [ 1 ] } ` ) {
154
171
const args = process . argv . slice ( 2 ) ;
155
-
172
+
156
173
if ( args . length === 0 ) {
157
174
console . log ( 'Usage: node validate-tags.js <version> [previous-version]' ) ;
158
175
console . log ( 'Examples:' ) ;
159
176
console . log ( ' node validate-tags.js v3.0.0' ) ;
160
177
console . log ( ' node validate-tags.js v3.0.0 v2.9.0' ) ;
161
- console . log ( ' node validate-tags.js local # Special case for development' ) ;
178
+ console . log (
179
+ ' node validate-tags.js local # Special case for development'
180
+ ) ;
162
181
process . exit ( 1 ) ;
163
182
}
164
183
165
184
const [ version , previousVersion ] = args ;
166
-
185
+
167
186
try {
168
187
const repoRoot = await getRepositoryRoot ( ) ;
169
188
await validateVersionInputs ( version , previousVersion , repoRoot ) ;
@@ -172,4 +191,4 @@ if (import.meta.url === `file://${process.argv[1]}`) {
172
191
console . error ( `Error: ${ error . message } ` ) ;
173
192
process . exit ( 1 ) ;
174
193
}
175
- }
194
+ }
0 commit comments