1
1
import * as path from "path" ;
2
2
import * as os from "os" ;
3
- import { promises as fs } from "fs" ;
3
+ import fs , { promises as fsPromises } from "fs" ;
4
4
import { setEnvVar } from "./cli" ;
5
5
import { fromBase64 , toBase64 } from "../utils/utils" ;
6
- import { asyncExists } from "../utils/file.utils" ;
7
6
8
7
const TABNINE_TOKEN_FILE = path . join (
9
8
os . homedir ( ) ,
@@ -28,37 +27,37 @@ export async function loadStateFromGitpodEnvVar(): Promise<void> {
28
27
const tabnineConfig = process . env [ TABNINE_CONFIG_ENV_VAR ] ;
29
28
30
29
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
+ } ) ;
36
35
}
37
36
38
37
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
+ } ) ;
44
43
}
45
44
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
+ } ) ;
64
63
}
0 commit comments