UNPKG

@selenite/graph-editor

Version:

A graph editor for visual programming, based on rete and svelte.

97 lines (96 loc) 3.08 kB
import buffer from 'buffer/'; let fsInstance; async function getFs() { if (!fsInstance) { fsInstance = new (await import('@isomorphic-git/lightning-fs')).default('fs'); } return fsInstance; } if (typeof window !== 'undefined') // @ts-expect-error window.Buffer = buffer.Buffer; export class GitHubDataSource { url; constructor(url) { this.url = new URL(url); } async getGraphs() { const fs = await getFs(); const { clone, pull } = await import('isomorphic-git'); const http = await import('isomorphic-git/http/web'); // console.debug('Getting graphs from GitHub', this.url.toString()); const url = this.url; const pathname = url.pathname; const pathParts = pathname.split('/'); const username = pathParts[1]; const repo = pathParts[2]; const folderPath = pathParts.slice(5).join('/'); const repoUrl = `https://github.com/${username}/${repo}.git`; const dir = `/${username}-${repo}`; try { const stat = await fs.promises.stat(dir); // console.debug('Pulling repo', repoUrl) await pull({ fs, http, dir, corsProxy: 'https://cors.isomorphic-git.org', author: { name: 'Selenite GEOS' } }); } catch (e) { // console.debug('Cloning repo', repoUrl) await clone({ fs, http, url: repoUrl, dir, corsProxy: 'https://cors.isomorphic-git.org' }); } const blocksPath = dir + '/' + folderPath; const res = []; for await (const { path, content } of readFiles(blocksPath)) { if (!path.endsWith('.json')) continue; const menuPath = path.slice(blocksPath.length + 1); let parsed; try { parsed = JSON.parse(content); } catch (e) { console.error('Invalid JSON at', menuPath); continue; } // console.debug("Read", menuPath, parsed) if (path.endsWith('index.json')) { console.log('folder description'); continue; } const graphData = parsed; res.push({ id: path, path: menuPath.split('/'), ...graphData, name: graphData.editorName }); } return res; } } async function* readFiles(dir) { const fs = await getFs(); for (const file of await fs.promises.readdir(dir)) { const path = dir + '/' + file; if ((await fs.promises.stat(path)).isFile()) { yield { path, content: (await fs.promises.readFile(path)).toString() }; } else { for await (const f of readFiles(path)) { yield f; } } } }