All files / backend/dist/import folder-importer.js

0% Statements 0/30
0% Branches 0/12
0% Functions 0/3
0% Lines 0/30

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                                                                                                                                                                                       
/**
 * Folder Importer
 *
 * Imports Hugo sites from local directories.
 */
import fs from 'fs-extra';
import path from 'path';
import { pathIsDirectory, filenameFromPath } from '../utils/file-dir-utils.js';
import { InitialWorkspaceConfigBuilder } from '../services/workspace/initial-workspace-config-builder.js';
import { siteConfigSchema } from '@quiqr/types';
/**
 * FolderImporter - Imports Hugo sites from local directories
 */
export class FolderImporter {
    pathHelper;
    formatProviderResolver;
    libraryService;
    workspaceConfigProvider;
    constructor(pathHelper, formatProviderResolver, libraryService, workspaceConfigProvider) {
        this.pathHelper = pathHelper;
        this.formatProviderResolver = formatProviderResolver;
        this.libraryService = libraryService;
        this.workspaceConfigProvider = workspaceConfigProvider;
    }
    /**
     * Inspect a site directory to determine what type of site it is
     * and what components are present
     */
    async siteDirectoryInspect(folder) {
        const inventory = {
            dirExist: pathIsDirectory(folder),
            dirName: filenameFromPath(folder),
            hugoConfigExists: false,
            hugoConfigParsed: null,
            hugoThemesDirExists: pathIsDirectory(path.join(folder, 'themes')),
            hugoContentDirExists: pathIsDirectory(path.join(folder, 'content')),
            hugoDataDirExists: pathIsDirectory(path.join(folder, 'data')),
            hugoStaticDirExists: pathIsDirectory(path.join(folder, 'static')),
            quiqrModelDirExists: pathIsDirectory(path.join(folder, 'quiqr', 'model')),
            quiqrFormsDirExists: pathIsDirectory(path.join(folder, 'quiqr', 'forms')),
            quiqrDirExists: pathIsDirectory(path.join(folder, 'quiqr')),
            quiqrModelParsed: null,
        };
        // Check if Quiqr model exists and parse it
        const quiqrModelPath = this.workspaceConfigProvider.getQuiqrModelBasePath(folder);
        if (quiqrModelPath) {
            inventory.quiqrModelParsed = await this.workspaceConfigProvider.readOrCreateMinimalModelConfig(folder, 'source');
        }
        // Check if Hugo config exists and parse it
        const hugoConfigFilePath = this.pathHelper.hugoConfigFilePath(folder);
        if (hugoConfigFilePath) {
            const strData = fs.readFileSync(hugoConfigFilePath, { encoding: 'utf-8' });
            const formatProvider = this.formatProviderResolver.resolveForFilePath(hugoConfigFilePath);
            if (formatProvider) {
                const config = formatProvider.parse(strData);
                const result = siteConfigSchema.safeParse(config);
                inventory.hugoConfigParsed = null;
                inventory.hugoConfigExists = false;
                if (result.data) {
                    inventory.hugoConfigParsed = result.data;
                    inventory.hugoConfigExists = true;
                }
            }
        }
        return inventory;
    }
    /**
     * Create a new site from a local directory
     *
     * @param directory - Path to the directory containing the site
     * @param siteName - Name for the new site
     * @param generateQuiqrModel - Whether to generate Quiqr model files
     * @param hugoVersion - Hugo version to use
     * @returns The site key of the newly created site
     */
    async newSiteFromLocalDirectory(directory, siteName, generateQuiqrModel, hugoVersion) {
        const siteKey = await this.libraryService.createSiteKeyFromName(siteName);
        // Copy directory to temp location
        const tempCopyDir = path.join(this.pathHelper.getTempDir(), 'siteFromDir');
        fs.removeSync(tempCopyDir);
        await fs.copy(directory, tempCopyDir);
        // Generate Quiqr model if requested
        if (generateQuiqrModel && hugoVersion) {
            const configBuilder = new InitialWorkspaceConfigBuilder(tempCopyDir, this.formatProviderResolver, this.pathHelper);
            configBuilder.buildAll('hugo', hugoVersion);
        }
        // Create the site from the temp directory
        await this.libraryService.createNewSiteWithTempDirAndKey(siteKey, tempCopyDir);
        return siteKey;
    }
}