mirror of
https://github.com/microsoft/monaco-editor.git
synced 2025-12-22 15:05:39 +01:00
Prettier format pending files
This commit is contained in:
parent
c3b3d72037
commit
34b0d92e8a
11 changed files with 1330 additions and 726 deletions
|
|
@ -1,14 +1,23 @@
|
|||
const { app, BrowserWindow } = require('electron');
|
||||
const path = require('path');
|
||||
const { app, BrowserWindow } = require("electron");
|
||||
const path = require("path");
|
||||
|
||||
function createWindow() {
|
||||
const win = new BrowserWindow({ width: 1280, height: 800, webPreferences: { nodeIntegration: false, contextIsolation: true } });
|
||||
const startUrl = process.env.SWITCH_URL || 'http://localhost:8080/switch.html';
|
||||
win.loadURL(startUrl);
|
||||
const win = new BrowserWindow({
|
||||
width: 1280,
|
||||
height: 800,
|
||||
webPreferences: { nodeIntegration: false, contextIsolation: true },
|
||||
});
|
||||
const startUrl =
|
||||
process.env.SWITCH_URL || "http://localhost:8080/switch.html";
|
||||
win.loadURL(startUrl);
|
||||
}
|
||||
|
||||
app.whenReady().then(() => {
|
||||
createWindow();
|
||||
app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) createWindow(); });
|
||||
createWindow();
|
||||
app.on("activate", () => {
|
||||
if (BrowserWindow.getAllWindows().length === 0) createWindow();
|
||||
});
|
||||
});
|
||||
app.on("window-all-closed", () => {
|
||||
if (process.platform !== "darwin") app.quit();
|
||||
});
|
||||
app.on('window-all-closed', () => { if (process.platform !== 'darwin') app.quit(); });
|
||||
|
|
|
|||
|
|
@ -1,26 +1,54 @@
|
|||
const CACHE = 'switch-cache-v1';
|
||||
const CACHE = "switch-cache-v1";
|
||||
const ASSETS = [
|
||||
'/',
|
||||
'/index.html',
|
||||
'/playground.html',
|
||||
'/monarch.html',
|
||||
'/switch.html'
|
||||
"/",
|
||||
"/index.html",
|
||||
"/playground.html",
|
||||
"/monarch.html",
|
||||
"/switch.html",
|
||||
];
|
||||
self.addEventListener('install', (e) => {
|
||||
e.waitUntil(caches.open(CACHE).then(c=>c.addAll(ASSETS)).then(()=>self.skipWaiting()));
|
||||
self.addEventListener("install", (e) => {
|
||||
e.waitUntil(
|
||||
caches
|
||||
.open(CACHE)
|
||||
.then((c) => c.addAll(ASSETS))
|
||||
.then(() => self.skipWaiting())
|
||||
);
|
||||
});
|
||||
self.addEventListener('activate', (e) => {
|
||||
e.waitUntil(caches.keys().then(keys=>Promise.all(keys.filter(k=>k!==CACHE).map(k=>caches.delete(k)))).then(()=>self.clients.claim()));
|
||||
self.addEventListener("activate", (e) => {
|
||||
e.waitUntil(
|
||||
caches
|
||||
.keys()
|
||||
.then((keys) =>
|
||||
Promise.all(
|
||||
keys.filter((k) => k !== CACHE).map((k) => caches.delete(k))
|
||||
)
|
||||
)
|
||||
.then(() => self.clients.claim())
|
||||
);
|
||||
});
|
||||
self.addEventListener('fetch', (e) => {
|
||||
const url = new URL(e.request.url);
|
||||
if (url.origin === location.origin) {
|
||||
e.respondWith(caches.match(e.request).then(r=> r || fetch(e.request).then(resp=>{
|
||||
if (e.request.method==='GET' && resp.ok && resp.type==='basic') {
|
||||
const clone = resp.clone();
|
||||
caches.open(CACHE).then(c=>c.put(e.request, clone));
|
||||
}
|
||||
return resp;
|
||||
}).catch(()=>caches.match('/index.html'))));
|
||||
}
|
||||
self.addEventListener("fetch", (e) => {
|
||||
const url = new URL(e.request.url);
|
||||
if (url.origin === location.origin) {
|
||||
e.respondWith(
|
||||
caches.match(e.request).then(
|
||||
(r) =>
|
||||
r ||
|
||||
fetch(e.request)
|
||||
.then((resp) => {
|
||||
if (
|
||||
e.request.method === "GET" &&
|
||||
resp.ok &&
|
||||
resp.type === "basic"
|
||||
) {
|
||||
const clone = resp.clone();
|
||||
caches
|
||||
.open(CACHE)
|
||||
.then((c) => c.put(e.request, clone));
|
||||
}
|
||||
return resp;
|
||||
})
|
||||
.catch(() => caches.match("/index.html"))
|
||||
)
|
||||
);
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -93,7 +93,10 @@ async function _loadMonaco(setup: IMonacoSetup): Promise<typeof monaco> {
|
|||
);
|
||||
} catch (e) {
|
||||
// If loading optional language contributions fails, still resolve the editor to keep app functional.
|
||||
console.error('Failed to load Monaco language contributions, continuing without them.', e);
|
||||
console.error(
|
||||
"Failed to load Monaco language contributions, continuing without them.",
|
||||
e
|
||||
);
|
||||
res(monaco);
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -9,8 +9,8 @@ elem.className = "root";
|
|||
document.body.append(elem);
|
||||
ReactDOM.render(<App />, elem);
|
||||
|
||||
if ('serviceWorker' in navigator) {
|
||||
window.addEventListener('load', () => {
|
||||
navigator.serviceWorker.register('/sw.js').catch(() => {});
|
||||
if ("serviceWorker" in navigator) {
|
||||
window.addEventListener("load", () => {
|
||||
navigator.serviceWorker.register("/sw.js").catch(() => {});
|
||||
});
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -1,167 +1,190 @@
|
|||
export type StoreName = "repos" | "branches" | "commits" | "issues" | "fsHandles" | "settings";
|
||||
export type StoreName =
|
||||
| "repos"
|
||||
| "branches"
|
||||
| "commits"
|
||||
| "issues"
|
||||
| "fsHandles"
|
||||
| "settings";
|
||||
|
||||
const DB_NAME = "switch-db";
|
||||
const DB_VERSION = 2;
|
||||
|
||||
export interface RepoRecord {
|
||||
id: string;
|
||||
name: string;
|
||||
createdAt: number;
|
||||
defaultBranch: string;
|
||||
fsHandleId?: string;
|
||||
id: string;
|
||||
name: string;
|
||||
createdAt: number;
|
||||
defaultBranch: string;
|
||||
fsHandleId?: string;
|
||||
}
|
||||
|
||||
export interface BranchRecord {
|
||||
id: string;
|
||||
repoId: string;
|
||||
name: string;
|
||||
headCommitId?: string;
|
||||
id: string;
|
||||
repoId: string;
|
||||
name: string;
|
||||
headCommitId?: string;
|
||||
}
|
||||
|
||||
export interface CommitRecord {
|
||||
id: string;
|
||||
repoId: string;
|
||||
message: string;
|
||||
parentIds: string[];
|
||||
timestamp: number;
|
||||
id: string;
|
||||
repoId: string;
|
||||
message: string;
|
||||
parentIds: string[];
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
export interface IssueRecord {
|
||||
id: string;
|
||||
repoId: string;
|
||||
title: string;
|
||||
body: string;
|
||||
labels: string[];
|
||||
status: "open" | "closed";
|
||||
createdAt: number;
|
||||
updatedAt: number;
|
||||
branchId?: string;
|
||||
filePath?: string;
|
||||
id: string;
|
||||
repoId: string;
|
||||
title: string;
|
||||
body: string;
|
||||
labels: string[];
|
||||
status: "open" | "closed";
|
||||
createdAt: number;
|
||||
updatedAt: number;
|
||||
branchId?: string;
|
||||
filePath?: string;
|
||||
}
|
||||
|
||||
export interface FsHandleRecord {
|
||||
id: string;
|
||||
handle: FileSystemDirectoryHandle;
|
||||
id: string;
|
||||
handle: FileSystemDirectoryHandle;
|
||||
}
|
||||
|
||||
export interface RepoTemplateRecord {
|
||||
id: string;
|
||||
name: string;
|
||||
fsHandleId: string;
|
||||
id: string;
|
||||
name: string;
|
||||
fsHandleId: string;
|
||||
}
|
||||
|
||||
export interface IssueTemplateRecord {
|
||||
id: string;
|
||||
name: string;
|
||||
title: string;
|
||||
body: string;
|
||||
labels: string[];
|
||||
id: string;
|
||||
name: string;
|
||||
title: string;
|
||||
body: string;
|
||||
labels: string[];
|
||||
}
|
||||
|
||||
export async function openDB(): Promise<IDBDatabase> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const req = indexedDB.open(DB_NAME, DB_VERSION);
|
||||
req.onupgradeneeded = () => {
|
||||
const db = req.result;
|
||||
if (!db.objectStoreNames.contains("repos")) {
|
||||
const store = db.createObjectStore("repos", { keyPath: "id" });
|
||||
store.createIndex("by_name", "name", { unique: false });
|
||||
}
|
||||
if (!db.objectStoreNames.contains("branches")) {
|
||||
const store = db.createObjectStore("branches", { keyPath: "id" });
|
||||
store.createIndex("by_repo", "repoId", { unique: false });
|
||||
store.createIndex("by_repo_name", ["repoId", "name"], { unique: true });
|
||||
}
|
||||
if (!db.objectStoreNames.contains("commits")) {
|
||||
const store = db.createObjectStore("commits", { keyPath: "id" });
|
||||
store.createIndex("by_repo", "repoId", { unique: false });
|
||||
}
|
||||
if (!db.objectStoreNames.contains("issues")) {
|
||||
const store = db.createObjectStore("issues", { keyPath: "id" });
|
||||
store.createIndex("by_repo", "repoId", { unique: false });
|
||||
}
|
||||
if (!db.objectStoreNames.contains("fsHandles")) {
|
||||
db.createObjectStore("fsHandles", { keyPath: "id" });
|
||||
}
|
||||
if (!db.objectStoreNames.contains("settings")) {
|
||||
db.createObjectStore("settings", { keyPath: "id" });
|
||||
}
|
||||
if (!db.objectStoreNames.contains("repoTemplates")) {
|
||||
db.createObjectStore("repoTemplates", { keyPath: "id" });
|
||||
}
|
||||
if (!db.objectStoreNames.contains("issueTemplates")) {
|
||||
db.createObjectStore("issueTemplates", { keyPath: "id" });
|
||||
}
|
||||
};
|
||||
req.onsuccess = () => resolve(req.result);
|
||||
req.onerror = () => reject(req.error);
|
||||
});
|
||||
return new Promise((resolve, reject) => {
|
||||
const req = indexedDB.open(DB_NAME, DB_VERSION);
|
||||
req.onupgradeneeded = () => {
|
||||
const db = req.result;
|
||||
if (!db.objectStoreNames.contains("repos")) {
|
||||
const store = db.createObjectStore("repos", { keyPath: "id" });
|
||||
store.createIndex("by_name", "name", { unique: false });
|
||||
}
|
||||
if (!db.objectStoreNames.contains("branches")) {
|
||||
const store = db.createObjectStore("branches", {
|
||||
keyPath: "id",
|
||||
});
|
||||
store.createIndex("by_repo", "repoId", { unique: false });
|
||||
store.createIndex("by_repo_name", ["repoId", "name"], {
|
||||
unique: true,
|
||||
});
|
||||
}
|
||||
if (!db.objectStoreNames.contains("commits")) {
|
||||
const store = db.createObjectStore("commits", {
|
||||
keyPath: "id",
|
||||
});
|
||||
store.createIndex("by_repo", "repoId", { unique: false });
|
||||
}
|
||||
if (!db.objectStoreNames.contains("issues")) {
|
||||
const store = db.createObjectStore("issues", { keyPath: "id" });
|
||||
store.createIndex("by_repo", "repoId", { unique: false });
|
||||
}
|
||||
if (!db.objectStoreNames.contains("fsHandles")) {
|
||||
db.createObjectStore("fsHandles", { keyPath: "id" });
|
||||
}
|
||||
if (!db.objectStoreNames.contains("settings")) {
|
||||
db.createObjectStore("settings", { keyPath: "id" });
|
||||
}
|
||||
if (!db.objectStoreNames.contains("repoTemplates")) {
|
||||
db.createObjectStore("repoTemplates", { keyPath: "id" });
|
||||
}
|
||||
if (!db.objectStoreNames.contains("issueTemplates")) {
|
||||
db.createObjectStore("issueTemplates", { keyPath: "id" });
|
||||
}
|
||||
};
|
||||
req.onsuccess = () => resolve(req.result);
|
||||
req.onerror = () => reject(req.error);
|
||||
});
|
||||
}
|
||||
|
||||
export async function tx<T>(storeNames: StoreName[], mode: IDBTransactionMode, fn: (tx: IDBTransaction) => Promise<T>): Promise<T> {
|
||||
const db = await openDB();
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
const transaction = db.transaction(storeNames, mode);
|
||||
const done = async () => {
|
||||
try {
|
||||
const r = await fn(transaction);
|
||||
resolve(r);
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
};
|
||||
transaction.oncomplete = () => db.close();
|
||||
transaction.onabort = () => reject(transaction.error);
|
||||
transaction.onerror = () => reject(transaction.error);
|
||||
done();
|
||||
});
|
||||
export async function tx<T>(
|
||||
storeNames: StoreName[],
|
||||
mode: IDBTransactionMode,
|
||||
fn: (tx: IDBTransaction) => Promise<T>
|
||||
): Promise<T> {
|
||||
const db = await openDB();
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
const transaction = db.transaction(storeNames, mode);
|
||||
const done = async () => {
|
||||
try {
|
||||
const r = await fn(transaction);
|
||||
resolve(r);
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
};
|
||||
transaction.oncomplete = () => db.close();
|
||||
transaction.onabort = () => reject(transaction.error);
|
||||
transaction.onerror = () => reject(transaction.error);
|
||||
done();
|
||||
});
|
||||
}
|
||||
|
||||
export async function put<T>(store: StoreName, value: T): Promise<void> {
|
||||
await tx([store], "readwrite", async (t) => {
|
||||
await requestAsPromise<void>(t.objectStore(store).put(value as any));
|
||||
return undefined as any;
|
||||
});
|
||||
await tx([store], "readwrite", async (t) => {
|
||||
await requestAsPromise<void>(t.objectStore(store).put(value as any));
|
||||
return undefined as any;
|
||||
});
|
||||
}
|
||||
|
||||
export async function get<T>(store: StoreName, key: IDBValidKey): Promise<T | undefined> {
|
||||
return tx([store], "readonly", async (t) => {
|
||||
return requestAsPromise<T | undefined>(t.objectStore(store).get(key));
|
||||
});
|
||||
export async function get<T>(
|
||||
store: StoreName,
|
||||
key: IDBValidKey
|
||||
): Promise<T | undefined> {
|
||||
return tx([store], "readonly", async (t) => {
|
||||
return requestAsPromise<T | undefined>(t.objectStore(store).get(key));
|
||||
});
|
||||
}
|
||||
|
||||
export async function del(store: StoreName, key: IDBValidKey): Promise<void> {
|
||||
await tx([store], "readwrite", async (t) => {
|
||||
await requestAsPromise<void>(t.objectStore(store).delete(key));
|
||||
return undefined as any;
|
||||
});
|
||||
await tx([store], "readwrite", async (t) => {
|
||||
await requestAsPromise<void>(t.objectStore(store).delete(key));
|
||||
return undefined as any;
|
||||
});
|
||||
}
|
||||
|
||||
export async function getAllByIndex<T>(store: StoreName, index: string, query: IDBValidKey | IDBKeyRange): Promise<T[]> {
|
||||
return tx([store], "readonly", async (t) => {
|
||||
const idx = t.objectStore(store).index(index);
|
||||
return requestAsPromise<T[]>(idx.getAll(query));
|
||||
});
|
||||
export async function getAllByIndex<T>(
|
||||
store: StoreName,
|
||||
index: string,
|
||||
query: IDBValidKey | IDBKeyRange
|
||||
): Promise<T[]> {
|
||||
return tx([store], "readonly", async (t) => {
|
||||
const idx = t.objectStore(store).index(index);
|
||||
return requestAsPromise<T[]>(idx.getAll(query));
|
||||
});
|
||||
}
|
||||
|
||||
export async function getAll<T>(store: StoreName): Promise<T[]> {
|
||||
return tx([store], "readonly", async (t) => {
|
||||
return requestAsPromise<T[]>(t.objectStore(store).getAll());
|
||||
});
|
||||
return tx([store], "readonly", async (t) => {
|
||||
return requestAsPromise<T[]>(t.objectStore(store).getAll());
|
||||
});
|
||||
}
|
||||
|
||||
export async function setSetting<T>(id: string, value: T): Promise<void> {
|
||||
await put("settings", { id, value } as any);
|
||||
await put("settings", { id, value } as any);
|
||||
}
|
||||
|
||||
export async function getSetting<T>(id: string): Promise<T | undefined> {
|
||||
const rec = await get<any>("settings", id);
|
||||
return rec?.value as T | undefined;
|
||||
const rec = await get<any>("settings", id);
|
||||
return rec?.value as T | undefined;
|
||||
}
|
||||
|
||||
function requestAsPromise<T>(req: IDBRequest<T>): Promise<T> {
|
||||
return new Promise((resolve, reject) => {
|
||||
req.onsuccess = () => resolve(req.result as T);
|
||||
req.onerror = () => reject(req.error);
|
||||
});
|
||||
return new Promise((resolve, reject) => {
|
||||
req.onsuccess = () => resolve(req.result as T);
|
||||
req.onerror = () => reject(req.error);
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,113 +1,162 @@
|
|||
import { put, get, type FsHandleRecord } from "./db";
|
||||
import { nanoid } from "./uid";
|
||||
|
||||
export async function pickDirectory(): Promise<{ id: string; handle: FileSystemDirectoryHandle }> {
|
||||
// @ts-ignore
|
||||
const handle: FileSystemDirectoryHandle = await (window as any).showDirectoryPicker();
|
||||
const id = nanoid();
|
||||
const rec: FsHandleRecord = { id, handle };
|
||||
await put("fsHandles", rec);
|
||||
return { id, handle };
|
||||
export async function pickDirectory(): Promise<{
|
||||
id: string;
|
||||
handle: FileSystemDirectoryHandle;
|
||||
}> {
|
||||
// @ts-ignore
|
||||
const handle: FileSystemDirectoryHandle = await (
|
||||
window as any
|
||||
).showDirectoryPicker();
|
||||
const id = nanoid();
|
||||
const rec: FsHandleRecord = { id, handle };
|
||||
await put("fsHandles", rec);
|
||||
return { id, handle };
|
||||
}
|
||||
|
||||
export async function getDirectoryHandle(id: string): Promise<FileSystemDirectoryHandle | undefined> {
|
||||
const rec = await get<FsHandleRecord>("fsHandles", id);
|
||||
return rec?.handle;
|
||||
export async function getDirectoryHandle(
|
||||
id: string
|
||||
): Promise<FileSystemDirectoryHandle | undefined> {
|
||||
const rec = await get<FsHandleRecord>("fsHandles", id);
|
||||
return rec?.handle;
|
||||
}
|
||||
|
||||
export async function ensureReadPerm(dir: FileSystemDirectoryHandle): Promise<boolean> {
|
||||
const perm = await (dir as any).queryPermission?.({ mode: "read" });
|
||||
if (perm === "granted") return true;
|
||||
const req = await (dir as any).requestPermission?.({ mode: "read" });
|
||||
return req === "granted";
|
||||
export async function ensureReadPerm(
|
||||
dir: FileSystemDirectoryHandle
|
||||
): Promise<boolean> {
|
||||
const perm = await (dir as any).queryPermission?.({ mode: "read" });
|
||||
if (perm === "granted") return true;
|
||||
const req = await (dir as any).requestPermission?.({ mode: "read" });
|
||||
return req === "granted";
|
||||
}
|
||||
|
||||
export async function ensureWritePerm(dir: FileSystemDirectoryHandle): Promise<boolean> {
|
||||
const perm = await (dir as any).queryPermission?.({ mode: "readwrite" });
|
||||
if (perm === "granted") return true;
|
||||
const req = await (dir as any).requestPermission?.({ mode: "readwrite" });
|
||||
return req === "granted";
|
||||
export async function ensureWritePerm(
|
||||
dir: FileSystemDirectoryHandle
|
||||
): Promise<boolean> {
|
||||
const perm = await (dir as any).queryPermission?.({ mode: "readwrite" });
|
||||
if (perm === "granted") return true;
|
||||
const req = await (dir as any).requestPermission?.({ mode: "readwrite" });
|
||||
return req === "granted";
|
||||
}
|
||||
|
||||
export async function getDirectoryHandleByPath(root: FileSystemDirectoryHandle, path: string, create = false): Promise<FileSystemDirectoryHandle | undefined> {
|
||||
const parts = path.split("/").filter(Boolean);
|
||||
let cur: FileSystemDirectoryHandle = root;
|
||||
for (const name of parts) {
|
||||
const next = await (cur as any).getDirectoryHandle(name, { create }).catch(() => undefined);
|
||||
if (!next) return undefined;
|
||||
cur = next;
|
||||
}
|
||||
return cur;
|
||||
export async function getDirectoryHandleByPath(
|
||||
root: FileSystemDirectoryHandle,
|
||||
path: string,
|
||||
create = false
|
||||
): Promise<FileSystemDirectoryHandle | undefined> {
|
||||
const parts = path.split("/").filter(Boolean);
|
||||
let cur: FileSystemDirectoryHandle = root;
|
||||
for (const name of parts) {
|
||||
const next = await (cur as any)
|
||||
.getDirectoryHandle(name, { create })
|
||||
.catch(() => undefined);
|
||||
if (!next) return undefined;
|
||||
cur = next;
|
||||
}
|
||||
return cur;
|
||||
}
|
||||
|
||||
export async function getFileHandleByPath(root: FileSystemDirectoryHandle, path: string, create = false): Promise<FileSystemFileHandle | undefined> {
|
||||
const parts = path.split("/");
|
||||
const dirPath = parts.slice(0, -1).join("/");
|
||||
const fileName = parts[parts.length - 1];
|
||||
const dir = dirPath ? await getDirectoryHandleByPath(root, dirPath, create) : root;
|
||||
if (!dir) return undefined;
|
||||
return (dir as any).getFileHandle(fileName, { create }).catch(() => undefined);
|
||||
export async function getFileHandleByPath(
|
||||
root: FileSystemDirectoryHandle,
|
||||
path: string,
|
||||
create = false
|
||||
): Promise<FileSystemFileHandle | undefined> {
|
||||
const parts = path.split("/");
|
||||
const dirPath = parts.slice(0, -1).join("/");
|
||||
const fileName = parts[parts.length - 1];
|
||||
const dir = dirPath
|
||||
? await getDirectoryHandleByPath(root, dirPath, create)
|
||||
: root;
|
||||
if (!dir) return undefined;
|
||||
return (dir as any)
|
||||
.getFileHandle(fileName, { create })
|
||||
.catch(() => undefined);
|
||||
}
|
||||
|
||||
export async function readFileText(root: FileSystemDirectoryHandle, path: string): Promise<string | undefined> {
|
||||
const fh = await getFileHandleByPath(root, path);
|
||||
if (!fh) return undefined;
|
||||
const file = await fh.getFile();
|
||||
return file.text();
|
||||
export async function readFileText(
|
||||
root: FileSystemDirectoryHandle,
|
||||
path: string
|
||||
): Promise<string | undefined> {
|
||||
const fh = await getFileHandleByPath(root, path);
|
||||
if (!fh) return undefined;
|
||||
const file = await fh.getFile();
|
||||
return file.text();
|
||||
}
|
||||
|
||||
export async function writeFileText(root: FileSystemDirectoryHandle, path: string, content: string): Promise<void> {
|
||||
const dirPerm = await ensureWritePerm(root);
|
||||
if (!dirPerm) throw new Error("No write permission");
|
||||
const fh = await getFileHandleByPath(root, path, true);
|
||||
if (!fh) throw new Error("Cannot create file");
|
||||
const w = await (fh as any).createWritable();
|
||||
await w.write(content);
|
||||
await w.close();
|
||||
export async function writeFileText(
|
||||
root: FileSystemDirectoryHandle,
|
||||
path: string,
|
||||
content: string
|
||||
): Promise<void> {
|
||||
const dirPerm = await ensureWritePerm(root);
|
||||
if (!dirPerm) throw new Error("No write permission");
|
||||
const fh = await getFileHandleByPath(root, path, true);
|
||||
if (!fh) throw new Error("Cannot create file");
|
||||
const w = await (fh as any).createWritable();
|
||||
await w.write(content);
|
||||
await w.close();
|
||||
}
|
||||
|
||||
export async function createDirectory(root: FileSystemDirectoryHandle, path: string): Promise<void> {
|
||||
const ok = await ensureWritePerm(root);
|
||||
if (!ok) throw new Error("No write permission");
|
||||
await getDirectoryHandleByPath(root, path, true);
|
||||
export async function createDirectory(
|
||||
root: FileSystemDirectoryHandle,
|
||||
path: string
|
||||
): Promise<void> {
|
||||
const ok = await ensureWritePerm(root);
|
||||
if (!ok) throw new Error("No write permission");
|
||||
await getDirectoryHandleByPath(root, path, true);
|
||||
}
|
||||
|
||||
export async function deleteEntry(root: FileSystemDirectoryHandle, path: string, recursive = false): Promise<void> {
|
||||
const parts = path.split("/");
|
||||
const dirPath = parts.slice(0, -1).join("/");
|
||||
const name = parts[parts.length - 1];
|
||||
const dir = dirPath ? await getDirectoryHandleByPath(root, dirPath) : root;
|
||||
if (!dir) throw new Error("Path not found");
|
||||
await (dir as any).removeEntry(name, { recursive }).catch(() => undefined);
|
||||
export async function deleteEntry(
|
||||
root: FileSystemDirectoryHandle,
|
||||
path: string,
|
||||
recursive = false
|
||||
): Promise<void> {
|
||||
const parts = path.split("/");
|
||||
const dirPath = parts.slice(0, -1).join("/");
|
||||
const name = parts[parts.length - 1];
|
||||
const dir = dirPath ? await getDirectoryHandleByPath(root, dirPath) : root;
|
||||
if (!dir) throw new Error("Path not found");
|
||||
await (dir as any).removeEntry(name, { recursive }).catch(() => undefined);
|
||||
}
|
||||
|
||||
export async function* walk(dir: FileSystemDirectoryHandle, pathPrefix = ""): AsyncGenerator<{ path: string; file: File }>{
|
||||
// @ts-ignore
|
||||
for await (const [name, entry] of (dir as any).entries()) {
|
||||
const p = pathPrefix ? `${pathPrefix}/${name}` : name;
|
||||
if (entry.kind === "directory") {
|
||||
yield* walk(entry as FileSystemDirectoryHandle, p);
|
||||
} else {
|
||||
const file = await (entry as FileSystemFileHandle).getFile();
|
||||
yield { path: p, file };
|
||||
}
|
||||
}
|
||||
export async function* walk(
|
||||
dir: FileSystemDirectoryHandle,
|
||||
pathPrefix = ""
|
||||
): AsyncGenerator<{ path: string; file: File }> {
|
||||
// @ts-ignore
|
||||
for await (const [name, entry] of (dir as any).entries()) {
|
||||
const p = pathPrefix ? `${pathPrefix}/${name}` : name;
|
||||
if (entry.kind === "directory") {
|
||||
yield* walk(entry as FileSystemDirectoryHandle, p);
|
||||
} else {
|
||||
const file = await (entry as FileSystemFileHandle).getFile();
|
||||
yield { path: p, file };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function copyDirectory(src: FileSystemDirectoryHandle, dest: FileSystemDirectoryHandle): Promise<void> {
|
||||
const ok = await ensureWritePerm(dest);
|
||||
if (!ok) throw new Error("No write permission on destination");
|
||||
// @ts-ignore
|
||||
for await (const [name, entry] of (src as any).entries()) {
|
||||
if (entry.kind === "directory") {
|
||||
const sub = await (dest as any).getDirectoryHandle(name, { create: true });
|
||||
await copyDirectory(entry as FileSystemDirectoryHandle, sub);
|
||||
} else {
|
||||
const file = await (entry as FileSystemFileHandle).getFile();
|
||||
const fh = await (dest as any).getFileHandle(name, { create: true });
|
||||
const w = await (fh as any).createWritable();
|
||||
await w.write(await file.arrayBuffer());
|
||||
await w.close();
|
||||
}
|
||||
}
|
||||
export async function copyDirectory(
|
||||
src: FileSystemDirectoryHandle,
|
||||
dest: FileSystemDirectoryHandle
|
||||
): Promise<void> {
|
||||
const ok = await ensureWritePerm(dest);
|
||||
if (!ok) throw new Error("No write permission on destination");
|
||||
// @ts-ignore
|
||||
for await (const [name, entry] of (src as any).entries()) {
|
||||
if (entry.kind === "directory") {
|
||||
const sub = await (dest as any).getDirectoryHandle(name, {
|
||||
create: true,
|
||||
});
|
||||
await copyDirectory(entry as FileSystemDirectoryHandle, sub);
|
||||
} else {
|
||||
const file = await (entry as FileSystemFileHandle).getFile();
|
||||
const fh = await (dest as any).getFileHandle(name, {
|
||||
create: true,
|
||||
});
|
||||
const w = await (fh as any).createWritable();
|
||||
await w.write(await file.arrayBuffer());
|
||||
await w.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { createClient, SupabaseClient } from '@supabase/supabase-js';
|
||||
import { createClient, SupabaseClient } from "@supabase/supabase-js";
|
||||
|
||||
const url = process.env.SUPABASE_URL as string;
|
||||
const key = process.env.SUPABASE_ANON_KEY as string;
|
||||
export const supabase: SupabaseClient | undefined = url && key ? createClient(url, key) : undefined;
|
||||
export const supabase: SupabaseClient | undefined =
|
||||
url && key ? createClient(url, key) : undefined;
|
||||
|
|
|
|||
|
|
@ -1,37 +1,64 @@
|
|||
import { getAll, getAllByIndex, type RepoRecord, type BranchRecord, type CommitRecord, type IssueRecord } from './db';
|
||||
import { supabase } from './supabase';
|
||||
import {
|
||||
getAll,
|
||||
getAllByIndex,
|
||||
type RepoRecord,
|
||||
type BranchRecord,
|
||||
type CommitRecord,
|
||||
type IssueRecord,
|
||||
} from "./db";
|
||||
import { supabase } from "./supabase";
|
||||
|
||||
export interface ExportBundle {
|
||||
repo: RepoRecord;
|
||||
branches: BranchRecord[];
|
||||
commits: CommitRecord[];
|
||||
issues: IssueRecord[];
|
||||
exportedAt: number;
|
||||
repo: RepoRecord;
|
||||
branches: BranchRecord[];
|
||||
commits: CommitRecord[];
|
||||
issues: IssueRecord[];
|
||||
exportedAt: number;
|
||||
}
|
||||
|
||||
export async function exportRepoBundle(repoId: string): Promise<ExportBundle> {
|
||||
const repos = await getAll<RepoRecord>('repos');
|
||||
const repo = repos.find(r=>r.id===repoId)!;
|
||||
const branches = await getAllByIndex<BranchRecord>('branches','by_repo', repoId);
|
||||
const commits = await getAllByIndex<CommitRecord>('commits','by_repo', repoId);
|
||||
const issues = await getAllByIndex<IssueRecord>('issues','by_repo', repoId);
|
||||
return { repo, branches, commits, issues, exportedAt: Date.now() };
|
||||
const repos = await getAll<RepoRecord>("repos");
|
||||
const repo = repos.find((r) => r.id === repoId)!;
|
||||
const branches = await getAllByIndex<BranchRecord>(
|
||||
"branches",
|
||||
"by_repo",
|
||||
repoId
|
||||
);
|
||||
const commits = await getAllByIndex<CommitRecord>(
|
||||
"commits",
|
||||
"by_repo",
|
||||
repoId
|
||||
);
|
||||
const issues = await getAllByIndex<IssueRecord>(
|
||||
"issues",
|
||||
"by_repo",
|
||||
repoId
|
||||
);
|
||||
return { repo, branches, commits, issues, exportedAt: Date.now() };
|
||||
}
|
||||
|
||||
export async function pushRepoToSupabase(repoId: string): Promise<void> {
|
||||
if (!supabase) throw new Error('Supabase not configured');
|
||||
const bundle = await exportRepoBundle(repoId);
|
||||
const path = `repos/${repoId}.json`;
|
||||
const data = new Blob([JSON.stringify(bundle)], { type: 'application/json' });
|
||||
const { error } = await supabase.storage.from('switch').upload(path, data, { upsert: true });
|
||||
if (error) throw error;
|
||||
if (!supabase) throw new Error("Supabase not configured");
|
||||
const bundle = await exportRepoBundle(repoId);
|
||||
const path = `repos/${repoId}.json`;
|
||||
const data = new Blob([JSON.stringify(bundle)], {
|
||||
type: "application/json",
|
||||
});
|
||||
const { error } = await supabase.storage
|
||||
.from("switch")
|
||||
.upload(path, data, { upsert: true });
|
||||
if (error) throw error;
|
||||
}
|
||||
|
||||
export async function pullRepoFromSupabase(repoId: string): Promise<ExportBundle | undefined> {
|
||||
if (!supabase) throw new Error('Supabase not configured');
|
||||
const path = `repos/${repoId}.json`;
|
||||
const { data, error } = await supabase.storage.from('switch').download(path);
|
||||
if (error) throw error;
|
||||
const text = await data.text();
|
||||
return JSON.parse(text) as ExportBundle;
|
||||
export async function pullRepoFromSupabase(
|
||||
repoId: string
|
||||
): Promise<ExportBundle | undefined> {
|
||||
if (!supabase) throw new Error("Supabase not configured");
|
||||
const path = `repos/${repoId}.json`;
|
||||
const { data, error } = await supabase.storage
|
||||
.from("switch")
|
||||
.download(path);
|
||||
if (error) throw error;
|
||||
const text = await data.text();
|
||||
return JSON.parse(text) as ExportBundle;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,26 +1,41 @@
|
|||
import { get, put, del, getAll, type RepoTemplateRecord, type IssueTemplateRecord } from "./db";
|
||||
import {
|
||||
get,
|
||||
put,
|
||||
del,
|
||||
getAll,
|
||||
type RepoTemplateRecord,
|
||||
type IssueTemplateRecord,
|
||||
} from "./db";
|
||||
import { nanoid } from "./uid";
|
||||
|
||||
export async function createRepoTemplate(name: string, fsHandleId: string): Promise<RepoTemplateRecord> {
|
||||
const rec: RepoTemplateRecord = { id: nanoid(), name, fsHandleId };
|
||||
await put("repoTemplates" as any, rec as any);
|
||||
return rec;
|
||||
export async function createRepoTemplate(
|
||||
name: string,
|
||||
fsHandleId: string
|
||||
): Promise<RepoTemplateRecord> {
|
||||
const rec: RepoTemplateRecord = { id: nanoid(), name, fsHandleId };
|
||||
await put("repoTemplates" as any, rec as any);
|
||||
return rec;
|
||||
}
|
||||
export async function listRepoTemplates(): Promise<RepoTemplateRecord[]> {
|
||||
return getAll<any>("repoTemplates" as any) as any;
|
||||
return getAll<any>("repoTemplates" as any) as any;
|
||||
}
|
||||
export async function deleteRepoTemplate(id: string): Promise<void> {
|
||||
await del("repoTemplates" as any, id);
|
||||
await del("repoTemplates" as any, id);
|
||||
}
|
||||
|
||||
export async function createIssueTemplate(data: { name: string; title: string; body: string; labels: string[] }): Promise<IssueTemplateRecord> {
|
||||
const rec: IssueTemplateRecord = { id: nanoid(), ...data };
|
||||
await put("issueTemplates" as any, rec as any);
|
||||
return rec;
|
||||
export async function createIssueTemplate(data: {
|
||||
name: string;
|
||||
title: string;
|
||||
body: string;
|
||||
labels: string[];
|
||||
}): Promise<IssueTemplateRecord> {
|
||||
const rec: IssueTemplateRecord = { id: nanoid(), ...data };
|
||||
await put("issueTemplates" as any, rec as any);
|
||||
return rec;
|
||||
}
|
||||
export async function listIssueTemplates(): Promise<IssueTemplateRecord[]> {
|
||||
return getAll<any>("issueTemplates" as any) as any;
|
||||
return getAll<any>("issueTemplates" as any) as any;
|
||||
}
|
||||
export async function deleteIssueTemplate(id: string): Promise<void> {
|
||||
await del("issueTemplates" as any, id);
|
||||
await del("issueTemplates" as any, id);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -76,7 +76,9 @@ module.exports = {
|
|||
"process.env": {
|
||||
YEAR: JSON.stringify(new Date().getFullYear()),
|
||||
SUPABASE_URL: JSON.stringify(process.env.SUPABASE_URL || ""),
|
||||
SUPABASE_ANON_KEY: JSON.stringify(process.env.SUPABASE_ANON_KEY || ""),
|
||||
SUPABASE_ANON_KEY: JSON.stringify(
|
||||
process.env.SUPABASE_ANON_KEY || ""
|
||||
),
|
||||
},
|
||||
}),
|
||||
new CleanWebpackPlugin(),
|
||||
|
|
@ -119,7 +121,11 @@ module.exports = {
|
|||
}),
|
||||
new CopyPlugin({
|
||||
patterns: [
|
||||
{ from: "./typedoc/dist", to: "./typedoc/", noErrorOnMissing: true },
|
||||
{
|
||||
from: "./typedoc/dist",
|
||||
to: "./typedoc/",
|
||||
noErrorOnMissing: true,
|
||||
},
|
||||
],
|
||||
}),
|
||||
new CopyPlugin({
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue