|
1 | | -import * as vscode from 'vscode'; |
| 1 | +import { WorkspaceConfiguration, workspace, ExtensionContext } from 'vscode'; |
2 | 2 |
|
3 | | -export const DEFAULT_BASE_URI = 'https://app.launchdarkly.com'; |
4 | | -export const DEFAULT_STREAM_URI = 'https://stream.launchdarkly.com'; |
| 3 | +const package_json = require('../package.json'); |
5 | 4 |
|
6 | | -export interface IConfiguration { |
7 | | - /** |
8 | | - * Your LaunchDarkly API access token with reader-level permissions. Required. |
9 | | - */ |
10 | | - accessToken: string; |
| 5 | +const DEFAULT_BASE_URI = 'https://app.launchdarkly.com'; |
| 6 | +const DEFAULT_STREAM_URI = 'https://stream.launchdarkly.com'; |
11 | 7 |
|
12 | | - /** |
13 | | - * Your LaunchDarkly SDK key. Required. |
14 | | - */ |
15 | | - sdkKey: string; |
| 8 | +export class Configuration { |
| 9 | + private readonly ctx: ExtensionContext; |
| 10 | + accessToken = ''; |
| 11 | + sdkKey = ''; |
| 12 | + project = ''; |
| 13 | + env = ''; |
| 14 | + enableHover = true; |
| 15 | + enableAutocomplete = true; |
| 16 | + baseUri = DEFAULT_BASE_URI; |
| 17 | + streamUri = DEFAULT_STREAM_URI; |
16 | 18 |
|
17 | | - /** |
18 | | - * Your LaunchDarkly project key, should match the provided SDK key. Required. |
19 | | - */ |
20 | | - project: string; |
| 19 | + constructor(ctx: ExtensionContext) { |
| 20 | + this.ctx = ctx; |
| 21 | + this.reload(); |
| 22 | + } |
21 | 23 |
|
22 | | - /** |
23 | | - * Your LaunchDarkly environment key, should match the provided SDK key. |
24 | | - */ |
25 | | - env: string; |
| 24 | + reload() { |
| 25 | + const config = workspace.getConfiguration('launchdarkly'); |
| 26 | + for (const option in this) { |
| 27 | + if (option === 'ctx') { |
| 28 | + continue; |
| 29 | + } |
| 30 | + this[option] = config.get(option); |
| 31 | + } |
26 | 32 |
|
27 | | - /** |
28 | | - * Enables flag info to be displayed on hover of a valid flag key. |
29 | | - */ |
30 | | - enableHover: boolean; |
| 33 | + // If accessToken is configured in state, use it. Otherwise, fall back to the legacy access token. |
| 34 | + this.accessToken = this.getState('accessToken') || this.accessToken; |
| 35 | + } |
31 | 36 |
|
32 | | - /** |
33 | | - * Enable flag key autocompletion. |
34 | | - */ |
35 | | - enableAutocomplete: boolean; |
| 37 | + async update(key: string, value: string | boolean, global: boolean) { |
| 38 | + if (typeof this[key] !== typeof value) { |
| 39 | + return; |
| 40 | + } |
36 | 41 |
|
37 | | - /** |
38 | | - * The LaunchDarkly base uri to be used. Optional. |
39 | | - */ |
40 | | - baseUri: string; |
| 42 | + let config: WorkspaceConfiguration = workspace.getConfiguration('launchdarkly'); |
| 43 | + if (key === 'accessToken') { |
| 44 | + const ctxState = global ? this.ctx.globalState : this.ctx.workspaceState; |
| 45 | + await ctxState.update(key, value); |
| 46 | + await config.update(key, '', global); |
| 47 | + return; |
| 48 | + } |
41 | 49 |
|
42 | | - /** |
43 | | - * The LaunchDarkly stream uri to be used. Optional. |
44 | | - */ |
45 | | - streamUri: string; |
46 | | -} |
| 50 | + await config.update(key, value, global); |
| 51 | + config = workspace.getConfiguration('launchdarkly'); |
47 | 52 |
|
48 | | -class Configuration implements IConfiguration { |
49 | | - constructor() { |
50 | | - this.reload(); |
| 53 | + this[key] = value; |
| 54 | + process.nextTick(function() {}); |
51 | 55 | } |
52 | 56 |
|
53 | | - reload() { |
54 | | - let config: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration('launchdarkly'); |
55 | | - for (const option in this) { |
56 | | - this[option] = config[option]; |
| 57 | + validate(): string { |
| 58 | + const version = package_json.version; |
| 59 | + const ctx = this.ctx; |
| 60 | + ctx.globalState.update('version', undefined); |
| 61 | + const storedVersion = ctx.globalState.get('version'); |
| 62 | + |
| 63 | + if (version !== storedVersion) { |
| 64 | + ctx.globalState.update('version', version); |
| 65 | + } |
| 66 | + |
| 67 | + const legacyConfiguration = !!this.sdkKey; |
| 68 | + if (legacyConfiguration && !ctx.globalState.get('legacyNotificationDismissed')) { |
| 69 | + return 'legacy'; |
| 70 | + } |
| 71 | + |
| 72 | + // Only recommend configuring the extension on install and update |
| 73 | + const configured = !!this.accessToken; |
| 74 | + if (version != storedVersion && !configured) { |
| 75 | + return 'unconfigured'; |
57 | 76 | } |
58 | 77 | } |
59 | 78 |
|
60 | | - accessToken = ''; |
61 | | - sdkKey = ''; |
62 | | - project = ''; |
63 | | - env = ''; |
64 | | - enableHover = true; |
65 | | - enableAutocomplete = true; |
66 | | - baseUri = DEFAULT_BASE_URI; |
67 | | - streamUri = DEFAULT_STREAM_URI; |
| 79 | + getState(key: string): string { |
| 80 | + return this.ctx.workspaceState.get(key) || this.ctx.globalState.get(key); |
| 81 | + } |
68 | 82 | } |
69 | | - |
70 | | -export const configuration = new Configuration(); |
|
0 commit comments