|
4 | 4 | * For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0 |
5 | 5 | */ |
6 | 6 |
|
7 | | -import {findDwJson, resolveConfig} from '@salesforce/b2c-tooling-sdk/config'; |
8 | 7 | import type {B2CInstance} from '@salesforce/b2c-tooling-sdk/instance'; |
9 | 8 | import type {Library} from '@salesforce/b2c-tooling-sdk/operations/content'; |
10 | | -import * as fs from 'fs'; |
11 | | -import * as vscode from 'vscode'; |
| 9 | +import type {B2CExtensionConfig} from '../config-provider.js'; |
12 | 10 |
|
13 | 11 | export interface BrowsedLibrary { |
14 | 12 | id: string; |
15 | 13 | isSiteLibrary: boolean; |
16 | 14 | } |
17 | 15 |
|
18 | 16 | export class ContentConfigProvider { |
19 | | - private instance: B2CInstance | null = null; |
20 | | - private configError: string | null = null; |
21 | | - private resolved = false; |
22 | 17 | private libraries: BrowsedLibrary[] = []; |
23 | 18 | private libraryCache = new Map<string, Library>(); |
24 | | - private contentLibrary: string | undefined; |
| 19 | + |
| 20 | + constructor(private readonly configProvider: B2CExtensionConfig) { |
| 21 | + configProvider.onDidReset(() => { |
| 22 | + this.libraryCache.clear(); |
| 23 | + }); |
| 24 | + } |
25 | 25 |
|
26 | 26 | getInstance(): B2CInstance | null { |
27 | | - if (!this.resolved) { |
28 | | - this.resolve(); |
29 | | - } |
30 | | - return this.instance; |
| 27 | + return this.configProvider.getInstance(); |
31 | 28 | } |
32 | 29 |
|
33 | 30 | getConfigError(): string | null { |
34 | | - if (!this.resolved) { |
35 | | - this.resolve(); |
36 | | - } |
37 | | - return this.configError; |
| 31 | + return this.configProvider.getConfigError(); |
38 | 32 | } |
39 | 33 |
|
40 | 34 | getContentLibrary(): string | undefined { |
41 | | - if (!this.resolved) { |
42 | | - this.resolve(); |
43 | | - } |
44 | | - return this.contentLibrary; |
| 35 | + return this.configProvider.getConfig()?.values.contentLibrary; |
45 | 36 | } |
46 | 37 |
|
47 | 38 | getLibraries(): BrowsedLibrary[] { |
@@ -76,36 +67,6 @@ export class ContentConfigProvider { |
76 | 67 | } |
77 | 68 |
|
78 | 69 | reset(): void { |
79 | | - this.instance = null; |
80 | | - this.configError = null; |
81 | | - this.resolved = false; |
82 | 70 | this.libraryCache.clear(); |
83 | 71 | } |
84 | | - |
85 | | - private resolve(): void { |
86 | | - this.resolved = true; |
87 | | - try { |
88 | | - let workingDirectory = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath ?? process.cwd(); |
89 | | - if (!workingDirectory || workingDirectory === '/' || !fs.existsSync(workingDirectory)) { |
90 | | - workingDirectory = ''; |
91 | | - } |
92 | | - const dwPath = workingDirectory ? findDwJson(workingDirectory) : undefined; |
93 | | - const config = dwPath ? resolveConfig({}, {configPath: dwPath}) : resolveConfig({}, {workingDirectory}); |
94 | | - |
95 | | - this.contentLibrary = config.values.contentLibrary; |
96 | | - |
97 | | - if (!config.hasB2CInstanceConfig()) { |
98 | | - this.configError = 'No B2C Commerce instance configured.'; |
99 | | - this.instance = null; |
100 | | - return; |
101 | | - } |
102 | | - |
103 | | - this.instance = config.createB2CInstance(); |
104 | | - this.configError = null; |
105 | | - } catch (err) { |
106 | | - const message = err instanceof Error ? err.message : String(err); |
107 | | - this.configError = message; |
108 | | - this.instance = null; |
109 | | - } |
110 | | - } |
111 | 72 | } |
0 commit comments