Passed
Push — trunk ( c4454b...a860c5 )
by Christian
14:18 queued 12s
created

ExtensionHelperService.getStatusOfExtension   A

Complexity

Conditions 2

Size

Total Lines 17
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 15
dl 0
loc 17
rs 9.65
c 0
b 0
f 0
1
// eslint-disable-next-line max-len
2
import type { ExtensionStoreActionService, ExtensionType } from '../../module/sw-extension/service/extension-store-action.service';
3
4
/**
5
 * @package merchant-services
6
 * @deprecated tag:v6.6.0 - Will be private
7
 */
8
// eslint-disable-next-line sw-deprecation-rules/private-feature-declarations
9
export default class ExtensionHelperService {
10
    private readonly extensionStoreActionService: ExtensionStoreActionService;
11
12
    constructor({ extensionStoreActionService }: { extensionStoreActionService: ExtensionStoreActionService }) {
13
        this.extensionStoreActionService = extensionStoreActionService;
14
    }
15
16
    async downloadAndActivateExtension(extensionName: string, type: ExtensionType = 'plugin') {
17
        const extensionStatus = await this.getStatusOfExtension(extensionName);
18
19
        if (!extensionStatus.downloaded) {
20
            await this.downloadStoreExtension(extensionName);
21
        }
22
23
        if (!extensionStatus.installedAt) {
24
            await this.installStoreExtension(extensionName, type);
25
        }
26
27
        if (!extensionStatus.active) {
28
            await this.activateStoreExtension(extensionName, type);
29
        }
30
    }
31
32
    downloadStoreExtension(extensionName: string) {
33
        return this.extensionStoreActionService.downloadExtension(extensionName);
34
    }
35
36
    installStoreExtension(extensionName: string, type: ExtensionType) {
37
        return this.extensionStoreActionService.installExtension(extensionName, type);
38
    }
39
40
    activateStoreExtension(extensionName: string, type: ExtensionType) {
41
        return this.extensionStoreActionService.activateExtension(extensionName, type);
42
    }
43
44
    async getStatusOfExtension(extensionName: string) {
45
        const extensions = await this.extensionStoreActionService.getMyExtensions();
46
        const extension = extensions.find(e => e && e.name === extensionName);
47
48
        if (!extension) {
49
            return {
50
                downloaded: false,
51
                installedAt: false,
52
                active: false,
53
            };
54
        }
55
56
        return {
57
            downloaded: extension.source === 'local',
58
            installedAt: extension.installedAt,
59
            active: extension.active,
60
        };
61
    }
62
}
63
64
/**
65
 * @private
66
 */
67
export type { ExtensionHelperService };
68