Skip to content

Commit

Permalink
firebase token file sync (#801)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimacodota authored Apr 3, 2022
1 parent 0a034fe commit 98b9fad
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
5 changes: 5 additions & 0 deletions src/authentication/authentication.api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { tabNineProcess } from "../binary/requests/requests";
import openLogin from "../cloudEnvs/openLogin";
import isCloudEnv from "../cloudEnvs/isCloudEnv";

export function callForLogin(): Promise<unknown> {
if (isCloudEnv) {
return openLogin();
}
return tabNineProcess.request({ Login: {} });
}

Expand Down
2 changes: 1 addition & 1 deletion src/cloudEnvs/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as os from "os";

export const TABNINE_CONFIG_DIR = path.join(os.homedir(), ".config", "TabNine");

export const TABNINE_TOKEN_FILE_NAME = "tabnine.token";
export const TABNINE_TOKEN_FILE_NAME = ".refresh_token_v2";

export const TABNINE_TOKEN_FILE_PATH = path.join(
TABNINE_CONFIG_DIR,
Expand Down
14 changes: 14 additions & 0 deletions src/cloudEnvs/openLogin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { env, Uri } from "vscode";
import { configuration } from "../binary/requests/requests";
import { StateType } from "../globals/consts";
import openHub from "../hub/openHub";

export default async function openLogin(): Promise<void> {
const config = await configuration({ quiet: true, source: StateType.AUTH });
if (config && config.message) {
const localUri = await env.asExternalUri(Uri.parse(config.message));
const callback = `https://app.tabnine.com/auth/sign-in?tabnineUrl=${localUri.toString()}&sync=false`;
await openHub(localUri);
void env.openExternal(Uri.parse(callback));
}
}
33 changes: 20 additions & 13 deletions src/cloudEnvs/setupCloudState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,27 @@ function persistStateToCloudEnv(context: ExtensionContext): void {
watch(consts.TABNINE_CONFIG_DIR, (event, filename) => {
switch (filename) {
case consts.TABNINE_TOKEN_FILE_NAME:
void fsPromises
.readFile(consts.TABNINE_TOKEN_FILE_PATH, "utf8")
.then((tabnineToken) =>
context.globalState.update(
consts.TABNINE_TOKEN_CONTEXT_KEY,
tabnineToken
if (event === "rename") {
void context.globalState.update(
consts.TABNINE_TOKEN_CONTEXT_KEY,
null
);
} else {
void fsPromises
.readFile(consts.TABNINE_TOKEN_FILE_PATH, "utf8")
.then((tabnineToken) =>
context.globalState.update(
consts.TABNINE_TOKEN_CONTEXT_KEY,
tabnineToken
)
)
)
.catch((e) => {
console.error(
"Error occurred while trying to persist Tabnine token",
e
);
});
.catch((e) => {
console.error(
"Error occurred while trying to persist Tabnine token",
e
);
});
}
break;
case consts.TABNINE_CONFIG_FILE_NAME:
void fsPromises
Expand Down
4 changes: 3 additions & 1 deletion src/hub/openHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { sleep } from "../utils/utils";
import hub from "./hub";

let panel: WebviewPanel | undefined;
let waitForServerReadyDelay = SLEEP_TIME_BEFORE_OPEN_HUB;

export default async function openHub(
uri: Uri,
Expand Down Expand Up @@ -42,9 +43,10 @@ export default async function openHub(

panel.iconPath = Uri.file(path.resolve(__dirname, "..", "small_logo.png"));

if (SLEEP_TIME_BEFORE_OPEN_HUB > 0) {
if (waitForServerReadyDelay > 0) {
panel.webview.html = setLoading();
await sleep(SLEEP_TIME_BEFORE_OPEN_HUB);
waitForServerReadyDelay = 0;
}
panel.webview.html = setUrl(uri.toString());
}
Expand Down

0 comments on commit 98b9fad

Please sign in to comment.