Skip to content

Commit 1db31f2

Browse files
committed
feat: replace simple-git with octokit to load fiddle
1 parent caa1f8d commit 1db31f2

File tree

6 files changed

+102
-40
lines changed

6 files changed

+102
-40
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@
3030
},
3131
"dependencies": {
3232
"@electron/get": "^2.0.0",
33+
"@octokit/rest": "^19.0.4",
3334
"debug": "^4.3.3",
3435
"env-paths": "^2.2.1",
3536
"extract-zip": "^2.0.1",
3637
"fs-extra": "^10.0.0",
3738
"getos": "^3.2.1",
3839
"node-fetch": "^2.6.1",
39-
"semver": "^7.3.5",
40-
"simple-git": "^2.48.0"
40+
"semver": "^7.3.5"
4141
},
4242
"devDependencies": {
4343
"@continuous-auth/semantic-release-npm": "^3.0.0",

src/fiddle.ts

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import * as fs from 'fs-extra';
22
import * as path from 'path';
33
import debug from 'debug';
4-
import simpleGit from 'simple-git';
4+
import fetch from 'node-fetch';
55
import { createHash } from 'crypto';
66

77
import { DefaultPaths } from './paths';
8+
import { getOctokit } from './utils/octokit';
89

910
function hashString(str: string): string {
1011
const md5sum = createHash('md5');
@@ -32,10 +33,41 @@ export class Fiddle {
3233
export type FiddleSource = Fiddle | string | Iterable<[string, string]>;
3334

3435
export class FiddleFactory {
36+
private readonly VALID_FILES: Array<string> = [
37+
'main.js',
38+
'renderer.js',
39+
'index.html',
40+
'preload.js',
41+
'styles.css',
42+
];
43+
// Thanks to https://serverfault.com/a/917253
44+
private readonly GITHUB_URL_REGEX = new RegExp(
45+
'^(https|git)(://|@)([^/:]+)[/:]([^/:]+)/(.+).git$',
46+
);
47+
3548
constructor(private readonly fiddles: string = DefaultPaths.fiddles) {}
3649

37-
public async fromGist(gistId: string): Promise<Fiddle> {
38-
return this.fromRepo(`https://gist.github.com/${gistId}.git`);
50+
public async fromGist(gistId: string) {
51+
// stores in format [filename, content]
52+
const gistContents: [string, string][] = [];
53+
const octokit = getOctokit(process.env.FIDDLE_CORE_GITHUB_TOKEN);
54+
const gist = await octokit.gists.get({ gist_id: gistId });
55+
56+
if (gist.data.files === undefined) {
57+
return;
58+
}
59+
// TODO(aryanshridhar): Avoid repetitive undefined/null checks
60+
for (const [, data] of Object.entries(gist.data.files)) {
61+
const fileName = data?.filename;
62+
if (fileName !== undefined && this.VALID_FILES.includes(fileName)) {
63+
if (data?.content === undefined) {
64+
continue;
65+
}
66+
gistContents.push([fileName, data.content]);
67+
}
68+
}
69+
70+
return this.fromEntries(gistContents);
3971
}
4072

4173
public async fromFolder(source: string): Promise<Fiddle> {
@@ -55,23 +87,44 @@ export class FiddleFactory {
5587
return new Fiddle(path.join(folder, 'main.js'), source);
5688
}
5789

58-
public async fromRepo(url: string, checkout = 'master'): Promise<Fiddle> {
90+
public async fromRepo(url: string) {
5991
const d = debug('fiddle-core:FiddleFactory:fromRepo');
60-
const folder = path.join(this.fiddles, hashString(url));
61-
d({ url, checkout, folder });
62-
63-
// get the repo
64-
if (!fs.existsSync(folder)) {
65-
d(`cloning "${url}" into "${folder}"`);
66-
const git = simpleGit();
67-
await git.clone(url, folder, { '--depth': 1 });
92+
const match = this.GITHUB_URL_REGEX.exec(url);
93+
if (match === null) {
94+
throw new Error(`Invalid github URL`);
95+
}
96+
// TODO (aryanshridhar): better error handling in case of array out
97+
// of bounds (not sure). This has to be done because octokit expects
98+
// an owner and repo keys to be passed instead of just git link.
99+
const user = match[4];
100+
const repo = match[5];
101+
const repoContents: [string, string][] = [];
102+
103+
d({ url, user, repo });
104+
const octokit = getOctokit(process.env.FIDDLE_CORE_GITHUB_TOKEN);
105+
const folder = await octokit.repos.getContent({
106+
owner: user,
107+
repo: repo,
108+
path: '.', // Look for in the base directory only
109+
});
110+
111+
if (!Array.isArray(folder.data)) {
112+
return;
68113
}
69114

70-
const git = simpleGit(folder);
71-
await git.checkout(checkout);
72-
await git.pull('origin', checkout);
115+
for (const file of folder.data) {
116+
if (!this.VALID_FILES.includes(file.name)) {
117+
continue;
118+
}
119+
120+
if (file.download_url) {
121+
const resp = await fetch(file.download_url);
122+
const content = await resp.text();
123+
repoContents.push([file.name, content]);
124+
}
125+
}
73126

74-
return new Fiddle(path.join(folder, 'main.js'), url);
127+
return this.fromEntries(repoContents);
75128
}
76129

77130
public async fromEntries(src: Iterable<[string, string]>): Promise<Fiddle> {

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
compareVersions,
2020
} from './versions';
2121
import { runFromCommandLine } from './command-line';
22+
import { setGithubToken, removeGithubToken } from './utils/env';
2223

2324
export {
2425
BaseVersions,
@@ -41,6 +42,8 @@ export {
4142
TestResult,
4243
Versions,
4344
compareVersions,
45+
setGithubToken,
46+
removeGithubToken,
4447
runFromCommandLine,
4548
};
4649

src/utils/env.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function setGithubToken(githubToken: string) {
2+
process.env.FIDDLE_CORE_GITHUB_TOKEN = githubToken;
3+
}
4+
5+
export function removeGithubToken() {
6+
delete process.env.FIDDLE_CORE_GITHUB_TOKEN;
7+
}

src/utils/octokit.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Octokit } from '@octokit/rest';
2+
3+
let _octo: Octokit;
4+
/**
5+
* Returns a loaded Octokit. If state is passed and authentication
6+
* is available, we'll token-authenticate.
7+
* @returns {Octokit}
8+
*/
9+
export function getOctokit(token?: string): Octokit {
10+
// It's possible to load Gists without being authenticated,
11+
// but we get better rate limits when authenticated.
12+
_octo =
13+
_octo || token
14+
? new Octokit({
15+
auth: token,
16+
})
17+
: new Octokit();
18+
19+
return _octo;
20+
}

yarn.lock

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -789,18 +789,6 @@
789789
"@types/yargs" "^16.0.0"
790790
chalk "^4.0.0"
791791

792-
"@kwsites/file-exists@^1.1.1":
793-
version "1.1.1"
794-
resolved "https://registry.yarnpkg.com/@kwsites/file-exists/-/file-exists-1.1.1.tgz#ad1efcac13e1987d8dbaf235ef3be5b0d96faa99"
795-
integrity sha512-m9/5YGR18lIwxSFDwfE3oA7bWuq9kdau6ugN4H2rJeyhFQZcG9AgSHkQtSD15a8WvTgfz9aikZMrKPHvbpqFiw==
796-
dependencies:
797-
debug "^4.1.1"
798-
799-
"@kwsites/promise-deferred@^1.1.1":
800-
version "1.1.1"
801-
resolved "https://registry.yarnpkg.com/@kwsites/promise-deferred/-/promise-deferred-1.1.1.tgz#8ace5259254426ccef57f3175bc64ed7095ed919"
802-
integrity sha512-GaHYm+c0O9MjZRu0ongGBRbinu8gVAMd2UZjji6jVmqKtZluZnptXGWhz1E8j8D2HJ3f/yMxKAUC0b+57wncIw==
803-
804792
"@microsoft/[email protected]":
805793
version "7.15.3"
806794
resolved "https://registry.yarnpkg.com/@microsoft/api-extractor-model/-/api-extractor-model-7.15.3.tgz#cf76deeeb2733d974da678f530c2dbaceb18a065"
@@ -1119,7 +1107,7 @@
11191107
node-fetch "^2.6.7"
11201108
universal-user-agent "^6.0.0"
11211109

1122-
"@octokit/rest@^19.0.0":
1110+
"@octokit/rest@^19.0.0", "@octokit/rest@^19.0.4":
11231111
version "19.0.4"
11241112
resolved "https://registry.yarnpkg.com/@octokit/rest/-/rest-19.0.4.tgz#fd8bed1cefffa486e9ae46a9dc608ce81bcfcbdd"
11251113
integrity sha512-LwG668+6lE8zlSYOfwPj4FxWdv/qFXYBpv79TWIQEpBLKA9D/IMcWsF/U9RGpA3YqMVDiTxpgVpEW3zTFfPFTA==
@@ -6101,15 +6089,6 @@ signale@^1.2.1:
61016089
figures "^2.0.0"
61026090
pkg-conf "^2.1.0"
61036091

6104-
simple-git@^2.48.0:
6105-
version "2.48.0"
6106-
resolved "https://registry.yarnpkg.com/simple-git/-/simple-git-2.48.0.tgz#87c262dba8f84d7b96bb3a713e9e34701c1f6e3b"
6107-
integrity sha512-z4qtrRuaAFJS4PUd0g+xy7aN4y+RvEt/QTJpR184lhJguBA1S/LsVlvE/CM95RsYMOFJG3NGGDjqFCzKU19S/A==
6108-
dependencies:
6109-
"@kwsites/file-exists" "^1.1.1"
6110-
"@kwsites/promise-deferred" "^1.1.1"
6111-
debug "^4.3.2"
6112-
61136092
sisteransi@^1.0.5:
61146093
version "1.0.5"
61156094
resolved "https://registry.yarnpkg.com/sisteransi/-/sisteransi-1.0.5.tgz#134d681297756437cc05ca01370d3a7a571075ed"

0 commit comments

Comments
 (0)