1
+ const https = require ( 'https' ) ;
2
+
3
+ /**
4
+ * Fetches the latest Lotus version from GitHub releases
5
+ * @returns {Promise<string> } The latest version number without the 'v' prefix
6
+ * @throws {Error } If the version cannot be fetched or parsed
7
+ */
8
+ async function getLatestLotusVersion ( ) {
9
+ return new Promise ( ( resolve , reject ) => {
10
+ const options = {
11
+ hostname : 'api.github.com' ,
12
+ path : '/repos/filecoin-project/lotus/releases/latest' ,
13
+ headers : {
14
+ 'User-Agent' : 'Node.js'
15
+ }
16
+ } ;
17
+
18
+ https . get ( options , ( res ) => {
19
+ let data = '' ;
20
+
21
+ res . on ( 'data' , ( chunk ) => {
22
+ data += chunk ;
23
+ } ) ;
24
+
25
+ res . on ( 'end' , ( ) => {
26
+ try {
27
+ const releaseInfo = JSON . parse ( data ) ;
28
+ const tagName = releaseInfo . tag_name ;
29
+
30
+ if ( ! tagName || ! tagName . startsWith ( 'v' ) || tagName . includes ( 'miner' ) ) {
31
+ throw new Error ( 'Could not find a valid tag in the release info' ) ;
32
+ }
33
+
34
+ // Remove 'v' prefix
35
+ const version = tagName . substring ( 1 ) ;
36
+ resolve ( version ) ;
37
+ } catch ( error ) {
38
+ reject ( new Error ( `Failed to parse release info: ${ error . message } ` ) ) ;
39
+ }
40
+ } ) ;
41
+ } ) . on ( 'error' , ( error ) => {
42
+ reject ( new Error ( `Failed to fetch release info: ${ error . message } ` ) ) ;
43
+ } ) ;
44
+ } ) ;
45
+ }
46
+
47
+ // If this file is run directly (not imported)
48
+ if ( require . main === module ) {
49
+ // Test mode
50
+ if ( process . argv [ 2 ] === '--test' ) {
51
+ console . log ( 'Testing getLatestLotusVersion function...' ) ;
52
+ getLatestLotusVersion ( )
53
+ . then ( version => {
54
+ console . log ( 'Success! Latest version:' , version ) ;
55
+ } )
56
+ . catch ( error => {
57
+ console . error ( 'Test failed:' , error . message ) ;
58
+ process . exit ( 1 ) ;
59
+ } ) ;
60
+ } else {
61
+ // Normal mode - just output the version
62
+ getLatestLotusVersion ( )
63
+ . then ( version => console . log ( version ) )
64
+ . catch ( error => {
65
+ console . error ( error . message ) ;
66
+ process . exit ( 1 ) ;
67
+ } ) ;
68
+ }
69
+ }
70
+
71
+ // Export the function for use in other files
72
+ module . exports = getLatestLotusVersion ;
0 commit comments