@@ -4,45 +4,78 @@ const jsonFile = require('jsonfile');
4
4
const fs = require ( 'fs' ) ;
5
5
const configName = '/.gh-code' ;
6
6
7
- export class Config {
7
+ export class Config {
8
8
private _originUrl : string ;
9
- private _token :string ;
9
+ private _token : string ;
10
10
11
- private _readFile ( ) {
11
+ private _readFile ( ) {
12
12
const file = workspace . workspaceFolders [ 0 ] . uri . path + configName ;
13
- if ( fs . existsSync ( file ) ) {
14
- let obj = jsonFile . readFileSync ( file ) ;
15
- this . _originUrl = obj . originUrl ;
16
- this . _token = obj . token ;
17
- } else {
18
- jsonFile . writeFileSync ( file , { originUrl :'' , token :'' } ) ;
13
+ const windowsFile = this . _windowsPath ( file ) ;
14
+ try {
15
+ if ( fs . existsSync ( file ) ) {
16
+ let obj = jsonFile . readFileSync ( file ) ;
17
+ this . _originUrl = obj . originUrl ;
18
+ this . _token = obj . token ;
19
+ } else if ( windowsFile ) {
20
+ let obj = jsonFile . readFileSync ( windowsFile ) ;
21
+ this . _originUrl = obj . originUrl ;
22
+ this . _token = obj . token ;
23
+ } else {
24
+ try {
25
+ jsonFile . writeFileSync ( file , { originUrl : '' , token : '' } ) ;
26
+ } catch ( ex ) {
27
+ jsonFile . writeFileSync ( windowsFile , { originUrl : '' , token : '' } ) ;
28
+ }
29
+ }
30
+ } catch ( ex ) {
31
+ console . error ( ex ) ;
19
32
}
20
33
}
21
34
22
- private _saveFile ( ) {
35
+ public validatePath ( filePath : string ) {
36
+ if ( fs . existsSync ( filePath ) ) {
37
+ return filePath ;
38
+ } else {
39
+ return this . _windowsPath ( filePath ) ;
40
+ }
41
+ }
42
+
43
+ private _windowsPath ( filePath : string ) {
44
+ filePath = filePath . split ( '/' ) . join ( '\\' ) ;
45
+ filePath = filePath . replace ( '\\' , '' ) ;
46
+ filePath = filePath . charAt ( 0 ) . toUpperCase ( ) + filePath . substr ( 1 ) ;
47
+ return filePath ;
48
+ }
49
+
50
+ private _saveFile ( ) {
23
51
const file = workspace . workspaceFolders [ 0 ] . uri . path + configName ;
24
- jsonFile . writeFileSync ( file , { originUrl :this . _originUrl , token :this . _token } ) ;
52
+ const windowsFile = this . _windowsPath ( file ) ;
53
+ try {
54
+ jsonFile . writeFileSync ( file , { originUrl : this . _originUrl , token : this . _token } ) ;
55
+ } catch ( ex ) {
56
+ jsonFile . writeFileSync ( windowsFile , { originUrl : this . _originUrl , token : this . _token } ) ;
57
+ }
25
58
}
26
59
27
- constructor ( ) {
60
+ constructor ( ) {
28
61
this . _readFile ( ) ;
29
62
}
30
63
31
- public setOriginUrl ( url :string ) {
32
- this . _originUrl = url + '' ;
64
+ public setOriginUrl ( url : string ) {
65
+ this . _originUrl = url + '' ;
33
66
this . _saveFile ( ) ;
34
67
}
35
68
36
- public getOriginUrl ( ) :string {
69
+ public getOriginUrl ( ) : string {
37
70
return this . _originUrl ;
38
71
}
39
72
40
- public setToken ( token :string ) {
73
+ public setToken ( token : string ) {
41
74
this . _token = token ;
42
75
this . _saveFile ( ) ;
43
76
}
44
77
45
- public getToken ( ) :string {
78
+ public getToken ( ) : string {
46
79
return this . _token ;
47
80
}
48
81
}
0 commit comments