11"use strict"
22
33import { getUser } from "coder/site/src/api/api"
4- import { readFileSync } from "fs"
54import * as module from "module"
6- import path from "path"
75import * as vscode from "vscode"
86import { Commands } from "./commands"
97import { Remote } from "./remote"
108import { Storage } from "./storage"
119
1210export async function activate ( ctx :vscode . ExtensionContext ) :Promise < void > {
13- const productJSON = readFileSync ( path . join ( vscode . env . appRoot , "product.json" ) )
14- const product = JSON . parse ( productJSON . toString ( ) )
15- const commit = product . commit
1611const output = vscode . window . createOutputChannel ( "Coder" )
17- const storage = new Storage ( output , ctx . globalState , ctx . globalStorageUri )
18- storage . init ( )
12+ const storage = new Storage ( output , ctx . globalState , ctx . secrets , ctx . globalStorageUri , ctx . logUri )
13+ await storage . init ( )
1914
2015getUser ( )
2116. then ( ( ) => {
@@ -45,13 +40,16 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
4540vscode . commands . registerCommand ( "coder.logout" , commands . logout . bind ( commands ) )
4641vscode . commands . registerCommand ( "coder.open" , commands . open . bind ( commands ) )
4742
48- // The remote SSH extension is required to provide the restricted
49- // proposed API for registering remote authority providers.
43+ // The Remote SSH extension's proposed APIs are used to override
44+ // the SSH host name in VS Code itself. It's visually unappealing
45+ // having a lengthy name!
46+ //
47+ // This is janky, but that's alright since it provides such minimal
48+ // functionality to the extension.
5049const remoteSSHExtension = vscode . extensions . getExtension ( "ms-vscode-remote.remote-ssh" )
5150if ( ! remoteSSHExtension ) {
5251throw new Error ( "Remote SSH extension not found" )
5352}
54-
5553// eslint-disable-next-line @typescript-eslint/no-explicit-any
5654const vscodeProposed :typeof vscode = ( module as any ) . _load (
5755"vscode" ,
@@ -61,8 +59,18 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
6159false ,
6260)
6361
64- const remote = new Remote ( output , vscodeProposed , storage , commit )
65- ctx . subscriptions . push ( remote )
62+ // Since the "onResolveRemoteAuthority:ssh-remote" activation event exists
63+ // in package.json we're able to perform actions before the authority is
64+ // resolved by the remote SSH extension.
65+ const activeRemotes = vscode . workspace . workspaceFolders ?. filter ( ( folder ) => folder . uri . scheme === "vscode-remote" )
66+ // If the currently opened folder isn't remote we can return early!
67+ if ( activeRemotes ?. length !== 1 ) {
68+ return
69+ }
70+ const activeRemote = activeRemotes [ 0 ] . uri
71+
72+ ctx . globalStorageUri
6673
67- vscodeProposed . workspace . registerRemoteAuthorityResolver ( "coder" , remote )
74+ const remote = new Remote ( vscodeProposed , storage , ctx . extensionMode )
75+ await remote . setup ( activeRemote )
6876}