UNPKG

@deepbrainspace/nx-surrealdb

Version:

NX plugin for SurrealDB migrations with modular architecture

111 lines (109 loc) • 4.6 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 tree_utils_1 = require("../../lib/filesystem/tree-utils"); async function default_1(tree, options) { const normalizedOptions = normalizeOptions(tree, options); devkit_1.logger.info(`šŸš€ Importing module: ${normalizedOptions.module}`); devkit_1.logger.info(`šŸ“¦ From package: ${normalizedOptions.packagePath}`); try { // Simple import: extract archive and copy files directly await simpleImport(tree, normalizedOptions); devkit_1.logger.info(`āœ… Module '${normalizedOptions.module}' imported successfully!`); return () => { devkit_1.logger.info(` šŸŽ‰ Import completed! Module: ${normalizedOptions.module} Location: ${normalizedOptions.initPath}/${normalizedOptions.module} Next steps: 1. Review the imported migrations in ${normalizedOptions.initPath}/${normalizedOptions.module} 2. Run: nx run your-project:migrate --module ${normalizedOptions.module} `); }; } catch (error) { devkit_1.logger.error(`āŒ Import failed: ${error.message}`); throw error; } } function normalizeOptions(tree, options) { return { ...options, targetModule: options.targetModule || '', targetNumber: options.targetNumber || 0, initPath: options.initPath || 'database', configPath: options.configPath || '', overwrite: options.overwrite ?? false, skipDependencyCheck: options.skipDependencyCheck ?? false, mergeConfig: options.mergeConfig ?? true, }; } async function simpleImport(tree, options) { const packagePath = path.resolve(options.packagePath); if (!fs.existsSync(packagePath)) { throw new Error(`Package not found: ${packagePath}`); } const stat = fs.statSync(packagePath); let sourceDir; if (stat.isDirectory()) { // Direct directory import devkit_1.logger.info(`šŸ“ Using directory package: ${packagePath}`); sourceDir = packagePath; } else if (packagePath.endsWith('.tar.gz') || packagePath.endsWith('.tgz')) { // Extract tar archive devkit_1.logger.info(`šŸ“¦ Extracting tar archive...`); const tempDir = path.join(process.cwd(), '.tmp', 'module-import', Date.now().toString()); fs.mkdirSync(tempDir, { recursive: true }); try { (0, child_process_1.execSync)(`tar -xzf "${packagePath}" -C "${tempDir}"`, { stdio: 'inherit' }); // Find the extracted module directory const extracted = fs.readdirSync(tempDir); if (extracted.length === 0) { throw new Error('Archive appears to be empty'); } sourceDir = path.join(tempDir, extracted[0]); } catch (error) { // Cleanup on error if (fs.existsSync(tempDir)) { fs.rmSync(tempDir, { recursive: true, force: true }); } throw error; } } else { throw new Error(`Unsupported package format: ${packagePath}`); } // Copy files directly to target location const targetDir = path.join(options.initPath, options.module); // Check if target already exists if (tree.exists(targetDir) && !options.overwrite) { throw new Error(`Target module '${options.module}' already exists. Use --overwrite to replace it.`); } if (tree.exists(targetDir) && options.overwrite) { devkit_1.logger.warn(`āš ļø Overwriting existing module: ${options.module}`); } // Create target directory tree_utils_1.TreeUtils.ensureDirectory(tree, targetDir); // Copy all .surql files from source to target const sourceFiles = fs.readdirSync(sourceDir); const copiedFiles = []; for (const file of sourceFiles) { if (file.endsWith('.surql')) { const sourceFilePath = path.join(sourceDir, file); const targetFilePath = path.join(targetDir, file); const content = fs.readFileSync(sourceFilePath, 'utf-8'); tree.write(targetFilePath, content); copiedFiles.push(file); devkit_1.logger.info(`šŸ“„ Imported: ${file}`); } } devkit_1.logger.info(`āœ… Imported ${copiedFiles.length} migration files`); } //# sourceMappingURL=generator.js.map