11import * as fs from 'fs-extra' ;
22import * as path from 'path' ;
33import debug from 'debug' ;
4- import simpleGit from 'simple-git ' ;
4+ import fetch from 'node-fetch ' ;
55import { createHash } from 'crypto' ;
66
77import { DefaultPaths } from './paths' ;
8+ import { getOctokit } from './utils/octokit' ;
89
910function hashString ( str : string ) : string {
1011 const md5sum = createHash ( 'md5' ) ;
@@ -32,10 +33,43 @@ export class Fiddle {
3233export type FiddleSource = Fiddle | string | Iterable < [ string , string ] > ;
3334
3435export 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+
60+ for ( const [ , data ] of Object . entries ( gist . data . files ) ) {
61+ const fileName = data ?. filename ;
62+ const content = data ?. content ;
63+
64+ if ( fileName === undefined || content === undefined ) {
65+ continue ;
66+ }
67+ if ( this . VALID_FILES . includes ( fileName ) ) {
68+ gistContents . push ( [ fileName , content ] ) ;
69+ }
70+ }
71+
72+ return this . fromEntries ( gistContents ) ;
3973 }
4074
4175 public async fromFolder ( source : string ) : Promise < Fiddle > {
@@ -55,23 +89,43 @@ export class FiddleFactory {
5589 return new Fiddle ( path . join ( folder , 'main.js' ) , source ) ;
5690 }
5791
58- public async fromRepo ( url : string , checkout = 'master' ) : Promise < Fiddle > {
92+ public async fromRepo ( url : string ) {
5993 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 } ) ;
94+ const match = this . GITHUB_URL_REGEX . exec ( url ) ;
95+ if ( match === null ) {
96+ throw new Error ( `Invalid github URL` ) ;
97+ }
98+ // This has to be done because octokit expects an owner and repo
99+ // params to be passed instead of just HTTPs/SSH git link.
100+ const owner = match [ 4 ] ;
101+ const repo = match [ 5 ] ;
102+ const repoContents : [ string , string ] [ ] = [ ] ;
103+
104+ d ( { url, owner, repo } ) ;
105+ const octokit = getOctokit ( process . env . FIDDLE_CORE_GITHUB_TOKEN ) ;
106+ const folder = await octokit . repos . getContent ( {
107+ owner,
108+ repo,
109+ path : '.' , // Look for in the base directory only
110+ } ) ;
111+
112+ if ( ! Array . isArray ( folder . data ) ) {
113+ return ;
68114 }
69115
70- const git = simpleGit ( folder ) ;
71- await git . checkout ( checkout ) ;
72- await git . pull ( 'origin' , checkout ) ;
116+ for ( const file of folder . data ) {
117+ if ( ! this . VALID_FILES . includes ( file . name ) ) {
118+ continue ;
119+ }
120+
121+ if ( file . download_url ) {
122+ const resp = await fetch ( file . download_url ) ;
123+ const content = await resp . text ( ) ;
124+ repoContents . push ( [ file . name , content ] ) ;
125+ }
126+ }
73127
74- return new Fiddle ( path . join ( folder , 'main.js' ) , url ) ;
128+ return this . fromEntries ( repoContents ) ;
75129 }
76130
77131 public async fromEntries ( src : Iterable < [ string , string ] > ) : Promise < Fiddle > {
0 commit comments