feat: preview server startup directly
This commit is contained in:
@@ -8,6 +8,8 @@ import { getProjectEngine } from './ProjectEngine';
|
||||
interface ActiveProjectContext {
|
||||
projectId: string;
|
||||
dataDir?: string;
|
||||
projectName?: string;
|
||||
projectDescription?: string;
|
||||
}
|
||||
|
||||
interface PostEngineContract {
|
||||
@@ -57,6 +59,30 @@ function clampMaxPostsPerPage(value: unknown): number {
|
||||
return normalized;
|
||||
}
|
||||
|
||||
function resolvePageTitle(metadata: ProjectMetadata | null, fallbackProjectName?: string, fallbackProjectDescription?: string): string {
|
||||
const candidate = metadata?.description?.trim();
|
||||
if (candidate) {
|
||||
return candidate;
|
||||
}
|
||||
|
||||
const metadataName = metadata?.name?.trim();
|
||||
if (metadataName) {
|
||||
return metadataName;
|
||||
}
|
||||
|
||||
const descriptionFallback = fallbackProjectDescription?.trim();
|
||||
if (descriptionFallback) {
|
||||
return descriptionFallback;
|
||||
}
|
||||
|
||||
const fallback = fallbackProjectName?.trim();
|
||||
if (fallback) {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
return 'Blog Preview';
|
||||
}
|
||||
|
||||
function escapeHtml(value: string): string {
|
||||
return value
|
||||
.replace(/&/g, '&')
|
||||
@@ -162,7 +188,12 @@ export class PreviewServer {
|
||||
const activeProject = await projectEngine.getActiveProject();
|
||||
const projectId = activeProject?.id ?? 'default';
|
||||
const dataDir = projectEngine.getDataDir(projectId, activeProject?.dataPath);
|
||||
return { projectId, dataDir };
|
||||
return {
|
||||
projectId,
|
||||
dataDir,
|
||||
projectName: activeProject?.name,
|
||||
projectDescription: activeProject?.description ?? undefined,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
@@ -267,7 +298,7 @@ export class PreviewServer {
|
||||
return;
|
||||
}
|
||||
|
||||
this.respond(res, 200, getPageHtml(result, 'Blog Preview'));
|
||||
this.respond(res, 200, getPageHtml(result, resolvePageTitle(metadata, context.projectName, context.projectDescription)));
|
||||
} catch (error) {
|
||||
console.error('[PreviewServer] Request failed:', error);
|
||||
this.respond(res, 500, 'Internal Server Error');
|
||||
|
||||
@@ -109,6 +109,14 @@ async function openPreviewInBrowser(): Promise<void> {
|
||||
await shell.openExternal(`${previewServer.getBaseUrl()}/`);
|
||||
}
|
||||
|
||||
async function startPreviewServerOnAppStart(): Promise<void> {
|
||||
if (!previewServer) {
|
||||
previewServer = new PreviewServer();
|
||||
}
|
||||
|
||||
await previewServer.start(PREVIEW_SERVER_PORT);
|
||||
}
|
||||
|
||||
function createApplicationMenu(): Menu {
|
||||
const template: MenuItemConstructorOptions[] = [
|
||||
{
|
||||
@@ -445,6 +453,11 @@ async function initialize(): Promise<void> {
|
||||
// App lifecycle
|
||||
app.whenReady().then(async () => {
|
||||
await initialize();
|
||||
try {
|
||||
await startPreviewServerOnAppStart();
|
||||
} catch (error) {
|
||||
console.error('Failed to start preview server on app startup:', error);
|
||||
}
|
||||
createWindow();
|
||||
|
||||
app.on('activate', () => {
|
||||
|
||||
Reference in New Issue
Block a user