Skip to content

Commit 5f70c5a

Browse files
committed
bugs fixes
1 parent 8b4056e commit 5f70c5a

7 files changed

Lines changed: 88 additions & 38 deletions

File tree

.vscode/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"dart.lineLength": 50,
3+
"prettier.printWidth": 50,
4+
}

icon.png

11.3 KB
Loading

plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"id": "acode.plugin.github",
33
"name": "Github",
44
"main": "dist/main.js",
5-
"version": "1.0.2",
5+
"version": "1.0.3",
66
"readme": "readme.md",
77
"icon": "icon.png",
88
"files": [],

readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ Access your gist/repositories without cloning/downloading it. To access search `
1212

1313
## Updates
1414

15-
- **1.0.2**: Create file, directory in repository.
15+
- **1.0.2**: Create/Delete file, directory in repository.
16+
- **1.0.3**: Bugs fixes.

src/GitHubAPI/Requestable.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,14 @@ class Requestable {
173173
data = undefined;
174174
}
175175

176-
const { url: pureUrl, query = '' } = Url.parse(url);
177-
const dateTime = `_datetime=${new Date().getTime()}`;
178-
const newQuery = query ? `${query}&${dateTime}` : dateTime;
176+
let newUrl = url;
179177

180-
const newUrl = `${pureUrl}?${newQuery}`;
178+
if (method === 'GET') {
179+
const { url: pureUrl, query = '' } = Url.parse(url);
180+
const dateTime = `_datetime=${new Date().getTime()}`;
181+
const newQuery = query ? `${query}&${dateTime}` : `?${dateTime}`;
182+
newUrl = `${pureUrl}${newQuery}`;
183+
}
181184

182185
const config = {
183186
url: newUrl,

src/githubFs.js

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ githubFs.constructUrl = (type, user, repo, path, branch) => {
3636
};
3737

3838
export default function githubFs(token) {
39-
4039
fsOperation.extend(test, (url) => {
4140
const { user, type, repo, path, gist } = parseUrl(url);
4241
if (type === 'repo') {
@@ -89,9 +88,9 @@ export default function githubFs(token) {
8988
* @returns
9089
*/
9190
function readRepo(user, repoAtBranch, path) {
92-
const gh = new GitHub({ token });
91+
let gh;
92+
let repo;
9393
const [repoName, branch] = repoAtBranch.split('@');
94-
const repo = gh.getRepo(user, repoName);
9594
let sha = '';
9695
const getSha = async () => {
9796
if (!sha) {
@@ -100,8 +99,23 @@ export default function githubFs(token) {
10099
}
101100
};
102101

102+
const init = async () => {
103+
if (gh) return;
104+
gh = new GitHub({ token: await token() });
105+
repo = gh.getRepo(user, repoName);
106+
}
107+
108+
const move = async (dest) => {
109+
const newUrl = githubFs.constructUrl('repo', user, repoName, dest, branch);
110+
if (dest === path) return newUrl;
111+
if (dest.startsWith('/')) dest = dest.slice(1);
112+
await repo.move(branch, path, dest);
113+
return newUrl;
114+
}
115+
103116
return {
104117
async lsDir() {
118+
await init();
105119
const res = await repo.getSha(branch, path);
106120
const { data } = res;
107121

@@ -115,6 +129,7 @@ export default function githubFs(token) {
115129
});
116130
},
117131
async readFile(encoding) {
132+
await init();
118133
await getSha();
119134
let { data } = await repo.getBlob(sha, 'blob');
120135
data = await data.arrayBuffer();
@@ -129,6 +144,7 @@ export default function githubFs(token) {
129144
return data;
130145
},
131146
async writeFile(data) {
147+
await init();
132148
await repo.writeFile(branch, path, data, `update ${path}`);
133149
},
134150
async createFile(name, data = '') {
@@ -149,6 +165,7 @@ export default function githubFs(token) {
149165
return githubFs.constructUrl('repo', user, repoName, newPath, branch);
150166
},
151167
async createDirectory(dirname) {
168+
await init();
152169
let newPath = path === '' ? dirname : Url.join(path, dirname);
153170
// check if file exists
154171
let sha;
@@ -170,18 +187,25 @@ export default function githubFs(token) {
170187
throw new Error('Not implemented');
171188
},
172189
async delete() {
190+
await init();
173191
await getSha();
174192
await repo.deleteFile(branch, path, `delete ${path}`, sha);
175193
},
176194
async moveTo(dest) {
177-
throw new Error('Not implemented');
195+
await init();
196+
const { path: destPath } = parseUrl(dest);
197+
const newName = Url.join(destPath, Url.basename(path));
198+
const res = await move(newName);
199+
return res;
178200
},
179201
async renameTo(name) {
180-
// rename file
181-
await getSha();
182-
await repo.move(branch, path, name, 'rename file', sha);
202+
await init();
203+
const newName = Url.join(Url.dirname(path), name);
204+
const res = await move(newName);
205+
return res;
183206
},
184207
async exists() {
208+
await init();
185209
try {
186210
await repo.getSha(branch, path);
187211
return true;
@@ -190,6 +214,7 @@ export default function githubFs(token) {
190214
}
191215
},
192216
async stat() {
217+
await init();
193218
await getSha();
194219
const content = await repo.getBlob(sha);
195220
return {
@@ -205,20 +230,27 @@ export default function githubFs(token) {
205230

206231
function readGist(gistId, path) {
207232
let file;
208-
const gh = new GitHub({ token });
209-
const gist = gh.getGist(gistId);
233+
let gh;
234+
let gist;
210235
const getFile = async () => {
211236
if (!file) {
212237
const { data } = await gist.read();
213238
file = data.files[path];
214239
}
215240
return file;
216241
}
242+
const init = async () => {
243+
if (gh) return;
244+
gh = new GitHub({ token: await token() });
245+
gist = gh.getGist(gistId);
246+
}
247+
217248
return {
218249
async lsDir() {
219250
throw new Error('Not implemented');
220251
},
221252
async readFile(encoding, progress) {
253+
await init();
222254
let { content: data } = await getFile();
223255
const textEncoder = new TextEncoder();
224256
data = textEncoder.encode(file.content);
@@ -230,6 +262,7 @@ export default function githubFs(token) {
230262
return data;
231263
},
232264
async writeFile(data) {
265+
await init();
233266
await gist.update({
234267
files: {
235268
[path]: {
@@ -257,9 +290,11 @@ export default function githubFs(token) {
257290
throw new Error('Not implemented');
258291
},
259292
async exists() {
293+
await init();
260294
return !!await getFile();
261295
},
262296
async stat() {
297+
await init();
263298
await getFile();
264299
return {
265300
length: file.size,

src/main.js

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,22 @@ class AcodePlugin {
3636
});
3737

3838
this.token = await this.#cacheFile.readFile('utf8');
39-
if (this.token) {
40-
await this.initFs();
41-
}
39+
await this.initFs();
4240
}
4341

4442
async initFs() {
4543
if (this.#fsInitialized) return;
46-
if (!this.token) {
47-
await this.updateToken();
48-
}
49-
5044
githubFs.remove();
51-
githubFs(this.token);
45+
githubFs(this.getToken.bind(this));
5246
this.#fsInitialized = true;
5347
}
5448

49+
async getToken() {
50+
if (this.token) return this.token;
51+
await this.updateToken();
52+
return this.token;
53+
}
54+
5555
async destroy() {
5656
githubFs.remove();
5757
this.commands.forEach(command => {
@@ -61,6 +61,7 @@ class AcodePlugin {
6161

6262
async openRepo() {
6363
await this.initFs();
64+
this.token = await this.getToken();
6465
pallete(
6566
this.listRepositories.bind(this),
6667
this.selectBranch.bind(this),
@@ -72,7 +73,8 @@ class AcodePlugin {
7273
const [user, repoName] = repo.split('/');
7374
pallete(
7475
this.listBranches.bind(this, user, repoName),
75-
(branch) => this.openRepoAsFolder(user, repoName, branch),
76+
(branch) => this.openRepoAsFolder(user, repoName, branch)
77+
.catch(helpers.error),
7678
'Type to search branch',
7779
);
7880
}
@@ -89,7 +91,7 @@ class AcodePlugin {
8991
const confirmation = await confirm(strings['warning'], 'Delete this gist?');
9092
if (!confirmation) return;
9193

92-
const gh = new GitHub({ token: this.token });
94+
const gh = await this.#GitHub();
9395
const gistApi = gh.getGist(gist);
9496
await gistApi.delete();
9597
this.#gists = this.#gists.filter(g => g.id !== gist);
@@ -117,7 +119,7 @@ class AcodePlugin {
117119
const confirmation = await confirm(strings['warning'], 'Delete this file?');
118120
if (!confirmation) return;
119121

120-
const gh = new GitHub({ token: this.token });
122+
const gh = await this.#GitHub();
121123
const gistApi = gh.getGist(gist);
122124
await gistApi.update({
123125
files: {
@@ -129,8 +131,8 @@ class AcodePlugin {
129131
window.toast('File deleted');
130132
}
131133

132-
async openRepoAsFolder(user, repo, branch) {
133-
const cachedRepo = this.#getRepo(user, repo);
134+
async openRepoAsFolder(user, repoName, branch) {
135+
const cachedRepo = this.#getRepo(user, repoName);
134136
if (branch === this.NEW) {
135137
const { from, branch: newBranch } = await multiPrompt(
136138
strings['create new branch'],
@@ -150,8 +152,8 @@ class AcodePlugin {
150152
}],
151153
);
152154
branch = newBranch;
153-
const gh = new GitHub({ token: this.token });
154-
const repo = gh.getRepo(user, repo);
155+
const gh = await this.#GitHub();
156+
const repo = gh.getRepo(user, repoName);
155157
await repo.createBranch(from, newBranch);
156158
}
157159

@@ -160,14 +162,15 @@ class AcodePlugin {
160162
return;
161163
}
162164

163-
const url = githubFs.constructUrl('repo', user, repo, '/', branch);
165+
const url = githubFs.constructUrl('repo', user, repoName, '/', branch);
164166
openFolder(url, {
165-
name: `${user}/${repo}/${branch}`,
167+
name: `${user}/${repoName}/${branch}`,
166168
});
167169
}
168170

169171
async openGist() {
170172
await this.initFs();
173+
this.token = await this.getToken();
171174

172175
pallete(
173176
this.listGists.bind(this),
@@ -215,7 +218,7 @@ class AcodePlugin {
215218
});
216219

217220
helpers.showTitleLoader();
218-
const gh = new GitHub({ token: this.token });
221+
const gh = await this.#GitHub();
219222
const gist = gh.getGist();
220223
const { data } = await gist.create({
221224
description,
@@ -244,7 +247,7 @@ class AcodePlugin {
244247
window.toast(strings['cancelled']);
245248
}
246249
helpers.showTitleLoader();
247-
const gh = new GitHub({ token: this.token });
250+
const gh = await this.#GitHub();
248251
await gh.getGist(gist).update({
249252
files: {
250253
[filename]: {
@@ -298,7 +301,7 @@ class AcodePlugin {
298301
if (this.#repos.length) {
299302
return [...this.#repos];
300303
}
301-
const gh = new GitHub({ token: this.token });
304+
const gh = await this.#GitHub();
302305
const user = gh.getUser();
303306
const repos = await user.listRepos();
304307
const { data } = repos;
@@ -323,7 +326,7 @@ class AcodePlugin {
323326
if (cachedRepo && cachedRepo.branches) {
324327
list = [...cachedRepo.branches];
325328
} else {
326-
const gh = new GitHub({ token: this.token });
329+
const gh = await this.#GitHub();
327330
const repo = gh.getRepo(user, repoName);
328331
const branches = await repo.listBranches();
329332
const { data } = branches;
@@ -358,7 +361,7 @@ class AcodePlugin {
358361
if (this.#gists.length) {
359362
list = [...this.#gists];
360363
} else {
361-
const gh = new GitHub({ token: this.token });
364+
const gh = await this.#GitHub();
362365
const user = gh.getUser();
363366
const gists = await user.listGists();
364367
const { data } = gists;
@@ -384,7 +387,7 @@ class AcodePlugin {
384387
if (cachedGist && cachedGist.files) {
385388
list = [...cachedGist.files];
386389
} else {
387-
const gh = new GitHub({ token: this.token });
390+
const gh = await this.#GitHub();
388391
const gist = gh.getGist(gistId);
389392
const { data: { files, owner } } = await gist.read();
390393

@@ -433,6 +436,10 @@ class AcodePlugin {
433436
return this.#gists.find(gist => gist.value === gistId);
434437
}
435438

439+
async #GitHub() {
440+
return new GitHub({ token: await this.getToken() });
441+
}
442+
436443
get commands() {
437444
return [
438445
{

0 commit comments

Comments
 (0)