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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 | 9x 9x 9x 9x 9x 33x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 10x 10x 9x 10x 10x 10x 10x 10x 10x 10x 2x 10x 2x 2x 10x 10x 10x 10x 10x 10x 10x 9x 10x 9x 9x 9x 9x 9x | /**
* Dependency Injection Container
*
* Central container for all application dependencies.
* Replaces scattered global variables with clean dependency injection.
*/
import type { PlatformAdapters } from '../adapters/types.js';
import { AppConfig } from './app-config.js';
import { AppState } from './app-state.js';
import { createUnifiedConfigService, type UnifiedConfigService } from './unified-config-service.js';
import { PathHelper } from '../utils/path-helper.js';
import { FormatProviderResolver } from '../utils/format-provider-resolver.js';
import { ConfigurationDataProvider, ConsoleLogger } from '../services/configuration/index.js';
import { LibraryService } from '../services/library/library-service.js';
import { SyncFactory } from '../sync/sync-factory.js';
import { SiteSourceFactory } from '../site-sources/site-source-factory.js';
import { WorkspaceConfigProvider } from '../services/workspace/workspace-config-provider.js';
import { FolderImporter } from '../import/folder-importer.js';
import { GitImporter } from '../import/git-importer.js';
import { Pogozipper } from '../import/pogozipper.js';
import { Embgit } from '../embgit/embgit.js';
import { WorkspaceService, type WorkspaceServiceDependencies } from '../services/workspace/workspace-service.js';
import { ModelWatcher, createModelWatcher } from '../services/workspace/model-watcher.js';
import { ScaffoldModelService, createScaffoldModelService } from '../services/scaffold-model/index.js';
import { BuildActionService } from '../build-actions/index.js';
import { ProviderFactory } from '../ssg-providers/provider-factory.js';
import type { EnvironmentInfo } from '../utils/path-helper.js';
import { HugoDownloader } from '../ssg-providers/hugo/hugo-downloader.js';
import { HugoUtils } from '../ssg-providers/hugo/hugo-utils.js';
import { Logger } from '../logging/logger.js';
import type { AuthProvider } from '../auth/types.js';
/**
* Event emitted when the model cache is cleared
*/
export interface ModelChangeEvent {
type: 'model-cache-cleared';
siteKey: string;
workspaceKey: string;
}
/**
* Main application container with all dependencies
*/
export interface AppContainer {
/**
* Application configuration (persistent) - LEGACY
* @deprecated Use unifiedConfig for new code
*/
config: AppConfig;
/**
* Unified configuration service (hierarchical, multi-user ready)
* Provides layered config resolution with env var overrides
*/
unifiedConfig: UnifiedConfigService;
/**
* Application runtime state (non-persistent)
*/
state: AppState;
/**
* Platform adapters (Electron, CLI, Web, etc.)
*/
adapters: PlatformAdapters;
/**
* Path helper utilities
*/
pathHelper: PathHelper;
/**
* Format provider resolver (JSON, YAML, TOML)
*/
formatResolver: FormatProviderResolver;
/**
* Configuration data provider (site configs)
*/
configurationProvider: ConfigurationDataProvider;
/**
* SSG provider factory (creates SSG provider instances)
*/
providerFactory: ProviderFactory;
/**
* Library service (site management and CRUD operations)
*/
libraryService: LibraryService;
/**
* Sync factory (creates sync service instances)
*/
syncFactory: SyncFactory;
/**
* Site source factory (creates site source instances)
*/
siteSourceFactory: SiteSourceFactory;
/**
* Workspace config provider (loads workspace configs)
*/
workspaceConfigProvider: WorkspaceConfigProvider;
/**
* Folder importer (imports sites from local directories)
*/
folderImporter: FolderImporter;
/**
* Embgit (embedded git client)
*/
embgit: Embgit;
/**
* Git importer (imports sites from git repositories)
*/
gitImporter: GitImporter;
/**
* Pogozipper (ZIP-based import/export for sites, themes, content)
*/
pogozipper: Pogozipper;
/**
* Environment information (platform, packaging status)
*/
environmentInfo: EnvironmentInfo;
/**
* Factory function to create WorkspaceService instances
* WorkspaceService is per-workspace, not a singleton
*/
createWorkspaceService: (
workspacePath: string,
workspaceKey: string,
siteKey: string
) => WorkspaceService;
/**
* Helper to get a WorkspaceService instance
* Handles the common pattern of: get site → get workspace → create service
*/
getWorkspaceService: (
siteKey: string,
workspaceKey: string
) => Promise<WorkspaceService>;
/**
* Get the currently cached WorkspaceService (for operations that need persistence like Hugo server)
* Returns undefined if no workspace is currently cached
*/
getCurrentWorkspaceService: () => WorkspaceService | undefined;
/**
* Event broadcaster for model change notifications (SSE)
* Used to notify frontend when model files change
*/
modelChangeEventBroadcaster: {
emit: (event: ModelChangeEvent) => void;
subscribe: (callback: (event: ModelChangeEvent) => void) => () => void;
};
/**
* @deprecated Use providerFactory.getProvider('hugo').getBinaryManager() instead
* Backward compatibility accessor for Hugo downloader
*/
get hugoDownloader(): HugoDownloader;
/**
* @deprecated Use providerFactory.getProvider('hugo') for site creation instead
* Backward compatibility accessor for Hugo utils
*/
get hugoUtils(): HugoUtils;
/**
* Structured logging service
*/
logger: Logger;
/**
* Auth provider (optional — only set when auth is enabled in standalone mode)
*/
authProvider?: AuthProvider;
/**
* Helper to get a ScaffoldModelService instance
* Handles the common pattern of: get site → get workspace → create service
*/
getScaffoldModelService: (
siteKey: string,
workspaceKey: string
) => Promise<ScaffoldModelService>;
}
/**
* Container creation options
*/
export interface ContainerOptions {
/**
* Path to user data directory
*/
userDataPath: string;
/**
* Root path of the application
*/
rootPath: string;
/**
* Platform adapters implementation
*/
adapters: PlatformAdapters;
/**
* Optional config file name (defaults to 'quiqr-app-config.json')
*/
configFileName?: string;
}
/**
* Create the application container with all dependencies
*/
export function createContainer(options: ContainerOptions): AppContainer {
const { userDataPath, rootPath, adapters, configFileName } = options;
// Create unified config service (new architecture)
// Relies on hardcoded defaults for initial values
const unifiedConfig = createUnifiedConfigService({ configDir: userDataPath });
// Create legacy config and state (for backward compatibility)
const config = new AppConfig(userDataPath, configFileName);
const state = new AppState();
// Create path helper with dynamic config getter
// This ensures PathHelper always reads the current dataFolder from UnifiedConfigService
const pathHelper = new PathHelper(
adapters.appInfo,
rootPath,
() => ({
dataFolder: unifiedConfig.getInstanceSetting('storage.dataFolder') as string,
currentSitePath: state.currentSitePath,
})
);
// Create format resolver
const formatResolver = new FormatProviderResolver();
// Create structured logger
const structuredLogger = new Logger();
// Create configuration provider with dependencies
const logger = new ConsoleLogger();
const configurationProvider = new ConfigurationDataProvider(
pathHelper,
formatResolver,
logger
);
// Create factories
const syncFactory = new SyncFactory();
const siteSourceFactory = new SiteSourceFactory(pathHelper);
// Initialize syncFactory with dependencies (will be set after configurationProvider is created)
// This is done below after all dependencies are available
// Create environment info from platform
const environmentInfo: EnvironmentInfo = {
platform:
process.platform === 'darwin' ? 'macOS' as const :
process.platform === 'win32' ? 'windows' as const :
'linux' as const,
isPackaged: adapters.appInfo.isPackaged(),
};
// Create provider factory for SSG providers
const providerFactory = new ProviderFactory({
pathHelper,
environmentInfo,
outputConsole: adapters.outputConsole,
windowAdapter: adapters.window,
shellAdapter: adapters.shell,
appConfig: config,
});
// Create workspace config provider
const workspaceConfigProvider = new WorkspaceConfigProvider(
formatResolver,
pathHelper,
unifiedConfig,
environmentInfo
);
// Create model change event broadcaster for SSE notifications
const modelChangeSubscribers = new Set<(event: ModelChangeEvent) => void>();
const modelChangeEventBroadcaster = {
emit: (event: ModelChangeEvent) => {
modelChangeSubscribers.forEach((cb) => cb(event));
},
subscribe: (callback: (event: ModelChangeEvent) => void) => {
modelChangeSubscribers.add(callback);
return () => {
modelChangeSubscribers.delete(callback);
};
},
};
// Create the container object first (needed for circular dependency)
const container: AppContainer = {
config,
unifiedConfig,
state,
adapters,
pathHelper,
formatResolver,
configurationProvider,
providerFactory,
syncFactory,
siteSourceFactory,
workspaceConfigProvider,
environmentInfo,
modelChangeEventBroadcaster,
logger: structuredLogger,
} as AppContainer;
// Create library service with container dependency
const libraryService = new LibraryService(container);
container.libraryService = libraryService;
// Create folder importer with all dependencies
const folderImporter = new FolderImporter(
pathHelper,
formatResolver,
libraryService,
workspaceConfigProvider
);
container.folderImporter = folderImporter;
// Create embgit with dependencies
const embgit = new Embgit(
pathHelper,
adapters.outputConsole,
adapters.appInfo,
rootPath,
environmentInfo
);
container.embgit = embgit;
// Create git importer with dependencies
const gitImporter = new GitImporter(
embgit,
pathHelper,
formatResolver,
libraryService
);
container.gitImporter = gitImporter;
// Create pogozipper with dependencies
const pogozipper = new Pogozipper(
pathHelper,
libraryService,
adapters.dialog,
adapters.window
);
container.pogozipper = pogozipper;
// Initialize syncFactory with dependencies
syncFactory.setDependencies({
pathHelper,
outputConsole: adapters.outputConsole,
windowAdapter: adapters.window,
configurationProvider,
embgit,
container,
});
// Initialize providerFactory with container reference (breaks circular dependency)
providerFactory.setContainer(container);
// Create BuildActionService (uses outputConsole for logging and container for structured logging)
const buildActionService = new BuildActionService(adapters.outputConsole, container);
// Create WorkspaceService factory
// Note: OutputConsole and ScreenshotWindowManager are provided by Electron runtime
container.createWorkspaceService = (
workspacePath: string,
workspaceKey: string,
siteKey: string
) => {
const dependencies: WorkspaceServiceDependencies = {
workspaceConfigProvider,
formatProviderResolver: formatResolver,
pathHelper,
appConfig: config,
appState: state,
providerFactory,
windowAdapter: adapters.window,
shellAdapter: adapters.shell,
outputConsole: adapters.outputConsole,
screenshotWindowManager: adapters.screenshotWindowManager,
buildActionService,
container,
};
return new WorkspaceService(workspacePath, workspaceKey, siteKey, dependencies);
};
// Cache for the current WorkspaceService (needed for Hugo server persistence)
let cachedWorkspaceService: WorkspaceService | undefined;
let cachedWorkspaceKey: string | undefined;
let cachedSiteKey: string | undefined;
let currentModelWatcher: ModelWatcher | undefined;
// Helper to get WorkspaceService (common pattern used across handlers)
container.getWorkspaceService = async (
siteKey: string,
workspaceKey: string
): Promise<WorkspaceService> => {
// Import SiteService dynamically to avoid circular dependency
const { SiteService } = await import('../services/site/site-service.js');
// Get site configuration
const siteConfig = await libraryService.getSiteConf(siteKey);
// Create SiteService instance
const siteService = new SiteService(
siteConfig,
siteSourceFactory,
syncFactory
);
// Handle special workspace keys that should resolve to the default workspace
let resolvedWorkspaceKey = workspaceKey;
Iif (workspaceKey === 'source' || workspaceKey === 'default') {
// Resolve to the first available workspace
const workspaces = await siteService.listWorkspaces();
if (workspaces.length === 0) {
throw new Error(`No workspaces found for site: ${siteKey}`);
}
resolvedWorkspaceKey = workspaces[0].key;
}
// Return cached instance if it matches the requested workspace
Iif (
cachedWorkspaceService &&
cachedSiteKey === siteKey &&
cachedWorkspaceKey === resolvedWorkspaceKey
) {
return cachedWorkspaceService;
}
// Stop Hugo server and model watcher from the old workspace before switching
if (cachedWorkspaceService) {
cachedWorkspaceService.stopHugoServer();
}
if (currentModelWatcher) {
await currentModelWatcher.stop();
currentModelWatcher = undefined;
}
// Get workspace head to find the path
const workspaceHead = await siteService.getWorkspaceHead(resolvedWorkspaceKey);
Iif (!workspaceHead) {
throw new Error(`Workspace not found: ${resolvedWorkspaceKey} for site: ${siteKey}`);
}
// Create WorkspaceService and cache it
const workspaceService = container.createWorkspaceService(
workspaceHead.path,
resolvedWorkspaceKey,
siteKey
);
cachedWorkspaceService = workspaceService;
cachedSiteKey = siteKey;
cachedWorkspaceKey = resolvedWorkspaceKey;
// Start model watcher for the new workspace
currentModelWatcher = createModelWatcher({
workspacePath: workspaceHead.path,
workspaceConfigProvider,
onCacheCleared: () => {
container.modelChangeEventBroadcaster.emit({
type: 'model-cache-cleared',
siteKey,
workspaceKey: resolvedWorkspaceKey,
});
},
});
return workspaceService;
};
// Get the currently cached WorkspaceService
container.getCurrentWorkspaceService = () => {
return cachedWorkspaceService;
};
// Helper to get ScaffoldModelService
container.getScaffoldModelService = async (
siteKey: string,
workspaceKey: string
): Promise<ScaffoldModelService> => {
// First get the workspace service to ensure we have a valid workspace path
const workspaceService = await container.getWorkspaceService(siteKey, workspaceKey);
// Create and return a ScaffoldModelService instance
return createScaffoldModelService(
workspaceService.getWorkspacePath(),
{
dialogAdapter: adapters.dialog,
formatResolver,
}
);
};
// Backward compatibility accessors (deprecated)
Object.defineProperty(container, 'hugoDownloader', {
get: async () => {
const provider = await providerFactory.getProvider('hugo');
return provider.getBinaryManager();
},
enumerable: false,
});
Object.defineProperty(container, 'hugoUtils', {
get: async () => {
const provider = await providerFactory.getProvider('hugo');
return {
createSiteDir: (directory: string, title: string, configFormat: string) =>
provider.createSite({ directory, title, configFormat }),
};
},
enumerable: false,
});
return container;
}
/**
* Type helper to extract container from context
*/
export type Container = AppContainer;
|