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 | /**
* Folder Sync Service
*
* Syncs built site to a local folder (no git operations).
* Used for local deployments or simple file-based publishing.
*/
import path from 'path';
import fs from 'fs-extra';
import type { PublConf } from '@quiqr/types';
import type { SyncService, SyncServiceDependencies, SyncProgressCallback } from '../sync-factory.js';
import type { PathHelper } from '../../utils/path-helper.js';
import type { OutputConsole, WindowAdapter } from '../../adapters/types.js';
import type { ConfigurationDataProvider } from '../../services/configuration/index.js';
import { recurForceRemove } from '../../utils/file-dir-utils.js';
/**
* Configuration specific to folder sync
*/
export interface FolderSyncConfig {
type: 'folder';
path?: string;
publishScope: 'build' | 'source';
overrideBaseURLSwitch?: boolean;
overrideBaseURL?: string;
}
/**
* FolderSync - Syncs site to a local folder
*/
export class FolderSync implements SyncService {
private config: FolderSyncConfig;
private siteKey: string;
private pathHelper: PathHelper;
private outputConsole: OutputConsole;
private windowAdapter: WindowAdapter;
private configurationProvider: ConfigurationDataProvider;
private progressCallback?: SyncProgressCallback;
constructor(
config: PublConf,
siteKey: string,
dependencies: SyncServiceDependencies
) {
this.config = config as FolderSyncConfig;
this.siteKey = siteKey;
this.pathHelper = dependencies.pathHelper;
this.outputConsole = dependencies.outputConsole;
this.windowAdapter = dependencies.windowAdapter;
this.configurationProvider = dependencies.configurationProvider;
this.progressCallback = dependencies.progressCallback;
}
/**
* Dispatch sync actions
*/
async actionDispatcher(action: string): Promise<unknown> {
switch (action) {
case 'pullFromRemote':
return this.pullFastForwardMerge();
case 'pushToRemote':
return this.publish();
default:
throw new Error(`Action not implemented: ${action}`);
}
}
/**
* Pull from remote (sync from destination to source)
*/
private async pullFastForwardMerge(): Promise<string> {
if (!this.config.path) {
throw new Error('Folder sync path is not configured');
}
const configurations = await this.configurationProvider.getConfigurations({});
const site = configurations.sites.find((x) => x.key === this.siteKey);
if (!site || !site.source?.path) {
throw new Error(`Site not found or invalid source path: ${this.siteKey}`);
}
await this.ensureSyncDir(site.source.path);
await this.syncSourceToDestination(this.config.path, site.source.path);
return 'reset-and-pulled-from-remote';
}
/**
* Publish to remote (sync from source to destination)
*/
private async publish(): Promise<boolean> {
if (!this.config.path) {
throw new Error('Folder sync path is not configured');
}
const destPath = this.config.path!; // Non-null assertion - checked above
await this.ensureSyncDir(destPath);
const from = this.pathHelper.getLastBuildDir();
if (!from) {
throw new Error('Could not determine last build directory');
}
this.outputConsole.appendLine('START FOLDER SYNC');
this.outputConsole.appendLine('-----------------');
this.outputConsole.appendLine(' from is: ' + from);
this.outputConsole.appendLine('');
this.outputConsole.appendLine(' destination path: ' + destPath);
this.outputConsole.appendLine(' override BaseURL: ' + (this.config.overrideBaseURL || ''));
this.outputConsole.appendLine('-----------------');
this.outputConsole.appendLine('');
this.sendProgress('Prepare files before uploading..', 30);
if (this.config.publishScope === 'build') {
await this.publishBuild(destPath, from);
} else {
await this.publishSource(destPath, from);
}
return true;
}
/**
* Publish built files only
*/
private async publishBuild(fullDestinationPath: string, from: string): Promise<boolean> {
await this.syncSourceToDestination(path.join(from, 'public'), fullDestinationPath);
await this.removeUnwanted(fullDestinationPath);
this.outputConsole.appendLine('prepare and sync finished');
return true;
}
/**
* Publish source files
*/
private async publishSource(fullDestinationPath: string, from: string): Promise<boolean> {
await this.syncSourceToDestination(from, fullDestinationPath);
await this.removeUnwanted(fullDestinationPath);
if (this.config.publishScope === 'source') {
await recurForceRemove(path.join(fullDestinationPath, 'public'));
}
await fs.ensureDir(path.join(fullDestinationPath, 'static'));
this.outputConsole.appendLine('prepare and sync finished');
return true;
}
/**
* Remove unwanted files from destination
*/
private async removeUnwanted(fullDestinationPath: string): Promise<void> {
await recurForceRemove(path.join(fullDestinationPath, '.quiqr-cache'));
await recurForceRemove(path.join(fullDestinationPath, '.gitlab-ci.yml'));
await recurForceRemove(path.join(fullDestinationPath, '.gitignore'));
await recurForceRemove(path.join(fullDestinationPath, '.sukoh'));
await recurForceRemove(path.join(fullDestinationPath, '.hugo_build.lock'));
await recurForceRemove(path.join(fullDestinationPath, '.git'));
}
/**
* Sync source to destination
*/
private async syncSourceToDestination(
sourcePath: string,
fullDestinationPath: string
): Promise<void> {
await fs.copy(sourcePath, fullDestinationPath);
this.outputConsole.appendLine('synced source to destination ...');
}
/**
* Ensure sync directory is clean and ready
*/
private async ensureSyncDir(dir: string): Promise<string> {
await fs.ensureDir(dir);
await fs.emptyDir(dir);
await fs.ensureDir(dir);
return dir;
}
/**
* Send progress update via SSE callback or fall back to window adapter
*/
private sendProgress(message: string, progress: number): void {
if (this.progressCallback) {
this.progressCallback(message, progress);
} else {
this.windowAdapter.sendToRenderer('updateProgress', { message, progress });
}
}
}
|