All files / backend/dist/sync/folder folder-sync.js

0% Statements 0/67
0% Branches 0/21
0% Functions 0/11
0% Lines 0/66

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                                                                                                                                                                                                                                                                                                           
/**
 * 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 { recurForceRemove } from '../../utils/file-dir-utils.js';
/**
 * FolderSync - Syncs site to a local folder
 */
export class FolderSync {
    config;
    siteKey;
    pathHelper;
    outputConsole;
    windowAdapter;
    configurationProvider;
    progressCallback;
    constructor(config, siteKey, dependencies) {
        this.config = config;
        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) {
        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)
     */
    async pullFastForwardMerge() {
        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)
     */
    async publish() {
        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
     */
    async publishBuild(fullDestinationPath, from) {
        await this.syncSourceToDestination(path.join(from, 'public'), fullDestinationPath);
        await this.removeUnwanted(fullDestinationPath);
        this.outputConsole.appendLine('prepare and sync finished');
        return true;
    }
    /**
     * Publish source files
     */
    async publishSource(fullDestinationPath, from) {
        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
     */
    async removeUnwanted(fullDestinationPath) {
        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
     */
    async syncSourceToDestination(sourcePath, fullDestinationPath) {
        await fs.copy(sourcePath, fullDestinationPath);
        this.outputConsole.appendLine('synced source to destination ...');
    }
    /**
     * Ensure sync directory is clean and ready
     */
    async ensureSyncDir(dir) {
        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
     */
    sendProgress(message, progress) {
        if (this.progressCallback) {
            this.progressCallback(message, progress);
        }
        else {
            this.windowAdapter.sendToRenderer('updateProgress', { message, progress });
        }
    }
}