From 10f89d0c53e610934b0035bbed5a4af258844786 Mon Sep 17 00:00:00 2001 From: Steven Hugg Date: Thu, 9 May 2019 13:22:24 -0400 Subject: [PATCH] list repositories in selector --- doc/notes.txt | 3 +-- src/project.ts | 14 -------------- src/services.ts | 30 ++++++++++++++++-------------- src/ui.ts | 33 ++++++++++++++++++++++++++++----- test/cli/testgithub.js | 8 ++++++-- 5 files changed, 51 insertions(+), 37 deletions(-) diff --git a/doc/notes.txt b/doc/notes.txt index 05fd5e72..9f90de7c 100644 --- a/doc/notes.txt +++ b/doc/notes.txt @@ -141,8 +141,7 @@ TODO: - what to do about included files? - what if files already open in editor - un-bind from repo? - - navigate to repo - - import 'examples/' retains path? + - can published files retain path? - what if import interrupted and partial files? - CORS for some blobs? - confusing when examples load if file not found diff --git a/src/project.ts b/src/project.ts index ce12c889..3e0b569a 100644 --- a/src/project.ts +++ b/src/project.ts @@ -337,18 +337,4 @@ export class CodeProject { return path; } - migrateToNewFolder(newprefix : string, newstore?) { - // TODO: must end with / - var newPath = newprefix + this.stripLocalPath(this.mainPath); - console.log(this.mainPath + "->" + newPath); - var data = this.filedata[this.mainPath]; - console.log(data.length + " bytes"); - return (newstore || this.store).setItem(newPath, data).then(() => { - //return this.store.removeItem(this.mainPath); - console.log("moved " + this.mainPath + " to " + newPath); - this.filedata[newPath] = this.filedata[this.mainPath]; //TODO? - this.mainPath = newPath; - }); - } - } diff --git a/src/services.ts b/src/services.ts index 4c2fb8a3..0c926eca 100644 --- a/src/services.ts +++ b/src/services.ts @@ -29,6 +29,14 @@ export function getRepos() : {[key:string]:GHRepoMetadata} { return JSON.parse(localStorage.getItem('__repos') || '{}'); } +export function parseGithubURL(ghurl:string) { + var toks = ghurl.split('/'); + if (toks.length < 5) return null; + if (toks[0] != 'https:') return null; + if (toks[2] != 'github.com') return null; + return {user:toks[3], repo:toks[4], repopath:toks[3]+'/'+toks[4]}; +} + export class GithubService { githubCons; @@ -80,28 +88,20 @@ export class GithubService { return false; } - parseGithubURL(ghurl:string) { - var toks = ghurl.split('/'); - if (toks.length < 5) return null; - if (toks[0] != 'https:') return null; - if (toks[2] != 'github.com') return null; - return {user:toks[3], repo:toks[4], repopath:toks[3]+'/'+toks[4]}; - } - getGithubSession(ghurl:string) : Promise { return new Promise( (yes,no) => { - var urlparse = this.parseGithubURL(ghurl); + var urlparse = parseGithubURL(ghurl); if (!urlparse) { no("Please enter a valid GitHub URL."); } var sess = { - url: ghurl, + url: 'https://github.com/' + urlparse.repopath, user: urlparse.user, reponame: urlparse.repo, repopath: urlparse.repopath, prefix: '', //this.getPrefix(urlparse.user, urlparse.repo), repo: this.github.repos(urlparse.user, urlparse.repo), - platform_id: this.project.platform_id + platform_id: this.project ? this.project.platform_id : null }; yes(sess); }); @@ -124,7 +124,8 @@ export class GithubService { // load README return sess.repo.contents('README.md').read(); }) - .catch( () => { + .catch( (e) => { + console.log(e); console.log('no README.md found') return ''; // empty README }) @@ -138,13 +139,14 @@ export class GithubService { sess.mainPath = m[1]; } // check README for proper platform + // unless we use githubURL= const re8plat = /8bitworkshop.com[^)]+platform=(\w+)/; m = re8plat.exec(readme); if (m) { console.log("platform id: '" + m[1] + "'"); - sess.platform_id = m[1]; - if (!this.project.platform_id.startsWith(m[1])) + if (sess.platform_id && !sess.platform_id.startsWith(m[1])) throw "Platform mismatch: Repository is " + m[1] + ", you have " + this.project.platform_id + " selected."; + sess.platform_id = m[1]; } // bind to repository this.bind(sess, true); diff --git a/src/ui.ts b/src/ui.ts index a18c1d6d..03e41afc 100644 --- a/src/ui.ts +++ b/src/ui.ts @@ -14,7 +14,7 @@ import { createNewPersistentStore } from "./store"; import { getFilenameForPath, getFilenamePrefix, highlightDifferences, invertMap, byteArrayToString, compressLZG, byteArrayToUTF8, isProbablyBinary, getWithBinary, getBasePlatform } from "./util"; import { StateRecorderImpl } from "./recorder"; -import { GHSession, GithubService, getRepos } from "./services"; +import { GHSession, GithubService, getRepos, parseGithubURL } from "./services"; // external libs (TODO) declare var Tour, GIF, saveAs, JSZip, Mousetrap, Split, firebase; @@ -253,6 +253,11 @@ function reloadProject(id:string) { // leave repository == '..' if (id == '..') { qs = {}; + } else if (id.indexOf('://') >= 0) { + var urlparse = parseGithubURL(id); + if (urlparse) { + qs = {repo:urlparse.repopath}; + } } else { qs['platform'] = platform_id; qs['file'] = id; @@ -390,7 +395,7 @@ function getGithubService() { function getBoundGithubURL() : string { var toks = (repo_id||'').split('/'); if (toks.length != 2) { - alert("This project is not bound to a GitHub project."); + alert("You are not in a GitHub repository. Choose Import or Publish first."); return null; } return 'https://github.com/' + toks[0] + '/' + toks[1]; @@ -398,7 +403,7 @@ function getBoundGithubURL() : string { function importProjectFromGithub(githuburl:string) { var sess : GHSession; - var urlparse = getGithubService().parseGithubURL(githuburl); + var urlparse = parseGithubURL(githuburl); if (!urlparse) { alert('Could not parse Github URL.'); return; @@ -468,7 +473,7 @@ function _publishProjectToGithub(e) { return pushChangesToGithub('initial import from 8bitworkshop.com'); }).then( () => { setWaitProgress(1.0); - reloadProject(current_project.mainPath); + reloadProject(current_project.stripLocalPath(current_project.mainPath)); }).catch( (e) => { setWaitDialog(false); console.log(e); @@ -749,6 +754,23 @@ function populateExamples(sel) { }); } +function populateRepos(sel) { + if (hasLocalStorage) { + var n = 0; + var repos = getRepos(); + if (repos) { + for (let repopath in repos) { + var repo = repos[repopath]; + if (getBasePlatform(repo.platform_id) == getBasePlatform(platform_id)) { + if (n++ == 0) + sel.append($("