|
|
|
@@ -0,0 +1,179 @@
|
|
|
|
|
import { mkdir, readdir, rename, rm } from 'node:fs/promises';
|
|
|
|
|
import { join } from 'node:path';
|
|
|
|
|
import { getUserWorkspacesDir, getUserHomepageWorkspaceDir, getUserStateFile } from '@@/data-path';
|
|
|
|
|
import type { KeyMapping, ResolveDirs } from './types';
|
|
|
|
|
|
|
|
|
|
const RESERVED_DIRS = new Set(['screens']);
|
|
|
|
|
|
|
|
|
|
export function resolveKey(dirs: ResolveDirs, key: string): KeyMapping | null {
|
|
|
|
|
if (key === 'workspaces') return { file: join(dirs.wsDir, 'index.json') };
|
|
|
|
|
if (key === 'ws-terminals-default') return { file: join(dirs.wsDir, 'default-terminals.json') };
|
|
|
|
|
if (key === 'ws-host-terminals-default') return { file: join(dirs.wsDir, 'default-host-terminals.json') };
|
|
|
|
|
|
|
|
|
|
// screens/{name} → screens/{name}/layout.json
|
|
|
|
|
const screensMatch = key.match(/^screens\/(.+)$/);
|
|
|
|
|
if (screensMatch) {
|
|
|
|
|
const name = screensMatch[1]!;
|
|
|
|
|
const dir = join(dirs.screensDir, name);
|
|
|
|
|
return { file: join(dir, 'layout.json'), dir };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const layoutMatch = key.match(/^ws-layout-(.+)$/);
|
|
|
|
|
if (layoutMatch) {
|
|
|
|
|
const id = layoutMatch[1]!;
|
|
|
|
|
const dir = join(dirs.wsDir, id);
|
|
|
|
|
return { file: join(dir, 'layout.json'), dir };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const terminalsMatch = key.match(/^ws-terminals-(.+)$/);
|
|
|
|
|
if (terminalsMatch) {
|
|
|
|
|
const id = terminalsMatch[1]!;
|
|
|
|
|
const dir = join(dirs.wsDir, id);
|
|
|
|
|
return { file: join(dir, 'terminals.json'), dir };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const hostTerminalsMatch = key.match(/^ws-host-terminals-(.+)$/);
|
|
|
|
|
if (hostTerminalsMatch) {
|
|
|
|
|
const id = hostTerminalsMatch[1]!;
|
|
|
|
|
const dir = join(dirs.wsDir, id);
|
|
|
|
|
return { file: join(dir, 'host-terminals.json'), dir };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function readJsonFile(path: string): Promise<unknown | null> {
|
|
|
|
|
const file = Bun.file(path);
|
|
|
|
|
if (await file.exists()) return file.json();
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function writeJsonFile(path: string, data: unknown) {
|
|
|
|
|
await Bun.write(path, JSON.stringify(data, null, 2));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function migrateFromState(email: string, dirs: ResolveDirs) {
|
|
|
|
|
const stateFile = getUserStateFile(email);
|
|
|
|
|
const file = Bun.file(stateFile);
|
|
|
|
|
if (!(await file.exists())) return;
|
|
|
|
|
|
|
|
|
|
const state = (await file.json()) as Record<string, unknown>;
|
|
|
|
|
const wsKeys = Object.keys(state).filter(
|
|
|
|
|
(k) => k === 'workspaces' || k.startsWith('ws-layout-') || k.startsWith('ws-terminals-') || k.startsWith('ws-host-terminals-'),
|
|
|
|
|
);
|
|
|
|
|
if (wsKeys.length === 0) return;
|
|
|
|
|
|
|
|
|
|
await mkdir(dirs.wsDir, { recursive: true });
|
|
|
|
|
|
|
|
|
|
for (const key of wsKeys) {
|
|
|
|
|
const migratedKey = key === 'ws-layout-workspaces' ? 'screens/homepage' : key;
|
|
|
|
|
const mapping = resolveKey(dirs, migratedKey);
|
|
|
|
|
if (!mapping) continue;
|
|
|
|
|
if (mapping.dir) await mkdir(mapping.dir, { recursive: true });
|
|
|
|
|
await writeJsonFile(mapping.file, state[key]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const cleaned = { ...state };
|
|
|
|
|
for (const key of wsKeys) delete cleaned[key];
|
|
|
|
|
await Bun.write(stateFile, JSON.stringify(cleaned, null, 2));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function migrateHomepageToScreens(dirs: ResolveDirs, email: string) {
|
|
|
|
|
// Migrate from old ws-homepage/ dir to workspaces/screens/homepage/
|
|
|
|
|
const oldHomepageDir = getUserHomepageWorkspaceDir(email);
|
|
|
|
|
const layoutFile = join(oldHomepageDir, 'layout.json');
|
|
|
|
|
if (!(await Bun.file(layoutFile).exists())) return;
|
|
|
|
|
|
|
|
|
|
const targetDir = join(dirs.screensDir, 'homepage');
|
|
|
|
|
await mkdir(targetDir, { recursive: true });
|
|
|
|
|
|
|
|
|
|
for (const name of ['layout.json', 'terminals.json', 'host-terminals.json']) {
|
|
|
|
|
const src = join(oldHomepageDir, name);
|
|
|
|
|
if (await Bun.file(src).exists()) {
|
|
|
|
|
await rename(src, join(targetDir, name));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const remaining = await readdir(oldHomepageDir);
|
|
|
|
|
if (remaining.length === 0) await rm(oldHomepageDir, { recursive: true, force: true });
|
|
|
|
|
|
|
|
|
|
// Also migrate from workspaces/ws-homepage/ if it exists
|
|
|
|
|
const oldWsHomepageDir = join(dirs.wsDir, 'ws-homepage');
|
|
|
|
|
const oldWsLayout = join(oldWsHomepageDir, 'layout.json');
|
|
|
|
|
if (!(await Bun.file(oldWsLayout).exists())) return;
|
|
|
|
|
|
|
|
|
|
for (const name of ['layout.json', 'terminals.json', 'host-terminals.json']) {
|
|
|
|
|
const src = join(oldWsHomepageDir, name);
|
|
|
|
|
const target = join(targetDir, name);
|
|
|
|
|
if (await Bun.file(src).exists() && !(await Bun.file(target).exists())) {
|
|
|
|
|
await rename(src, target);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const wsRemaining = await readdir(oldWsHomepageDir);
|
|
|
|
|
if (wsRemaining.length === 0) await rm(oldWsHomepageDir, { recursive: true, force: true });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function readWorkspaceDir(dirPath: string, id: string, result: Record<string, unknown>) {
|
|
|
|
|
const layout = await readJsonFile(join(dirPath, 'layout.json'));
|
|
|
|
|
if (layout !== null) result[`ws-layout-${id}`] = layout;
|
|
|
|
|
|
|
|
|
|
const terminals = await readJsonFile(join(dirPath, 'terminals.json'));
|
|
|
|
|
if (terminals !== null) result[`ws-terminals-${id}`] = terminals;
|
|
|
|
|
|
|
|
|
|
const hostTerminals = await readJsonFile(join(dirPath, 'host-terminals.json'));
|
|
|
|
|
if (hostTerminals !== null) result[`ws-host-terminals-${id}`] = hostTerminals;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function readScreensDir(screensDir: string, result: Record<string, unknown>) {
|
|
|
|
|
let entries: import('node:fs').Dirent[] = [];
|
|
|
|
|
try {
|
|
|
|
|
entries = await readdir(screensDir, { withFileTypes: true });
|
|
|
|
|
} catch {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const entry of entries) {
|
|
|
|
|
if (!entry.isDirectory()) continue;
|
|
|
|
|
const layout = await readJsonFile(join(screensDir, entry.name, 'layout.json'));
|
|
|
|
|
if (layout !== null) result[`screens/${entry.name}`] = layout;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function readAllWorkspacesState(dirs: ResolveDirs): Promise<Record<string, unknown>> {
|
|
|
|
|
const result: Record<string, unknown> = {};
|
|
|
|
|
|
|
|
|
|
const indexData = await readJsonFile(join(dirs.wsDir, 'index.json'));
|
|
|
|
|
if (indexData !== null) result['workspaces'] = indexData;
|
|
|
|
|
|
|
|
|
|
const defaultTerminals = await readJsonFile(join(dirs.wsDir, 'default-terminals.json'));
|
|
|
|
|
if (defaultTerminals !== null) result['ws-terminals-default'] = defaultTerminals;
|
|
|
|
|
|
|
|
|
|
const defaultHostTerminals = await readJsonFile(join(dirs.wsDir, 'default-host-terminals.json'));
|
|
|
|
|
if (defaultHostTerminals !== null) result['ws-host-terminals-default'] = defaultHostTerminals;
|
|
|
|
|
|
|
|
|
|
// Read screens
|
|
|
|
|
await readScreensDir(dirs.screensDir, result);
|
|
|
|
|
|
|
|
|
|
// Read per-workspace subdirs
|
|
|
|
|
let entries: import('node:fs').Dirent[] = [];
|
|
|
|
|
try {
|
|
|
|
|
entries = await readdir(dirs.wsDir, { withFileTypes: true });
|
|
|
|
|
} catch {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const entry of entries) {
|
|
|
|
|
if (!entry.isDirectory() || RESERVED_DIRS.has(entry.name)) continue;
|
|
|
|
|
await readWorkspaceDir(join(dirs.wsDir, entry.name), entry.name, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getDirs(email: string): ResolveDirs {
|
|
|
|
|
return {
|
|
|
|
|
wsDir: getUserWorkspacesDir(email),
|
|
|
|
|
screensDir: join(getUserWorkspacesDir(email), 'screens'),
|
|
|
|
|
};
|
|
|
|
|
}
|