UNPKG

@nrwl/schematics

Version:

Extensible Dev Tools for Monorepos: Schematics

63 lines (62 loc) 2.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const fs = require("fs"); const path = require("path"); const workspace_1 = require("@nrwl/workspace"); const allMigrations = fs .readdirSync(path.join(__dirname, '/../../migrations')) .filter(f => f.endsWith('.js') && !f.endsWith('.d.js')) .map(file => ({ migration: require(`../../migrations/${file}`).default, name: path.parse(file).name })); const latestMigration = readLatestMigration(); const migrationsToRun = calculateMigrationsToRun(allMigrations, latestMigration); if (migrationsToRun.length === 0) { console.log('No migrations to run'); process.exit(0); } printMigrationsNames(latestMigration, migrationsToRun); runMigrations(migrationsToRun); updateLatestMigration(); console.log('All migrations run successfully'); function readLatestMigration() { const angularCli = workspace_1.readWorkspaceConfigPath(); return angularCli.project.latestMigration; } function calculateMigrationsToRun(migrations, latestMigration) { const startingWith = latestMigration ? migrations.findIndex(item => item.name === latestMigration) + 1 : 0; return migrations.slice(startingWith); } function printMigrationsNames(latestMigration, migrations) { console.log(`Nx will run the following migrations (after ${latestMigration}):`); migrations.forEach(m => { console.log(`- ${m.name}`); }); console.log('---------------------------------------------'); } function runMigrations(migrations) { migrations.forEach(m => { try { console.log(`Running ${m.name}`); console.log(m.migration.description); m.migration.run(); console.log('---------------------------------------------'); } catch (e) { console.error(`Migration ${m.name} failed`); console.error(e); console.error(`Please run 'git checkout .'`); process.exit(1); } }); } function updateLatestMigration() { // we must reread .angular-cli.json because some of the migrations could have modified it workspace_1.updateJsonFile('.angular-cli.json', angularCliJson => { angularCliJson.project.latestMigration = migrationsToRun[migrationsToRun.length - 1].name; }); }