UNPKG

morpheus4j

Version:

Morpheus is a migration tool for Neo4j. It aims to be a simple and intuitive way to migrate your database.

94 lines 3.94 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FileService = void 0; const fs_extra_1 = require("fs-extra"); const node_path_1 = __importDefault(require("node:path")); const constants_1 = require("../constants"); const errors_1 = require("../errors"); class FileService { config; migrationsPath; constructor(config) { this.config = config; this.migrationsPath = this.resolveMigrationsPath(); } compareVersions(v1, v2) { return v1.localeCompare(v2, undefined, { numeric: true, sensitivity: 'base', }); } async getFileContent(fileName) { const filePath = node_path_1.default.join(this.migrationsPath, fileName); if (!(await (0, fs_extra_1.pathExists)(filePath))) { throw new errors_1.MigrationError(`Missing migration: ${fileName}. Neo4j reports it as applied, but it is missing locally.`); } try { return await (0, fs_extra_1.readFile)(filePath, 'utf8'); } catch (error) { throw new errors_1.MigrationError(`Failed to read migration file ${fileName}: ${error}`); } } async getFileNamesFromMigrationsFolder() { try { await this.createMigrationsFolder(); const files = await (0, fs_extra_1.readdir)(this.migrationsPath, { withFileTypes: true }); return files .filter((file) => file.isFile() && this.isValidMigrationFile(file.name)) .map((file) => file.name) .sort(); } catch (error) { throw new errors_1.MigrationError(`Failed to read migrations folder: ${error}`); } } getMigrationDescriptionFromFileName(fileName) { const result = fileName.match(constants_1.MIGRATION_NAME_REGEX); if (!result?.groups?.description) { throw new errors_1.MigrationError(`Invalid or missing description in migration file name: ${fileName}`); } return result.groups.description; } getMigrationVersionFromFileName(fileName) { const result = fileName.match(constants_1.MIGRATION_NAME_REGEX); if (!result?.groups?.version) { throw new errors_1.MigrationError(`Invalid or missing version in migration file name: ${fileName}`); } return result.groups.version.replaceAll('_', '.'); } async prepareMigration(fileName) { const fileContent = await this.getFileContent(fileName); const version = this.getMigrationVersionFromFileName(fileName); const description = this.getMigrationDescriptionFromFileName(fileName); const statements = this.splitFileContentIntoStatements(fileContent); return { description, statements, version }; } async createMigrationsFolder() { try { await (0, fs_extra_1.ensureDir)(this.migrationsPath); } catch (error) { throw new errors_1.MigrationError(`Failed to create migrations folder: ${error}`); } } isValidMigrationFile(fileName) { const extension = node_path_1.default.extname(fileName).toLowerCase(); return constants_1.VALID_FILE_EXTENSIONS.includes(extension) && constants_1.MIGRATION_NAME_REGEX.test(fileName); } resolveMigrationsPath() { const basePath = this.config.migrationsPath ?? constants_1.DEFAULT_MIGRATIONS_PATH; return node_path_1.default.resolve(process.cwd(), basePath); } splitFileContentIntoStatements(fileContent) { return fileContent .split(/;(?:\r?\n|\r)/) .map((statement) => statement.trim().replace(/;$/, '')) .filter(Boolean); } } exports.FileService = FileService; //# sourceMappingURL=file.service.js.map