1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-06-02 12:41:30 +00:00

redirect after main file imported

This commit is contained in:
Steven Hugg 2019-05-08 13:42:24 -04:00
parent 4cc9aaeaca
commit 19d145bbd5
4 changed files with 65 additions and 16 deletions

View File

@ -143,7 +143,6 @@ TODO:
- what if files already open in editor - what if files already open in editor
- import twice? - import twice?
- un-bind from repo? - un-bind from repo?
- login/logout?
WEB WORKER FORMAT WEB WORKER FORMAT

View File

@ -21,15 +21,45 @@ const README_md_template = "$NAME\n=====\n\nCompatible with the [$PLATFORM](http
export class GithubService { export class GithubService {
githubCons;
githubToken;
github; github;
store; store;
project : CodeProject; project : CodeProject;
branch : string = "master"; branch : string = "master";
constructor(github, store, project : CodeProject) { constructor(githubCons:() => any, githubToken:string, store, project : CodeProject) {
this.github = github; this.githubCons = githubCons;
this.githubToken = githubToken;
this.store = store; this.store = store;
this.project = project; this.project = project;
this.recreateGithub();
}
recreateGithub() {
this.github = new this.githubCons({token:this.githubToken});
}
login() : Promise<void> {
// already logged in? return immediately
if (this.githubToken && this.githubToken.length) {
return new Promise<void>( (yes,no) => {
yes();
});
}
// login via popup
var provider = new firebase.auth.GithubAuthProvider();
provider.addScope('repo');
return firebase.auth().signInWithPopup(provider).then( (result) => {
this.githubToken = result.credential.accessToken;
var user = result.user;
this.recreateGithub();
document.cookie = "__github_key=" + this.githubToken + ";path=/;max-age=31536000";
console.log("Stored GitHub OAUTH key");
}).catch( (error) => {
console.log(error);
alert("Could not login to GitHub: " + error);
});
} }
isFileIgnored(s : string) : boolean { isFileIgnored(s : string) : boolean {
@ -94,6 +124,7 @@ export class GithubService {
return sess.repo.contents('README.md').read(); return sess.repo.contents('README.md').read();
}) })
.catch( () => { .catch( () => {
console.log('no README.md found')
return ''; // empty README return ''; // empty README
}) })
.then( (readme) => { .then( (readme) => {
@ -101,7 +132,7 @@ export class GithubService {
// check README for main file // check README for main file
const re8main = /\(([^)]+)#mainfile\)/; const re8main = /\(([^)]+)#mainfile\)/;
m = re8main.exec(readme); m = re8main.exec(readme);
if (m) { if (m && m[1]) {
console.log("main path: '" + m[1] + "'"); console.log("main path: '" + m[1] + "'");
sess.mainPath = m[1]; sess.mainPath = m[1];
} }
@ -112,7 +143,7 @@ export class GithubService {
throw "Platform mismatch: Repository is " + m[1] + ", you have " + this.project.platform_id + " selected."; throw "Platform mismatch: Repository is " + m[1] + ", you have " + this.project.platform_id + " selected.";
} }
// get head commit // get head commit
return this.pull(ghurl); return sess;
}); });
} }
@ -151,6 +182,12 @@ export class GithubService {
return sess; return sess;
}); });
} }
importAndPull(ghurl:string) {
return this.import(ghurl).then((sess) => {
return this.pull(ghurl);
});
}
publish(reponame:string, desc:string, license:string, isprivate:boolean) : Promise<GHSession> { publish(reponame:string, desc:string, license:string, isprivate:boolean) : Promise<GHSession> {
var repo; var repo;

View File

@ -383,19 +383,20 @@ function getCookie(name) {
function getGithubService() { function getGithubService() {
if (!githubService) { if (!githubService) {
// get github API key from cookie // get github API key from cookie
// TODO: move to service?
var ghkey = getCookie('__github_key'); var ghkey = getCookie('__github_key');
var ghopts = {token:ghkey}; githubService = new GithubService(exports['Octokat'], ghkey, store, current_project);
githubService = new GithubService(new exports['Octokat'](ghopts), store, current_project);
console.log("loaded github service"); console.log("loaded github service");
} }
return githubService; return githubService;
} }
function getBoundGithubURL() { function getBoundGithubURL() : string {
console.log("main path: " + current_project.mainPath); console.log("main path: " + current_project.mainPath);
var ghurl = getGithubService().getBoundURL(current_project.mainPath); var ghurl = getGithubService().getBoundURL(current_project.mainPath);
console.log("Github URL: " + ghurl); console.log("Github URL: " + ghurl);
return ghurl || alert("This project (" + current_project.mainPath + ") is not bound to a GitHub project."); if (!ghurl) alert("This project (" + current_project.mainPath + ") is not bound to a GitHub project.")
return ghurl;
} }
function _importProjectFromGithub(e) { function _importProjectFromGithub(e) {
@ -404,9 +405,17 @@ function _importProjectFromGithub(e) {
modal.modal('show'); modal.modal('show');
btn.off('click').on('click', () => { btn.off('click').on('click', () => {
var githuburl = $("#importGithubURL").val()+""; var githuburl = $("#importGithubURL").val()+"";
getGithubService().import(githuburl).then( (sess:GHSession) => { var sess;
getGithubService().import(githuburl).then( (sess1:GHSession) => {
sess = sess1;
return getGithubService().pull(githuburl);
}).then( (sess2:GHSession) => {
// TODO: only first sessino has mainPath
if (sess.mainPath) { if (sess.mainPath) {
reloadPresetNamed(sess.prefix + sess.mainPath); reloadPresetNamed(sess.prefix + sess.mainPath);
} else {
updateSelector();
alert("Files imported, but no main file was found so you'll have to select this project in the pulldown.");
} }
// TODO : redirect to main file // TODO : redirect to main file
modal.modal('hide'); modal.modal('hide');
@ -435,7 +444,9 @@ function _publishProjectToGithub(e) {
var sess; var sess;
modal.modal('hide'); modal.modal('hide');
setWaitDialog(true); setWaitDialog(true);
getGithubService().publish(name, desc, license, priv).then( (_sess) => { getGithubService().login().then( () => {
return getGithubService().publish(name, desc, license, priv);
}).then( (_sess) => {
sess = _sess; sess = _sess;
console.log(sess); console.log(sess);
return current_project.migrateToNewFolder(sess.prefix); return current_project.migrateToNewFolder(sess.prefix);
@ -487,7 +498,9 @@ function pushChangesToGithub(message:string) {
} }
// push files // push files
setWaitDialog(true); setWaitDialog(true);
return getGithubService().commitPush(ghurl, message, files).then( (sess) => { return getGithubService().login().then( () => {
return getGithubService().commitPush(ghurl, message, files);
}).then( (sess) => {
setWaitDialog(false); setWaitDialog(false);
alert("Pushed files to " + ghurl); alert("Pushed files to " + ghurl);
return sess; return sess;

View File

@ -19,7 +19,7 @@ function newGH(store, platform_id) {
var project = new prj.CodeProject({}, platform_id||test_platform_id, null, store); var project = new prj.CodeProject({}, platform_id||test_platform_id, null, store);
project.mainPath = 'local/main.asm'; project.mainPath = 'local/main.asm';
project.updateFileInStore(project.mainPath, '\torg $0 ; test\n'); project.updateFileInStore(project.mainPath, '\torg $0 ; test\n');
return new serv.GithubService(new Octokat({token:'ec64fdd81dedab8b7547388eabef09288e9243a9'}), store, project); return new serv.GithubService(Octokat, 'ec64fdd81dedab8b7547388eabef09288e9243a9', store, project);
} }
const t0 = new Date().getTime(); const t0 = new Date().getTime();
@ -29,7 +29,7 @@ describe('Store', function() {
it('Should import from Github (check README)', function(done) { it('Should import from Github (check README)', function(done) {
var store = mstore.createNewPersistentStore(test_platform_id, function(store) { var store = mstore.createNewPersistentStore(test_platform_id, function(store) {
var gh = newGH(store); var gh = newGH(store);
gh.import('https://github.com/pzpinfo/testrepo1557322631070').then( (sess) => { gh.importAndPull('https://github.com/pzpinfo/testrepo1557322631070').then( (sess) => {
console.log(sess.paths); console.log(sess.paths);
assert.equal(2, sess.paths.length); assert.equal(2, sess.paths.length);
// TODO: test for presence in local storage, make sure returns keys // TODO: test for presence in local storage, make sure returns keys
@ -41,7 +41,7 @@ describe('Store', function() {
it('Should import from Github (no README)', function(done) { it('Should import from Github (no README)', function(done) {
var store = mstore.createNewPersistentStore(test_platform_id, function(store) { var store = mstore.createNewPersistentStore(test_platform_id, function(store) {
var gh = newGH(store); var gh = newGH(store);
gh.import('https://github.com/pzpinfo/testrepo3').then( (sess) => { gh.importAndPull('https://github.com/pzpinfo/testrepo3').then( (sess) => {
console.log(sess.paths); console.log(sess.paths);
assert.equal(3, sess.paths.length); assert.equal(3, sess.paths.length);
// TODO: test for presence in local storage, make sure returns keys // TODO: test for presence in local storage, make sure returns keys
@ -53,7 +53,7 @@ describe('Store', function() {
it('Should import from Github (wrong platform)', function(done) { it('Should import from Github (wrong platform)', function(done) {
var store = mstore.createNewPersistentStore('_FOO', function(store) { var store = mstore.createNewPersistentStore('_FOO', function(store) {
var gh = newGH(store, '_FOO'); var gh = newGH(store, '_FOO');
gh.import('https://github.com/pzpinfo/testrepo1557326056720').catch( (e) => { gh.importAndPull('https://github.com/pzpinfo/testrepo1557326056720').catch( (e) => {
assert.ok(e.startsWith('Platform mismatch')); assert.ok(e.startsWith('Platform mismatch'));
done(); done();
}); });