@@ -6,77 +6,62 @@ import {getVariable} from './utils';
66
77const advinstIniUrlVar = 'advancedinstaller_ini_url' ;
88const DEFAULT_ADVINST_INI_URL =
9- 'https://www.advancedinstaller.com/downloads/updates.ini' ;
9+ 'https://www.advancedinstaller.com/downloads/updates-cicd-integration .ini' ;
1010
11- export async function getLatest ( ) : Promise < string > {
12- const versions = await getAll ( ) ;
13- return versions [ 0 ] ;
14- }
11+ export class AdvinstVersions {
12+ private _ini : ConfigIniParser | null = null ;
1513
16- export async function getAll ( ) : Promise < string [ ] > {
17- const versionsFileContent = await _getUpdatesFileContent ( ) ;
14+ private async _getIni ( ) : Promise < ConfigIniParser > {
15+ if ( ! this . _ini ) {
16+ const url = getVariable ( advinstIniUrlVar ) || DEFAULT_ADVINST_INI_URL ;
17+ const filePath = await toolCache . downloadTool ( url ) ;
18+ const content = _readTextFileWithDetectedEncoding ( filePath ) ;
19+ const ini = new ConfigIniParser ( ) ;
20+ ini . parse ( content ) ;
21+ if ( ini . sections ( ) . length === 0 ) {
22+ throw new Error ( 'Invalid updated config file' ) ;
23+ }
24+ this . _ini = ini ;
25+ }
26+ return this . _ini ;
27+ }
1828
19- const ini = new ConfigIniParser ( ) ;
20- ini . parse ( versionsFileContent ) ;
21- const sections = ini . sections ( ) ;
22- if ( sections . length === 0 ) {
23- throw new Error ( 'Invalid updated config file' ) ;
29+ async getAll ( ) : Promise < string [ ] > {
30+ const ini = await this . _getIni ( ) ;
31+ return ini . sections ( ) . map ( s => ini . get ( s , 'ProductVersion' ) ) ;
2432 }
25- const versions : string [ ] = [ ] ;
26- for ( const section of sections ) {
27- versions . push ( ini . get ( section , 'ProductVersion' ) ) ;
33+
34+ async getLatest ( ) : Promise < string > {
35+ return ( await this . getAll ( ) ) [ 0 ] ;
2836 }
29- return versions ;
30- }
3137
32- export async function getMinAllowedAdvinstVersion ( ) : Promise < string | null > {
33- const RELEASE_INTERVAL_MONTHS = 24 ;
38+ async getMinAllowedAdvinstVersion ( ) : Promise < string | null > {
39+ const RELEASE_INTERVAL_MONTHS = 24 ;
40+ const minReleaseDate = new Date ( ) ;
41+ minReleaseDate . setMonth ( minReleaseDate . getMonth ( ) - RELEASE_INTERVAL_MONTHS ) ;
3442
35- const minReleaseDate = new Date ( ) ;
36- minReleaseDate . setMonth ( minReleaseDate . getMonth ( ) - RELEASE_INTERVAL_MONTHS ) ;
43+ const ini = await this . _getIni ( ) ;
44+ const section = ini . sections ( ) . find ( s => {
45+ const [ day , month , year ] = ini . get ( s , 'ReleaseDate' ) . split ( '/' ) ;
46+ return minReleaseDate > new Date ( `${ year } -${ month } -${ day } ` ) ;
47+ } ) ;
3748
38- const versionsFileContent = await _getUpdatesFileContent ( ) ;
39- const ini = new ConfigIniParser ( ) ;
40- ini . parse ( versionsFileContent ) ;
41- const sections = ini . sections ( ) ;
42- if ( sections . length === 0 ) {
43- throw new Error ( 'Invalid updated config file' ) ;
49+ return section ? ini . get ( section , 'ProductVersion' ) : null ;
4450 }
4551
46- const r = ini . sections ( ) . find ( s => {
47- const [ day , month , year ] = ini . get ( s , 'ReleaseDate' ) . split ( '/' ) ;
48- const releaseDate = new Date ( `${ year } -${ month } -${ day } ` ) ;
49- return minReleaseDate > releaseDate ;
50- } ) ;
51-
52- if ( ! r ) {
53- return null ;
52+ async versionIsDeprecated (
53+ version : string
54+ ) : Promise < [ boolean , string | null ] > {
55+ const minAllowedVer = await this . getMinAllowedAdvinstVersion ( ) ;
56+ const isDeprecated =
57+ minAllowedVer !== null && compareVersions ( version , minAllowedVer ) === - 1 ;
58+ return [ isDeprecated , minAllowedVer ] ;
5459 }
55- return ini . get ( r , 'ProductVersion' ) ;
56- }
57-
58- export async function versionIsDeprecated (
59- version : string
60- ) : Promise < [ boolean , string | null ] > {
61- const minAllowedVer = await getMinAllowedAdvinstVersion ( ) ;
62- const isDeprecated : boolean =
63- minAllowedVer !== null && compareVersions ( version , minAllowedVer ) === - 1 ;
64- return [ isDeprecated , minAllowedVer ] ;
65- }
66-
67- async function _getUpdatesFileContent ( ) : Promise < string > {
68- const advinstIniUrl =
69- getVariable ( advinstIniUrlVar ) || DEFAULT_ADVINST_INI_URL ;
70- const updatesFile : string = await toolCache . downloadTool ( advinstIniUrl ) ;
71- return _readTextFileWithDetectedEncoding ( updatesFile ) ;
7260}
7361
7462function _readTextFileWithDetectedEncoding ( filePath : string ) : string {
7563 const raw = fs . readFileSync ( filePath ) ;
76- const encoding = _hasUtf16LeBom ( raw ) ? 'utf16le' : 'utf8' ;
64+ const encoding = raw . length >= 2 && raw [ 0 ] === 0xff && raw [ 1 ] === 0xfe ? 'utf16le' : 'utf8' ;
7765 return raw . toString ( encoding ) ;
7866}
7967
80- function _hasUtf16LeBom ( raw : Buffer ) : boolean {
81- return raw . length >= 2 && raw [ 0 ] === 0xff && raw [ 1 ] === 0xfe ;
82- }
0 commit comments