Last active
January 30, 2020 05:43
-
-
Save billiegoose/c311070034bc8d99e3e82d97636d0fd8 to your computer and use it in GitHub Desktop.
magic portal (isomorphic-git)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div> | |
<input id="repository" type="text" style="width: 50em" title="Tip: enter a private repo URL to see the credentialManager plugin prompt for a password."> | |
<button type="button" id="cloneButton">Clone</button> | |
</div> | |
<output id="log" style="white-space: pre; font-family: monospace;"></output> | |
<script src="https://unpkg.com/magic-portal"></script> | |
<script> | |
let worker = new Worker("./worker.js") | |
const portal = new MagicPortal(worker) | |
// 'emitter' plugin | |
const emitter = { | |
async emit (event, message) { | |
if (event === 'message') { | |
document.getElementById('log').textContent += message + '\n'; | |
} | |
} | |
} | |
portal.set('emitter', emitter, {void: ['emit']}) | |
// 'credentialManager' plugin | |
const credentialManager = { | |
async fill({ url }) { | |
let username = window.prompt("Username:") | |
let password = window.prompt("Password:") | |
return { username, password } | |
}, | |
async approved({ url, auth }) { | |
return | |
}, | |
async rejected({ url, auth }) { | |
window.alert('Authentication rejected') | |
return | |
} | |
} | |
portal.set('credentialManager', credentialManager, {void: ['approved', 'rejected']}) | |
;(async () => { | |
const git = await portal.get('git') | |
document.getElementById('log').textContent += 'ready\n'; | |
document.getElementById('repository').value = 'https://github.com/isomorphic-git/isomorphic-git' | |
document.getElementById('cloneButton').addEventListener('click', async () => { | |
document.getElementById('log').textContent = ''; | |
await git.clone({ | |
dir: '.', | |
corsProxy: 'https://cors.isomorphic-git.org', | |
url: document.getElementById('repository').value, | |
singleBranch: true, | |
depth: 100 | |
}) | |
let branches = await git.listBranches({ dir: '.', remote: 'origin' }) | |
console.log('branches', branches) | |
document.getElementById('log').textContent += 'branches ' + branches + '\n'; | |
}) | |
window.git = git; | |
window.worker = worker; | |
console.log(git); | |
})(); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
importScripts( | |
"https://isomorphic-git.org/js/browserfs.js", | |
"https://unpkg.com/isomorphic-git", | |
"https://unpkg.com/magic-portal" | |
) | |
let fsOptions = { | |
fs: 'IndexedDB', | |
options: {} | |
} | |
BrowserFS.configure(fsOptions, async function (err) { | |
if (err) return console.log(err) | |
// Initialize isomorphic-git | |
const fs = BrowserFS.BFSRequire('fs') | |
git.plugins.set('fs', fs) | |
const portal = new MagicPortal(self) | |
let emitter = await portal.get('emitter') | |
git.plugins.set('emitter', emitter) | |
let credentialManager = await portal.get('credentialManager') | |
git.plugins.set('credentialManager', credentialManager) | |
fs.getRootFS().empty(() => { | |
fs.mkdir('/', () => { | |
portal.set('git', git) | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment