summaryrefslogtreecommitdiff
path: root/extension/src/extension.ts
blob: 8f49728216a151b05af3fa1d5df5172b07dc0d67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
 * This is the entry point for the extension.
 */

import * as vscode from "vscode";
import { getExtensionVersion } from "./activation/environmentSetup";
import { getUniqueId } from "./util/vscode";

let client: any = undefined;
export async function capture(args: any) {
  console.log("Capturing posthog event: ", args);
  if (!client) {
    const { PostHog } = await import("posthog-node");
    client = new PostHog("phc_JS6XFROuNbhJtVCEdTSYk6gl5ArRrTNMpCcguAXlSPs", {
      host: "https://app.posthog.com",
    });
  }
  client.capture(args);
}

async function dynamicImportAndActivate(context: vscode.ExtensionContext) {
  if (!context.globalState.get("hasBeenInstalled")) {
    context.globalState.update("hasBeenInstalled", true);
    capture({
      distinctId: getUniqueId(),
      event: "install",
      properties: {
        extensionVersion: getExtensionVersion(),
      },
    });
  }

  const { activateExtension } = await import("./activation/activate");
  try {
    await activateExtension(context);
  } catch (e) {
    console.log("Error activating extension: ", e);
    vscode.window
      .showInformationMessage(
        "Error activating the Continue extension.",
        "View Logs",
        "Retry"
      )
      .then((selection) => {
        if (selection === "View Logs") {
          vscode.commands.executeCommand("sncontinue.viewLogs");
        } else if (selection === "Retry") {
          // Reload VS Code window
          vscode.commands.executeCommand("workbench.action.reloadWindow");
        }
      });
  }
}

export function activate(context: vscode.ExtensionContext) {
  dynamicImportAndActivate(context);
}

export function deactivate() {
  capture({
    distinctId: getUniqueId(),
    event: "deactivate",
    properties: {
      extensionVersion: getExtensionVersion(),
    },
  });

  client?.shutdown();
}