Skip to content

Commit 7b34ace

Browse files
committed
v1.2.0
1 parent 2b161d3 commit 7b34ace

4 files changed

Lines changed: 102 additions & 13 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "acode-plugin-github",
3-
"version": "1.1.3",
3+
"version": "1.2.0",
44
"description": "Github plugin for acode editor",
55
"main": "dist/main.js",
66
"repository": "https://github.com/deadlyjack/acode-plugin-github.git",
@@ -34,4 +34,4 @@
3434
"terser": ">=5.16.6",
3535
"glob-parent": ">=5.1.2"
3636
}
37-
}
37+
}

plugin.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
"id": "acode.plugin.github",
33
"name": "Github",
44
"main": "dist/main.js",
5-
"version": "1.1.7",
5+
"version": "1.2.0",
66
"readme": "readme.md",
77
"icon": "icon.png",
8+
"minVersionCode": 292,
89
"files": [],
910
"author": {
1011
"name": "Ajit Kumar",

readme.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ Access your gist/repositories without cloning/downloading it. To access search `
1212

1313
## Updates
1414

15-
- **1.0.2**: Create/Delete file, directory in repository.
16-
- **1.0.3**: Bugs fixes.
17-
- **1.1.2**: Ask for commit message.
18-
- **1.1.3**: Updated to work with latest acode.
15+
- **1.2.0**
16+
- Upload non text file using 'insert-file' option.
17+
- Updated api according to latest acode.
18+
- **1.1.3**
19+
- Updated to work with latest acode.
20+
- **1.1.2**
21+
- Ask for commit message.
22+
- **1.0.3**
23+
- Bugs fixes.
24+
- **1.0.2**
25+
- Create/Delete file, directory in repository.

src/githubFs.js

Lines changed: 87 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import GitHub from './GitHubAPI/GitHub';
22
import { lookup } from 'mime-types';
3+
import Repository from './GitHubAPI/Repository';
4+
import Gist from './GitHubAPI/Gist';
35

4-
const fsOperation = acode.require('fsoperation');
56
const Url = acode.require('url');
7+
const fsOperation = acode.require('fs') || acode.require('fsOperation');
68
const helpers = acode.require('helpers');
79
const prompt = acode.require('prompt');
10+
const encodings = acode.require('encodings');
11+
812
const test = (url) => /^gh:/.test(url);
913

1014
githubFs.remove = () => {
@@ -108,7 +112,9 @@ export default function githubFs(token, settings) {
108112
* @returns
109113
*/
110114
function readRepo(user, repoAtBranch, path) {
115+
/**@type {GitHub} */
111116
let gh;
117+
/**@type {Repository} */
112118
let repo;
113119
const [repoName, branch] = repoAtBranch.split('@');
114120
let sha = '';
@@ -156,23 +162,51 @@ export default function githubFs(token, settings) {
156162
data = await data.arrayBuffer();
157163

158164
if (encoding) {
165+
if (encodings?.decode) {
166+
const decoded = await encodings.decode(data, encoding);
167+
if (decoded) return decoded;
168+
}
169+
170+
/**@deprecated just for backward compatibility */
159171
return helpers.decodeText(data, encoding);
160172
}
161173

162174
return data;
163175
},
164-
async writeFile(data) {
176+
async writeFile(data, encoding) {
165177
if (!path) throw new Error('Cannot write to root directory')
166178
const commitMessage = await getCommitMessage(`update ${path}`);
167179
if (!commitMessage) return;
180+
181+
let encode = true;
182+
183+
if (encoding) {
184+
if (data instanceof ArrayBuffer && encodings?.decode) {
185+
data = await encodings.decode(data, encoding);
186+
}
187+
188+
if (encoding && encodings?.encode) {
189+
data = await encodings.encode(data, encoding);
190+
}
191+
192+
if (data instanceof ArrayBuffer && encodings?.decode) {
193+
data = await encodings.decode(data, encoding);
194+
}
195+
} else if (data instanceof ArrayBuffer) {
196+
// convert to base64
197+
data = await bufferToBase64(data);
198+
encode = false;
199+
}
200+
168201
await init();
169-
await repo.writeFile(branch, path, data, commitMessage);
202+
await repo.writeFile(branch, path, data, commitMessage, { encode });
170203
},
171204
async createFile(name, data = '') {
172205
await init();
173206
const newPath = path === '' ? name : Url.join(path, name);
174207
// check if file exists
175208
let sha;
209+
let encode = true;
176210
try {
177211
sha = await repo.getSha(branch, newPath);
178212
} catch (e) {
@@ -183,9 +217,15 @@ export default function githubFs(token, settings) {
183217
throw new Error('File already exists');
184218
}
185219

220+
if (data instanceof ArrayBuffer) {
221+
// convert to base64
222+
data = await bufferToBase64(data);
223+
encode = false;
224+
}
225+
186226
const commitMessage = await getCommitMessage(`create ${newPath}`);
187227
if (!commitMessage) return;
188-
await repo.writeFile(branch, newPath, data, commitMessage);
228+
await repo.writeFile(branch, newPath, data, commitMessage, { encode });
189229
return githubFs.constructUrl('repo', user, repoName, newPath, branch);
190230
},
191231
async createDirectory(dirname) {
@@ -269,8 +309,11 @@ export default function githubFs(token, settings) {
269309
}
270310

271311
function readGist(gistId, path) {
312+
/**@type {string} */
272313
let file;
314+
/**@type {GitHub} */
273315
let gh;
316+
/**@type {Gist} */
274317
let gist;
275318
const getFile = async () => {
276319
if (!file) {
@@ -289,20 +332,42 @@ export default function githubFs(token, settings) {
289332
async lsDir() {
290333
throw new Error('Not implemented');
291334
},
292-
async readFile(encoding, progress) {
335+
async readFile(encoding) {
293336
await init();
294337
let { content: data } = await getFile();
295338
const textEncoder = new TextEncoder();
296339
data = textEncoder.encode(file.content);
297340

298341
if (encoding) {
342+
if (encodings?.decode) {
343+
const decoded = await encodings.decode(data, encoding);
344+
if (decoded) return decoded;
345+
}
346+
299347
return helpers.decodeText(data, encoding);
300348
}
301349

302350
return data;
303351
},
304-
async writeFile(data) {
352+
async writeFile(data, encoding) {
305353
await init();
354+
355+
encoding = settings.value.defaultFileEncoding || 'utf-8';
356+
357+
if (encoding) {
358+
if (data instanceof ArrayBuffer && encodings?.decode) {
359+
data = await encodings.decode(data, encoding);
360+
}
361+
362+
if (encoding && encodings?.encode) {
363+
data = await encodings.encode(data, encoding);
364+
}
365+
366+
if (data instanceof ArrayBuffer && encodings?.decode) {
367+
data = await encodings.decode(data, encoding);
368+
}
369+
}
370+
306371
await gist.update({
307372
files: {
308373
[path]: {
@@ -347,3 +412,19 @@ export default function githubFs(token, settings) {
347412
}
348413
}
349414
}
415+
416+
async function bufferToBase64(buffer) {
417+
const blob = new Blob([buffer]);
418+
const reader = new FileReader();
419+
420+
reader.readAsDataURL(blob);
421+
return new Promise((resolve, reject) => {
422+
reader.onloadend = () => {
423+
// strip off the data: url prefix
424+
const content = reader.result.slice(reader.result.indexOf(',') + 1);
425+
resolve(content);
426+
};
427+
428+
reader.onerror = reject;
429+
});
430+
}

0 commit comments

Comments
 (0)