Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | 26x 26x 26x 26x 26x 26x 26x 26x 16x 16x 2x 2x 2x 3x 3x 3x 16x 1x 1x 23x 2x 2x 2x 2x 2x 23x 23x 23x 1x 20x 20x 20x 20x 1x 20x 20x 20x 1x 1x 1x 19x 19x 1x 1x 1x 19x 1x 19x 19x 19x 19x 1x 16x 16x 1x 1x 1x 16x 1x 1x 16x 16x 16x 16x 5x 3x 3x 3x 3x 3x 3x 3x 1x 3x 2x 2x 2x 2x 3x 3x 3x 3x 3x 3x 3x 8x | /**
* Hugo Server
*
* Manages the Hugo development server process.
*/
import { spawn, ChildProcess } from 'child_process';
import fs from 'fs-extra';
import type { PathHelper } from '../../utils/path-helper.js';
import type { AppConfig } from '../../config/app-config.js';
import type { WindowAdapter, OutputConsole } from '../../adapters/types.js';
import type { AppContainer } from '../../config/container.js';
import { SITE_CATEGORIES } from '../../logging/categories.js';
/**
* Hugo server configuration
*/
export interface HugoServerConfig {
workspacePath: string;
hugover: string;
config?: string;
}
/**
* HugoServer - Manages Hugo development server
*/
export class HugoServer {
private config: HugoServerConfig;
private pathHelper: PathHelper;
private appConfig: AppConfig;
private windowAdapter: WindowAdapter;
private outputConsole: OutputConsole;
private container: AppContainer;
private siteKey: string;
private workspaceKey: string;
private currentServerProcess?: ChildProcess;
constructor(
config: HugoServerConfig,
pathHelper: PathHelper,
appConfig: AppConfig,
windowAdapter: WindowAdapter,
outputConsole: OutputConsole,
container: AppContainer,
siteKey: string,
workspaceKey: string
) {
this.config = config;
this.pathHelper = pathHelper;
this.appConfig = appConfig;
this.windowAdapter = windowAdapter;
this.outputConsole = outputConsole;
this.container = container;
this.siteKey = siteKey;
this.workspaceKey = workspaceKey;
}
/**
* Emit lines from a stream
*/
private emitLines(stream: NodeJS.ReadableStream): void {
let backlog = '';
stream.on('data', (data) => {
backlog += data;
let n = backlog.indexOf('\n');
// got a \n? emit one or more 'line' events
while (~n) {
stream.emit('line', backlog.substring(0, n));
backlog = backlog.substring(n + 1);
n = backlog.indexOf('\n');
}
});
stream.on('end', () => {
Eif (backlog) {
stream.emit('line', backlog);
}
});
}
/**
* Stop the server if it's running
*/
stopIfRunning(): void {
if (this.currentServerProcess) {
this.outputConsole.appendLine('Stopping Hugo Server...');
this.outputConsole.appendLine('');
// Log server shutdown
this.container.logger.infoSite(
this.siteKey,
this.workspaceKey,
SITE_CATEGORIES.BUILDACTION,
'Stopping Hugo Server',
{}
);
this.currentServerProcess.kill();
this.currentServerProcess = undefined;
}
try {
this.outputConsole.appendLine('Sending serverDown.');
this.windowAdapter.sendToRenderer('serverDown', {});
} catch (e) {
console.log('Failed to send serverDown message:', e);
}
}
/**
* Start the Hugo development server
*/
async serve(): Promise<void> {
const { config, workspacePath, hugover } = this.config;
try {
this.outputConsole.appendLine('Sending serverDown.');
this.windowAdapter.sendToRenderer('serverDown', {});
} catch (e) {
console.log('Failed to send serverDown message:', e);
}
this.stopIfRunning();
const exec = this.pathHelper.getSSGBinForVer('hugo', hugover);
if (!fs.existsSync(exec)) {
const error = 'Could not find hugo executable for version ' + hugover;
this.container.logger.errorSite(
this.siteKey,
this.workspaceKey,
SITE_CATEGORIES.BUILDACTION,
'Hugo executable not found',
{ errorCode: 'HUGO_EXEC_NOT_FOUND', hugover, exec }
);
throw new Error(error);
}
const hugoArgs = ['server', '--bind','0.0.0.0', '--port', '13131', '--disableFastRender'];
if (this.container.unifiedConfig.getInstanceSetting('hugo.serveDraftMode')) {
this.outputConsole.appendLine('Server Draft Mode Enabled...');
this.container.logger.infoSite(
this.siteKey,
this.workspaceKey,
SITE_CATEGORIES.BUILDACTION,
'Hugo server draft mode enabled',
{ draftMode: true }
);
hugoArgs.push('--buildDrafts');
}
if (config) {
hugoArgs.push('--config', config);
}
try {
this.currentServerProcess = spawn(exec, hugoArgs, {
cwd: workspacePath,
});
const { stdout, stderr } = this.currentServerProcess;
if (!stdout || !stderr) {
throw new Error('Failed to get stdout/stderr from Hugo process');
}
this.emitLines(stdout);
stderr.on('data', (data) => {
const errorMsg = String(data);
this.outputConsole.appendLine('Hugo Server Error: ' + errorMsg);
// Log Hugo errors
this.container.logger.errorSite(
this.siteKey,
this.workspaceKey,
SITE_CATEGORIES.BUILDACTION,
'Hugo server error',
{ errorCode: 'HUGO_SERVER_ERROR', error: errorMsg }
);
});
this.currentServerProcess.on('close', (code) => {
this.outputConsole.appendLine('Hugo Server Closed: ' + code);
// Log server close
this.container.logger.infoSite(
this.siteKey,
this.workspaceKey,
SITE_CATEGORIES.BUILDACTION,
'Hugo server closed',
{ exitCode: code }
);
});
stdout.setEncoding('utf8');
stdout.resume();
let isFirst = true;
stdout.on('line', (line: string) => {
if (isFirst) {
isFirst = false;
this.outputConsole.appendLine('Starting Hugo Server...');
this.outputConsole.appendLine('');
// Log server start
this.container.logger.infoSite(
this.siteKey,
this.workspaceKey,
SITE_CATEGORIES.BUILDACTION,
'Hugo Server started',
{ port: 13131, workspacePath }
);
try {
this.outputConsole.appendLine('Sending serverLive.');
this.windowAdapter.sendToRenderer('serverLive', {});
} catch {
this.outputConsole.appendLine('Failed to send serverLive message.');
}
return;
}
// Write to console
this.outputConsole.appendLine(line);
// Also log to structured logs
// Determine log level based on content
Iif (line.includes('ERROR') || line.includes('FATAL')) {
this.container.logger.errorSite(
this.siteKey,
this.workspaceKey,
SITE_CATEGORIES.BUILDACTION,
'Hugo output',
{ message: line }
);
I} else if (line.includes('WARN')) {
this.container.logger.warningSite(
this.siteKey,
this.workspaceKey,
SITE_CATEGORIES.BUILDACTION,
'Hugo output',
{ message: line }
);
} else {
// Regular info messages
this.container.logger.infoSite(
this.siteKey,
this.workspaceKey,
SITE_CATEGORIES.BUILDACTION,
'Hugo output',
{ message: line }
);
}
});
} catch (e) {
this.outputConsole.appendLine('Hugo Server failed to start.');
this.outputConsole.appendLine((e as Error).message);
// Log server start failure
this.container.logger.errorSite(
this.siteKey,
this.workspaceKey,
SITE_CATEGORIES.BUILDACTION,
'Hugo server failed to start',
{ errorCode: 'HUGO_START_FAILED', error: (e as Error).message }
);
try {
this.outputConsole.appendLine('Sending serverDown.');
this.windowAdapter.sendToRenderer('serverDown', {});
} catch {
this.outputConsole.appendLine('Failed to send serverDown message.');
}
throw e;
}
}
/**
* Get the current server process (if running)
*/
getCurrentProcess(): ChildProcess | undefined {
return this.currentServerProcess;
}
}
|