1
+ const fs = require ( 'fs' ) ;
2
+ const path = require ( 'path' ) ;
3
+ const { promisify } = require ( 'util' ) ;
4
+
5
+ const readFile = promisify ( fs . readFile ) ;
6
+ const writeFile = promisify ( fs . writeFile ) ;
7
+ const access = promisify ( fs . access ) ;
8
+
9
+ /**
10
+ * Updates version references in documentation files
11
+ * @param {string } newVersion - The new version to update to
12
+ */
13
+ async function updateVersions ( newVersion ) {
14
+ if ( ! newVersion ) {
15
+ throw new Error ( 'Version number is required' ) ;
16
+ }
17
+
18
+ // Validate version format (e.g., 1.32.3)
19
+ if ( ! / ^ \d + \. \d + \. \d + $ / . test ( newVersion ) ) {
20
+ throw new Error ( 'Invalid version format. Expected format: x.y.z' ) ;
21
+ }
22
+
23
+ try {
24
+ // Read the current version from package.json
25
+ const packageJson = JSON . parse ( await readFile ( 'package.json' , 'utf8' ) ) ;
26
+ const currentVersion = packageJson . version ;
27
+
28
+ console . log ( `Updating version references from ${ currentVersion } to ${ newVersion } ` ) ;
29
+
30
+ // Update package.json
31
+ packageJson . version = newVersion ;
32
+ await writeFile ( 'package.json' , JSON . stringify ( packageJson , null , 2 ) + '\n' ) ;
33
+ console . log ( 'Updated version in package.json' ) ;
34
+
35
+ // Update version in documentation files
36
+ const docsDir = path . join ( __dirname , '..' , 'docs' ) ;
37
+
38
+ try {
39
+ // Check if docs directory exists
40
+ await access ( docsDir , fs . constants . F_OK ) ;
41
+
42
+ const files = await findFiles ( docsDir , [ '.md' , '.mdx' ] ) ;
43
+ console . log ( `Found ${ files . length } documentation files to process` ) ;
44
+
45
+ for ( const file of files ) {
46
+ const content = await readFile ( file , 'utf8' ) ;
47
+ const updatedContent = content . replace (
48
+ new RegExp ( `v${ currentVersion } ` , 'g' ) ,
49
+ `v${ newVersion } `
50
+ ) ;
51
+
52
+ if ( content !== updatedContent ) {
53
+ await writeFile ( file , updatedContent ) ;
54
+ console . log ( `Updated version in ${ file } ` ) ;
55
+ }
56
+ }
57
+ } catch ( error ) {
58
+ if ( error . code === 'ENOENT' ) {
59
+ console . log ( 'No docs directory found, skipping documentation updates' ) ;
60
+ } else {
61
+ throw error ;
62
+ }
63
+ }
64
+
65
+ console . log ( 'Version update completed successfully' ) ;
66
+ } catch ( error ) {
67
+ console . error ( 'Error updating versions:' , error . message ) ;
68
+ process . exit ( 1 ) ;
69
+ }
70
+ }
71
+
72
+ /**
73
+ * Recursively finds files with specific extensions in a directory
74
+ * @param {string } dir - Directory to search in
75
+ * @param {string[] } extensions - Array of file extensions to match
76
+ * @returns {Promise<string[]> } Array of file paths
77
+ */
78
+ async function findFiles ( dir , extensions ) {
79
+ const files = [ ] ;
80
+ const entries = await fs . promises . readdir ( dir , { withFileTypes : true } ) ;
81
+
82
+ for ( const entry of entries ) {
83
+ const fullPath = path . join ( dir , entry . name ) ;
84
+
85
+ if ( entry . isDirectory ( ) ) {
86
+ files . push ( ...await findFiles ( fullPath , extensions ) ) ;
87
+ } else if ( entry . isFile ( ) && extensions . some ( ext => entry . name . endsWith ( ext ) ) ) {
88
+ files . push ( fullPath ) ;
89
+ }
90
+ }
91
+
92
+ return files ;
93
+ }
94
+
95
+ // If this file is run directly (not imported)
96
+ if ( require . main === module ) {
97
+ const newVersion = process . argv [ 2 ] ;
98
+ updateVersions ( newVersion ) . catch ( error => {
99
+ console . error ( error . message ) ;
100
+ process . exit ( 1 ) ;
101
+ } ) ;
102
+ }
103
+
104
+ module . exports = updateVersions ;
0 commit comments