Skip to content

Commit e19aa6a

Browse files
Dilantha WijayarathneDilantha Wijayarathne
Dilantha Wijayarathne
authored and
Dilantha Wijayarathne
committed
.gh-code file not found in windows env fixed
1 parent d51f451 commit e19aa6a

File tree

3 files changed

+66
-32
lines changed

3 files changed

+66
-32
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
},
7777
"dependencies": {
7878
"axios": "^0.16.2",
79+
"git-label": "^4.1.1",
7980
"git-remote-origin-url": "^2.0.0",
8081
"git-url-parse": "^7.0.1",
8182
"jsonfile": "^4.0.0"

src/Issues.ts

+15-15
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class Issues {
1111
private _rootPath: string;
1212

1313
constructor(rootPath: string) {
14-
this._rootPath = rootPath;
14+
this._rootPath = config.validatePath(rootPath);
1515
}
1616

1717
private async _getMetadata() {
@@ -78,13 +78,13 @@ export class Issues {
7878
}
7979
}
8080

81-
public async getMilestones(){
82-
try{
81+
public async getMilestones() {
82+
try {
8383
let meta = await this._getMetadata();
8484
let apiUrl = api + '/repos/' + meta.user + '/' + meta.repo + '/milestones';
8585
let res = await axios.get(apiUrl);
8686
return res.data;
87-
}catch(e){
87+
} catch (e) {
8888
console.error(e);
8989
return false;
9090
}
@@ -122,7 +122,7 @@ export class Issues {
122122
}
123123

124124
public async removeLabel(label: string, number: number) {
125-
try{
125+
try {
126126
if (config.getToken().length < 1) {
127127
window.showInformationMessage('please add a token or set the password for adding labels');
128128
return false;
@@ -136,14 +136,14 @@ export class Issues {
136136
}
137137
});
138138
return res.status === 200;
139-
}catch(e){
139+
} catch (e) {
140140
console.error(e);
141141
return false;
142142
}
143143
}
144144

145-
public async addComment(comment:string,number:number){
146-
try{
145+
public async addComment(comment: string, number: number) {
146+
try {
147147
if (config.getToken().length < 1) {
148148
window.showInformationMessage('please add a token or set the password for adding labels');
149149
return false;
@@ -153,14 +153,14 @@ export class Issues {
153153
let apiUrl = api + '/repos/' + meta.user + '/' + meta.repo + '/issues/' + number + '/comments';
154154
let res = await axios.post(apiUrl, {
155155
"body": comment
156-
} , {
157-
auth: {
158-
username: meta.user,
159-
password: config.getToken()
160-
}
161-
});
156+
}, {
157+
auth: {
158+
username: meta.user,
159+
password: config.getToken()
160+
}
161+
});
162162
return res.status === 201;
163-
}catch(e){
163+
} catch (e) {
164164
console.error(e);
165165
return false;
166166
}

src/config.ts

+50-17
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,78 @@ const jsonFile = require('jsonfile');
44
const fs = require('fs');
55
const configName = '/.gh-code';
66

7-
export class Config{
7+
export class Config {
88
private _originUrl: string;
9-
private _token:string;
9+
private _token: string;
1010

11-
private _readFile(){
11+
private _readFile() {
1212
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);
1932
}
2033
}
2134

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() {
2351
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+
}
2558
}
2659

27-
constructor(){
60+
constructor() {
2861
this._readFile();
2962
}
3063

31-
public setOriginUrl(url:string){
32-
this._originUrl = url+'';
64+
public setOriginUrl(url: string) {
65+
this._originUrl = url + '';
3366
this._saveFile();
3467
}
3568

36-
public getOriginUrl():string{
69+
public getOriginUrl(): string {
3770
return this._originUrl;
3871
}
3972

40-
public setToken(token:string){
73+
public setToken(token: string) {
4174
this._token = token;
4275
this._saveFile();
4376
}
4477

45-
public getToken():string{
78+
public getToken(): string {
4679
return this._token;
4780
}
4881
}

0 commit comments

Comments
 (0)