first
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
import { createRouter } from '../../create-router';
|
||||
import { homedir } from 'node:os';
|
||||
import { mkdir } from 'node:fs/promises';
|
||||
import { readdirSync, existsSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { officerdb, count, Users } from 'officerdb';
|
||||
import { claudeCodeRouter } from './claude-code';
|
||||
import { opencodeRouter } from './opencode';
|
||||
import { applicationsRouter } from './applications';
|
||||
|
||||
const configDir = `${homedir()}/.config/officer.dev`;
|
||||
export const settingsPath = `${configDir}/server-settings.json`;
|
||||
|
||||
const settingsFile = Bun.file(settingsPath);
|
||||
if (!(await settingsFile.exists())) {
|
||||
await mkdir(configDir, { recursive: true });
|
||||
await Bun.write(settingsPath, '{}');
|
||||
}
|
||||
|
||||
export const serverSettingsRouter = createRouter();
|
||||
|
||||
serverSettingsRouter.route('/claude-code', claudeCodeRouter);
|
||||
serverSettingsRouter.route('/opencode', opencodeRouter);
|
||||
serverSettingsRouter.route('/applications', applicationsRouter);
|
||||
|
||||
serverSettingsRouter.get('/', async (ctx) => {
|
||||
const result = await officerdb.select({ count: count() }).from(Users);
|
||||
const userCount = result[0]?.count ?? 0;
|
||||
return ctx.json({ registrationOpen: userCount === 0 });
|
||||
});
|
||||
|
||||
serverSettingsRouter.get('/settings', async (ctx) => {
|
||||
const settings = await Bun.file(settingsPath).json();
|
||||
return ctx.json(settings);
|
||||
});
|
||||
|
||||
serverSettingsRouter.get('/onboarding-complete', async (ctx) => {
|
||||
const settings = await Bun.file(settingsPath).json();
|
||||
return ctx.json({ onboardingComplete: !!settings.onboardingComplete });
|
||||
});
|
||||
|
||||
serverSettingsRouter.put('/', async (ctx) => {
|
||||
const body = await ctx.req.json();
|
||||
const settings = await Bun.file(settingsPath).json();
|
||||
const updated = { ...settings, ...body };
|
||||
await Bun.write(settingsPath, JSON.stringify(updated, null, 2));
|
||||
return ctx.json(updated);
|
||||
});
|
||||
|
||||
serverSettingsRouter.get('/plugins', async (ctx) => {
|
||||
const pluginsDir = join(import.meta.dir, '../../../workspaces/plugins');
|
||||
const settings = await Bun.file(settingsPath)
|
||||
.json()
|
||||
.catch(() => ({}));
|
||||
const pluginSettings: Record<string, boolean> = settings.plugins ?? {};
|
||||
|
||||
const plugins: { id: string; name: string; description: string; enabled: boolean }[] = [];
|
||||
|
||||
if (!existsSync(pluginsDir)) return ctx.json(plugins);
|
||||
|
||||
const dirs = readdirSync(pluginsDir, { withFileTypes: true }).filter((d) => d.isDirectory());
|
||||
|
||||
for (const dir of dirs) {
|
||||
const hasServer = existsSync(join(pluginsDir, dir.name, 'server', 'index.ts'));
|
||||
const hasClient = existsSync(join(pluginsDir, dir.name, 'client', 'index.ts'));
|
||||
if (!hasServer && !hasClient) continue;
|
||||
|
||||
const mainIndex = join(pluginsDir, dir.name, 'index.ts');
|
||||
if (!existsSync(mainIndex)) continue;
|
||||
|
||||
const mod = await import(mainIndex);
|
||||
const meta = mod.plugin ?? { id: dir.name, name: dir.name, description: '' };
|
||||
plugins.push({
|
||||
id: meta.id,
|
||||
name: meta.name,
|
||||
description: meta.description,
|
||||
enabled: pluginSettings[meta.id] !== false,
|
||||
});
|
||||
}
|
||||
|
||||
return ctx.json(plugins);
|
||||
});
|
||||
Reference in New Issue
Block a user