Skip to content

Commit a3a33b4

Browse files
committed
watch for token changes
1 parent d8b55d7 commit a3a33b4

File tree

7 files changed

+31
-48
lines changed

7 files changed

+31
-48
lines changed

.vscodeignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
**
22
!out/
33
!out/extension.js
4-
!out/uninstall.js
54
!small_logo.png
65
!package.json
76
!logo.svg

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@
9494
"scripts": {
9595
"analyze:bundle": "webpack --env analyzeBundle",
9696
"vscode:prepublish": "yarn --frozen-lockfile && yarn clear-out && webpack --mode production",
97-
"vscode:uninstall":"node ./out/uninstall.js",
9897
"compile": "webpack --mode development --watch",
9998
"build": "webpack --mode development",
10099
"test:copyassets": "ncp ./src/test/fixture ./out/test/fixture/",

src/extension.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,13 @@ import {
4040
loadStateFromGitpodEnvVar,
4141
persistStateToGitpodEnvVar,
4242
} from "./gitpod/state";
43-
import { onSigTerm } from "./utils/utils";
4443

4544
export async function activate(
4645
context: vscode.ExtensionContext
4746
): Promise<void> {
4847
if (isGitpod) {
49-
void loadStateFromGitpodEnvVar();
50-
onSigTerm(() => {
51-
void persistStateToGitpodEnvVar();
52-
});
48+
await loadStateFromGitpodEnvVar();
49+
void persistStateToGitpodEnvVar();
5350
}
5451
void initStartup(context);
5552
handleSelection(context);
@@ -110,7 +107,6 @@ async function backgroundInit(context: vscode.ExtensionContext) {
110107
}
111108

112109
export async function deactivate(): Promise<unknown> {
113-
if (isGitpod) await persistStateToGitpodEnvVar();
114110
disposeReporter();
115111
void closeValidator();
116112
cancelNotificationsPolling();

src/gitpod/state.ts

+29-30
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import * as path from "path";
22
import * as os from "os";
3-
import { promises as fs } from "fs";
3+
import fs, { promises as fsPromises } from "fs";
44
import { setEnvVar } from "./cli";
55
import { fromBase64, toBase64 } from "../utils/utils";
6-
import { asyncExists } from "../utils/file.utils";
76

87
const TABNINE_TOKEN_FILE = path.join(
98
os.homedir(),
@@ -28,37 +27,37 @@ export async function loadStateFromGitpodEnvVar(): Promise<void> {
2827
const tabnineConfig = process.env[TABNINE_CONFIG_ENV_VAR];
2928

3029
if (tabnineToken) {
31-
try {
32-
await fs.writeFile(TABNINE_TOKEN_FILE, fromBase64(tabnineToken));
33-
} catch (e) {
34-
console.error("Error occurred while trying to load Tabnine token", e);
35-
}
30+
await fsPromises
31+
.writeFile(TABNINE_TOKEN_FILE, fromBase64(tabnineToken))
32+
.catch((e) => {
33+
console.error("Error occurred while trying to load Tabnine token", e);
34+
});
3635
}
3736

3837
if (tabnineConfig)
39-
try {
40-
await fs.writeFile(TABNINE_CONFIG_FILE, fromBase64(tabnineConfig));
41-
} catch (e) {
42-
console.error("Error occurred while trying to load Tabnine config", e);
43-
}
38+
await fsPromises
39+
.writeFile(TABNINE_CONFIG_FILE, fromBase64(tabnineConfig))
40+
.catch((e) => {
41+
console.error("Error occurred while trying to load Tabnine config", e);
42+
});
4443
}
4544

46-
export async function persistStateToGitpodEnvVar(): Promise<void> {
47-
if (await asyncExists(TABNINE_TOKEN_FILE)) {
48-
try {
49-
const tabnineToken = await fs.readFile(TABNINE_TOKEN_FILE, "utf8");
50-
await setEnvVar(TABNINE_TOKEN_ENV_VAR, toBase64(tabnineToken));
51-
} catch (e) {
52-
console.error("Error occurred while trying to persist Tabnine token", e);
53-
}
54-
}
55-
56-
if (await asyncExists(TABNINE_CONFIG_FILE)) {
57-
try {
58-
const tabnineConfig = await fs.readFile(TABNINE_CONFIG_FILE, "utf8");
59-
await setEnvVar(TABNINE_CONFIG_ENV_VAR, toBase64(tabnineConfig));
60-
} catch (e) {
61-
console.error("Error occurred while trying to persist Tabnine config", e);
62-
}
63-
}
45+
export function persistStateToGitpodEnvVar(): void {
46+
fs.watch(TABNINE_TOKEN_FILE, (event, filename) => {
47+
if (event === "change")
48+
void fsPromises
49+
.readFile(filename, "utf8")
50+
.then((tabnineToken) =>
51+
setEnvVar(TABNINE_TOKEN_ENV_VAR, toBase64(tabnineToken))
52+
);
53+
});
54+
55+
fs.watch(TABNINE_TOKEN_FILE, (event, filename) => {
56+
if (event === "change")
57+
void fsPromises
58+
.readFile(filename, "utf8")
59+
.then((tabnineConfig) =>
60+
setEnvVar(TABNINE_CONFIG_ENV_VAR, toBase64(tabnineConfig))
61+
);
62+
});
6463
}

src/scripts/uninstall.ts

-5
This file was deleted.

src/utils/utils.ts

-4
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,3 @@ export function fromBase64(str: string): string {
6767
export function toBase64(str: string): string {
6868
return Buffer.from(str, "utf8").toString("base64");
6969
}
70-
71-
export function onSigTerm(callback: () => void): void {
72-
process.on("SIGTERM", callback);
73-
}

webpack.config.js

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const config = {
1212

1313
entry: {
1414
extension: "./src/extension.ts",
15-
uninstall: "./src/scripts/uninstall.ts",
1615
}, // the entry point of this extension, 📖 -> https://webpack.js.org/configuration/entry-context/
1716
output: {
1817
// the bundle is stored in the 'dist' folder (check package.json), 📖 -> https://webpack.js.org/configuration/output/

0 commit comments

Comments
 (0)