UNPKG

@monstrs/mctl-migration

Version:

82 lines (81 loc) 3.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); /* eslint-disable import/no-dynamic-require */ /* eslint-disable class-methods-use-this */ /* eslint-disable global-require */ const fs_1 = tslib_1.__importDefault(require("fs")); const path_1 = tslib_1.__importDefault(require("path")); const command_1 = require("@oclif/command"); const mctl_config_1 = require("@monstrs/mctl-config"); const lerna_1 = require("../../lerna"); const migrate_1 = require("../../migrate"); const schematic_1 = require("../../schematic"); class RunCommand extends command_1.Command { async run() { const configs = (await Promise.all([ new mctl_config_1.ConfigurationLoader().load(), ...(await lerna_1.getPackages()).map((pkg) => new mctl_config_1.ConfigurationLoader().load(pkg.location, pkg.location)), ])).filter(config => config && config.filepath); configs.forEach(async (config) => { const collections = this.expandCollections(config.config.collection, config.config.schematic).reverse(); collections.forEach(async ({ path: collectionPath, schematic }) => { if (schematic) { let migrations = []; try { migrations = this.getMigrations(collectionPath, schematic, config.config.migration); } catch (error) { } // eslint-disable-line no-empty if (migrations.length > 0) { await Promise.all(migrations.map(async (migration) => { await migrate_1.runMigrations(path_1.default.dirname(config.filepath), migration.collection, migration.schematic); })); this.updateConfig(config.filepath, Object.assign(Object.assign({}, config.config), { migration: new Date().getTime() })); } } }); }); } expandCollections(collectionName, schematicName) { const collections = schematic_1.expandCollection(collectionName).reverse(); const nested = collections.map((collection) => { const schematic = collection.description.schematics[schematicName]; if (schematic && schematic.extends) { const [extendsCollection, extendsSchematic] = schematic.extends.split(':'); return this.expandCollections(extendsCollection, extendsSchematic); } return []; }); const result = [ ...nested, collections.map((collection) => ({ schematic: collection.description.schematics[schematicName], path: collection.description.path, })), ]; return result.flat(); } getMigrations(schematicsAbsolutePath, schematic, migration) { const migrationsPath = path_1.default.join(path_1.default.dirname(schematicsAbsolutePath), path_1.default.dirname(schematic.schema), 'migrations.json'); const data = require(migrationsPath); const migrations = Object.keys(data.schematics) .map(key => ({ collection: migrationsPath, schematic: key, migration: data.schematics[key], })) .filter(config => config.migration.version > migration); return migrations; } updateConfig(configPath, config) { if (path_1.default.basename(configPath) !== 'package.json') { fs_1.default.writeFileSync(configPath, JSON.stringify(config, null, 2)); } } } exports.default = RunCommand; RunCommand.description = 'Migration generate'; RunCommand.examples = ['$ mctl migration:generate']; RunCommand.strict = false; RunCommand.flags = {}; RunCommand.args = [];