@ts-dev-tools/core
Version:
TS dev tools Core
108 lines (107 loc) • 4.97 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.MigrationsService = void 0;
const node_fs_1 = require("node:fs");
const node_path_1 = require("node:path");
const PackageJson_1 = require("../services/PackageJson");
const PluginService_1 = require("./PluginService");
class MigrationsService {
constructor() { }
static MIGRATION_BUILT_PATH = "dist/install/migrations";
static async executeMigrations(absoluteProjectDir, currentVersion) {
const migrations = MigrationsService.getAvailableMigrations(absoluteProjectDir, currentVersion);
const packageJson = PackageJson_1.PackageJson.fromDirPath(absoluteProjectDir);
const packageJsonBackupPath = packageJson.backup();
try {
for (const migration of migrations) {
console.info(`Applying migration "${migration.fullname}"...`);
const { up } = await Promise.resolve(`${migration.path}`).then(s => __importStar(require(s)));
// Apply migration
await up(absoluteProjectDir);
// Upgrade current version
PackageJson_1.PackageJson.fromDirPath(absoluteProjectDir).merge({
tsDevTools: { version: migration.shortname },
});
console.info(`Migration "${migration.fullname}" applied!`);
}
}
catch (error) {
// Rollback package.json
packageJson.restore(packageJsonBackupPath);
throw error;
}
if ((0, node_fs_1.existsSync)(packageJsonBackupPath)) {
(0, node_fs_1.unlinkSync)(packageJsonBackupPath);
}
}
static getAvailableMigrations(absoluteProjectDir, currentVersion) {
const installedPlugins = PluginService_1.PluginService.getInstalledPlugins(absoluteProjectDir);
const migrationFiles = [];
for (const installedPlugin of installedPlugins) {
migrationFiles.push(...MigrationsService.getPluginMigrations(installedPlugin, currentVersion));
}
migrationFiles.sort(({ shortname: nameA }, { shortname: nameB }) => nameA.localeCompare(nameB));
return Array.from(new Set(migrationFiles));
}
static getPluginMigrations(plugin, currentVersion) {
const migrationFiles = [];
const pluginMigrationsDirPath = (0, node_path_1.resolve)(plugin.path, MigrationsService.MIGRATION_BUILT_PATH);
for (const migrationFile of (0, node_fs_1.readdirSync)(pluginMigrationsDirPath)) {
if (!migrationFile.match(/^[0-9]{14}-[-a-z]+\.(js|ts)$/)) {
continue;
}
const migrationName = MigrationsService.getMigrationNameFromFile(migrationFile);
const shouldApplyMigration = MigrationsService.migrationIsAfterCurrentVersion(migrationName, currentVersion);
if (!shouldApplyMigration) {
continue;
}
migrationFiles.push({
shortname: migrationName,
fullname: `${plugin.shortname} - ${migrationName}`,
path: (0, node_path_1.resolve)(pluginMigrationsDirPath, migrationFile),
});
}
return migrationFiles;
}
static getMigrationNameFromFile(migrationFile) {
const migrationName = migrationFile.split(".").slice(0, -1).join(".");
return migrationName;
}
static migrationIsAfterCurrentVersion(migrationName, currentVersion) {
return !currentVersion || currentVersion.localeCompare(migrationName) < 0;
}
}
exports.MigrationsService = MigrationsService;