UNPKG

@deepbrainspace/nx-surrealdb

Version:

NX plugin for SurrealDB migrations with modular architecture

97 lines (95 loc) • 4.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = default_1; const tslib_1 = require("tslib"); const devkit_1 = require("@nx/devkit"); const path = tslib_1.__importStar(require("path")); const fs = tslib_1.__importStar(require("fs")); const child_process_1 = require("child_process"); const config_loader_1 = require("../../lib/configuration/config-loader"); const tree_utils_1 = require("../../lib/filesystem/tree-utils"); async function default_1(tree, options) { const normalizedOptions = normalizeOptions(tree, options); devkit_1.logger.info(`šŸš€ Exporting module: ${normalizedOptions.module}`); // Find the module directory const moduleDir = await findModuleDirectory(tree, normalizedOptions); if (!moduleDir) { throw new Error(`Module '${normalizedOptions.module}' not found in ${normalizedOptions.initPath}`); } devkit_1.logger.info(`šŸ“ Found module directory: ${moduleDir.name}`); // Load configuration to get module metadata await loadModuleConfig(normalizedOptions, moduleDir.name); // Skip complex package creation - we'll just tar the module files directly // Skip formatFiles for this generator since it causes test timeouts return () => { // Create simple tar archive directly from module files try { createSimpleArchive(normalizedOptions, moduleDir); devkit_1.logger.info(`āœ… Module '${moduleDir.name}' exported successfully!`); devkit_1.logger.info(`šŸ“¦ Archive: ${moduleDir.name}.tar.gz`); devkit_1.logger.info(` šŸŽ‰ Export completed! Module: ${moduleDir.name} Archive: ${moduleDir.name}.tar.gz To import this module in another project: nx g @deepbrainspace/nx-surrealdb:import-module --module=${moduleDir.name} --packagePath=${moduleDir.name}.tar.gz --initPath=path/to/db `); } catch (error) { devkit_1.logger.error(`āŒ Export failed: ${error.message}`); } }; } function normalizeOptions(tree, options) { return { ...options, module: String(options.module), outputPath: options.outputPath || 'exported-modules', includeConfig: options.includeConfig ?? true, packageFormat: options.packageFormat || 'tar', initPath: options.initPath || 'database', configPath: options.configPath || '', description: options.description || '', }; } async function findModuleDirectory(tree, options) { if (!tree.exists(options.initPath)) { throw new Error(`Migrations directory not found: ${options.initPath}`); } const moduleName = tree_utils_1.TreeUtils.findMatchingSubdirectory(tree, options.initPath, options.module); if (!moduleName) { return null; } return { name: moduleName, path: path.join(options.initPath, moduleName), }; } // Use shared TreeUtils for finding subdirectories async function loadModuleConfig(options, moduleName) { try { const configPath = options.configPath || 'config.json'; const fullConfigPath = path.isAbsolute(configPath) ? configPath : path.join(options.initPath, configPath); if (fs.existsSync(fullConfigPath)) { const config = await config_loader_1.ConfigLoader.loadConfig(options.initPath, configPath); return config.modules[moduleName] || null; } return null; } catch (error) { devkit_1.logger.warn(`āš ļø Could not load module configuration: ${error.message}`); return null; } } function createSimpleArchive(options, moduleDir) { const archiveName = `${moduleDir.name}.tar.gz`; // Create tar archive directly from the source module directory // This will include all .surql files in the module without extra wrapper directories (0, child_process_1.execSync)(`tar -czf ${archiveName} -C ${path.dirname(moduleDir.path)} ${moduleDir.name}`, { stdio: 'inherit', }); devkit_1.logger.info(`šŸ“¦ Created simple archive: ${archiveName}`); } //# sourceMappingURL=generator.js.map