1+ import * as fsPromises from 'fs/promises' ;
12import * as fs from 'fs' ;
23import * as path from 'path' ;
34import * as tar from 'tar' ;
@@ -11,16 +12,19 @@ type SupportedPlatforms = 'win32' | 'darwin' | 'linux';
1112
1213export class CxInstaller {
1314 private readonly platform : string ;
14- private cliVersion = '2.2.5' ;
15+ private cliVersion : string ;
1516 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.
1618 private static installSemaphore = new Semaphore ( 1 ) ; // Semaphore with 1 slot
1719
1820 constructor ( platform : string ) {
1921 this . platform = platform ;
2022 this . resourceDirPath = path . join ( __dirname , `../wrapper/resources` ) ;
2123 }
2224
23- getDownloadURL ( ) : string {
25+ async getDownloadURL ( ) : Promise < string > {
26+ const cliVersion = await this . readASTCLIVersion ( ) ;
27+
2428 const platforms : Record < SupportedPlatforms , { platform : string ; extension : string } > = {
2529 win32 : { platform : 'windows' , extension : 'zip' } ,
2630 darwin : { platform : 'darwin' , extension : 'tar.gz' } ,
@@ -34,7 +38,7 @@ export class CxInstaller {
3438 throw new Error ( 'Unsupported platform or architecture' ) ;
3539 }
3640
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 } ` ;
3842 }
3943
4044 getExecutablePath ( ) : string {
@@ -52,7 +56,7 @@ export class CxInstaller {
5256 logger . info ( 'Executable already installed.' ) ;
5357 return ;
5458 }
55- const url = this . getDownloadURL ( ) ;
59+ const url = await this . getDownloadURL ( ) ;
5660 const zipPath = path . join ( this . resourceDirPath , this . getCompressFolderName ( ) ) ;
5761
5862 await this . downloadFile ( url , zipPath ) ;
@@ -101,6 +105,20 @@ export class CxInstaller {
101105 checkExecutableExists ( ) : boolean {
102106 return fs . existsSync ( this . getExecutablePath ( ) ) ;
103107 }
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+ }
104122
105123 getCompressFolderName ( ) : string {
106124 return `ast-cli.${ this . platform === 'win32' ? 'zip' : 'tar.gz' } ` ;
0 commit comments