Skip to content

Instantly share code, notes, and snippets.

@mjgartendev
Forked from billiegoose/index.html
Created August 10, 2019 10:30
Show Gist options
  • Save mjgartendev/abd3396d31683bba0a03a7c36d828bce to your computer and use it in GitHub Desktop.
Save mjgartendev/abd3396d31683bba0a03a7c36d828bce to your computer and use it in GitHub Desktop.
magic portal (isomorphic-git)
<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>
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