Skip to content

Commit 418a7cc

Browse files
committed
add cliInstalledVersion
1 parent abbc713 commit 418a7cc

File tree

1 file changed

+63
-2
lines changed

1 file changed

+63
-2
lines changed

src/main/osinstaller/CxInstaller.ts

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export class CxInstaller {
1313
private readonly platform: string;
1414
private cliVersion: string;
1515
private readonly resourceDirPath: string;
16+
private readonly installedCLIVersionFileName = 'cli-version';
1617
private readonly cliDefaultVersion = '2.2.5'; // This will be used if the version file is not found. Should be updated with the latest version.
1718

1819
constructor(platform: string) {
@@ -47,18 +48,25 @@ export class CxInstaller {
4748
public async downloadIfNotInstalledCLI(): Promise<void> {
4849
try {
4950
await fs.promises.mkdir(this.resourceDirPath, {recursive: true});
51+
const cliVersion = await this.readASTCLIVersion();
5052

5153
if (this.checkExecutableExists()) {
52-
logger.info('Executable already installed.');
53-
return;
54+
const installedVersion = await this.readInstalledVersionFile(this.resourceDirPath);
55+
if (installedVersion === cliVersion) {
56+
logger.info('Executable already installed.');
57+
return;
58+
}
5459
}
60+
61+
await this.cleanDirectoryContents(this.resourceDirPath);
5562
const url = await this.getDownloadURL();
5663
const zipPath = path.join(this.resourceDirPath, this.getCompressFolderName());
5764

5865
await this.downloadFile(url, zipPath);
5966
logger.info('Downloaded CLI to:', zipPath);
6067

6168
await this.extractArchive(zipPath, this.resourceDirPath);
69+
await this.saveVersionFile(this.resourceDirPath, cliVersion);
6270

6371
fs.unlink(zipPath, (err) => {
6472
if (err) {
@@ -75,6 +83,33 @@ export class CxInstaller {
7583
}
7684
}
7785

86+
private async cleanDirectoryContents(directoryPath: string): Promise<void> {
87+
try {
88+
const files = await fsPromises.readdir(directoryPath);
89+
90+
await Promise.all(files.map(async (file) => {
91+
const filePath = path.join(directoryPath, file);
92+
const fileStat = await fsPromises.stat(filePath);
93+
94+
if (fileStat.isDirectory()) {
95+
await fsPromises.rm(filePath, { recursive: true, force: true });
96+
logger.info(`Directory ${filePath} deleted.`);
97+
} else {
98+
await fsPromises.unlink(filePath);
99+
logger.info(`File ${filePath} deleted.`);
100+
}
101+
}));
102+
103+
logger.info(`All contents in ${directoryPath} have been cleaned.`);
104+
} catch (error) {
105+
if (error.code === 'ENOENT') {
106+
logger.info(`Directory at ${directoryPath} does not exist.`);
107+
} else {
108+
logger.error(`Failed to clean directory contents: ${error.message}`);
109+
}
110+
}
111+
}
112+
78113
private async extractArchive(zipPath: string, extractPath: string): Promise<void> {
79114
if (zipPath.endsWith('.zip')) {
80115
await unzipper.Open.file(zipPath)
@@ -86,6 +121,32 @@ export class CxInstaller {
86121
}
87122
}
88123

124+
private async saveVersionFile(resourcePath: string, version: string): Promise<void> {
125+
const versionFilePath = path.join(resourcePath, this.installedCLIVersionFileName);
126+
try {
127+
await fsPromises.writeFile(versionFilePath, `${version}`, 'utf8');
128+
logger.info(`Version file created at ${versionFilePath} with version ${version}`);
129+
} catch (error) {
130+
logger.error(`Failed to create version file: ${error.message}`);
131+
}
132+
}
133+
134+
private async readInstalledVersionFile(resourcePath: string): Promise<string | null> {
135+
const versionFilePath = path.join(resourcePath, this.installedCLIVersionFileName);
136+
try {
137+
const content = await fsPromises.readFile(versionFilePath, 'utf8');
138+
logger.info(`Version file content: ${content}`);
139+
return content;
140+
} catch (error) {
141+
if (error.code === 'ENOENT') {
142+
logger.warn(`Version file not found at ${versionFilePath}.`);
143+
} else {
144+
logger.error(`Failed to read version file: ${error.message}`);
145+
}
146+
return null;
147+
}
148+
}
149+
89150
private async downloadFile(url: string, outputPath: string) {
90151
logger.info('Downloading file from:', url);
91152
const writer = fs.createWriteStream(outputPath);

0 commit comments

Comments
 (0)