1
+ import * as fsPromises from 'fs/promises' ;
1
2
import * as fs from 'fs' ;
2
3
import * as path from 'path' ;
3
4
import * as tar from 'tar' ;
@@ -11,16 +12,19 @@ type SupportedPlatforms = 'win32' | 'darwin' | 'linux';
11
12
12
13
export class CxInstaller {
13
14
private readonly platform : string ;
14
- private cliVersion = '2.2.5' ;
15
+ private cliVersion : string ;
15
16
private readonly resourceDirPath : string ;
17
+ private readonly cliDefaultVersion = '2.2.5' ; // This will be used if the version file is not found. Should be updated with the latest version.
16
18
private static installSemaphore = new Semaphore ( 1 ) ; // Semaphore with 1 slot
17
19
18
20
constructor ( platform : string ) {
19
21
this . platform = platform ;
20
22
this . resourceDirPath = path . join ( __dirname , `../wrapper/resources` ) ;
21
23
}
22
24
23
- getDownloadURL ( ) : string {
25
+ async getDownloadURL ( ) : Promise < string > {
26
+ const cliVersion = await this . readASTCLIVersion ( ) ;
27
+
24
28
const platforms : Record < SupportedPlatforms , { platform : string ; extension : string } > = {
25
29
win32 : { platform : 'windows' , extension : 'zip' } ,
26
30
darwin : { platform : 'darwin' , extension : 'tar.gz' } ,
@@ -34,7 +38,7 @@ export class CxInstaller {
34
38
throw new Error ( 'Unsupported platform or architecture' ) ;
35
39
}
36
40
37
- return `https://download.checkmarx.com/CxOne/CLI/${ this . cliVersion } /ast-cli_${ this . cliVersion } _${ platformData . platform } _x64.${ platformData . extension } ` ;
41
+ return `https://download.checkmarx.com/CxOne/CLI/${ cliVersion } /ast-cli_${ cliVersion } _${ platformData . platform } _x64.${ platformData . extension } ` ;
38
42
}
39
43
40
44
getExecutablePath ( ) : string {
@@ -52,7 +56,7 @@ export class CxInstaller {
52
56
logger . info ( 'Executable already installed.' ) ;
53
57
return ;
54
58
}
55
- const url = this . getDownloadURL ( ) ;
59
+ const url = await this . getDownloadURL ( ) ;
56
60
const zipPath = path . join ( this . resourceDirPath , this . getCompressFolderName ( ) ) ;
57
61
58
62
await this . downloadFile ( url , zipPath ) ;
@@ -101,6 +105,20 @@ export class CxInstaller {
101
105
checkExecutableExists ( ) : boolean {
102
106
return fs . existsSync ( this . getExecutablePath ( ) ) ;
103
107
}
108
+
109
+ async readASTCLIVersion ( ) : Promise < string > {
110
+ if ( this . cliVersion ) {
111
+ return this . cliVersion ;
112
+ }
113
+ try {
114
+ const versionFilePath = path . join ( process . cwd ( ) , 'checkmarx-ast-cli.version' ) ;
115
+ const versionContent = await fsPromises . readFile ( versionFilePath , 'utf-8' ) ;
116
+ return versionContent . trim ( ) ;
117
+ } catch ( error ) {
118
+ logger . error ( 'Error reading AST CLI version: ' + error . message ) ;
119
+ return this . cliDefaultVersion ;
120
+ }
121
+ }
104
122
105
123
getCompressFolderName ( ) : string {
106
124
return `ast-cli.${ this . platform === 'win32' ? 'zip' : 'tar.gz' } ` ;
0 commit comments