diff --git a/src/ts/icon-service/mac-os-app-icon-store.ts b/src/ts/icon-service/mac-os-app-icon-store.ts index 843f2daf3..fdea339d6 100644 --- a/src/ts/icon-service/mac-os-app-icon-store.ts +++ b/src/ts/icon-service/mac-os-app-icon-store.ts @@ -9,7 +9,7 @@ import { AppIconStoreHelpers } from "../helpers/app-icon-store-helpers"; export class MacOsAppIconStore implements AppIconStore { private readonly storePath: string; - private readonly icons: ApplicationIcon[] = []; + private icons: ApplicationIcon[] = []; private readonly iconSet: IconSet; constructor(storePath: string, iconSet: IconSet) { @@ -26,22 +26,40 @@ export class MacOsAppIconStore implements AppIconStore { } public init(searchResultItems: SearchResultItem[]): void { + this.icons = []; + if (!existsSync(this.storePath)) { mkdirSync(this.storePath); } - searchResultItems.filter((searchResultItem: SearchResultItem) => { - return searchResultItem.icon === this.iconSet.appIcon; - }).forEach((searchResultItem: SearchResultItem) => { - const appFilePath = normalize(searchResultItem.executionArgument); - const appName = searchResultItem.name; - const outFilePath = join(this.storePath, `${AppIconStoreHelpers.buildIconFileName(appName)}.png`); - - convert(appFilePath, outFilePath).then(() => { - this.addIcon({ name: appName, PNGFilePath: outFilePath }); - }).catch(() => { - // do nothing + searchResultItems + .filter((searchResultItem: SearchResultItem) => { + return searchResultItem.icon === this.iconSet.appIcon; + }).forEach((searchResultItem: SearchResultItem) => { + const appFilePath = normalize(searchResultItem.executionArgument); + const appName = searchResultItem.name; + const outFilePath = join(this.storePath, `${AppIconStoreHelpers.buildIconFileName(appName)}.png`); + + convert(appFilePath, outFilePath).then(() => { + this.addIcon({ name: appName, PNGFilePath: outFilePath }); + }).catch((err) => { + // tslint:disable-next-line:no-console + console.log(`Error while generating app icon: ${err}`); + }); }); - }); + + this.removeNonExistentIcons(); + } + + private removeNonExistentIcons(): void { + const indexesToDelete = []; + + for (let i = 0; i < this.icons.length; i++) { + if (!existsSync(this.icons[i].PNGFilePath)) { + indexesToDelete.push(i); + } + } + + indexesToDelete.forEach((i) => this.icons.splice(i, 1)); } } diff --git a/src/ts/icon-service/windows-app-icon-store.ts b/src/ts/icon-service/windows-app-icon-store.ts index c32197080..8d08b8ebf 100644 --- a/src/ts/icon-service/windows-app-icon-store.ts +++ b/src/ts/icon-service/windows-app-icon-store.ts @@ -8,7 +8,7 @@ import { AppIconStoreHelpers } from "../helpers/app-icon-store-helpers"; import { existsSync, mkdirSync } from "fs"; export class WindowsAppIconStore implements AppIconStore { - private readonly icons: ApplicationIcon[] = []; + private icons: ApplicationIcon[] = []; private readonly storePath: string; private readonly iconSet: IconSet; @@ -26,6 +26,8 @@ export class WindowsAppIconStore implements AppIconStore { } public init(searchResultItems: SearchResultItem[]): void { + this.icons = []; + if (!existsSync(this.storePath)) { mkdirSync(this.storePath); } @@ -38,29 +40,43 @@ export class WindowsAppIconStore implements AppIconStore { ps.addCommand(`Add-Type -AssemblyName System.Drawing`); - const apps = searchResultItems.filter((searchResultItem: SearchResultItem) => { - return searchResultItem.icon === this.iconSet.appIcon; - }); + searchResultItems + .filter((searchResultItem: SearchResultItem) => { + return searchResultItem.icon === this.iconSet.appIcon; + }).filter((app) => { + const inFilePath = normalize(app.executionArgument); + const appName = app.name; + const outFilePath = join(this.storePath, `${AppIconStoreHelpers.buildIconFileName(appName)}.png`); - for (const app of apps) { - const inFilePath = normalize(app.executionArgument); - const appName = app.name; - const outFilePath = join(this.storePath, `${AppIconStoreHelpers.buildIconFileName(appName)}.png`); + ps.addCommand(`$fileExists = Test-Path -Path "${inFilePath}";`); + ps.addCommand(`if($fileExists) { $icon = [System.Drawing.Icon]::ExtractAssociatedIcon("${inFilePath}"); }`); + ps.addCommand(`if($fileExists) { $bitmap = $icon.ToBitmap().save("${outFilePath}", [System.Drawing.Imaging.ImageFormat]::Png); }`); - ps.addCommand(`$icon = [System.Drawing.Icon]::ExtractAssociatedIcon("${inFilePath}")`); - ps.addCommand(`$bitmap = $icon.ToBitmap().save("${outFilePath}", [System.Drawing.Imaging.ImageFormat]::Png);`); - - this.addIcon({ name: app.name, PNGFilePath: outFilePath }); - } + this.addIcon({ name: app.name, PNGFilePath: outFilePath }); + }); ps.invoke().then(() => { // tslint:disable-next-line:no-console console.log("Sucessfully generated all app icons"); + this.removeNonExistentIcons(); ps.dispose(); }).catch((err) => { // tslint:disable-next-line:no-console console.log(`Error while generating app icon: ${err}`); + this.removeNonExistentIcons(); ps.dispose(); }); } + + private removeNonExistentIcons(): void { + const indexesToDelete = []; + + for (let i = 0; i < this.icons.length; i++) { + if (!existsSync(this.icons[i].PNGFilePath)) { + indexesToDelete.push(i); + } + } + + indexesToDelete.forEach((i) => this.icons.splice(i, 1)); + } }