UNPKG

2.33 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const fs = require("fs");
4const path = require("path");
5const workspace_1 = require("@nrwl/workspace");
6const allMigrations = fs
7 .readdirSync(path.join(__dirname, '/../../migrations'))
8 .filter(f => f.endsWith('.js') && !f.endsWith('.d.js'))
9 .map(file => ({
10 migration: require(`../../migrations/${file}`).default,
11 name: path.parse(file).name
12}));
13const latestMigration = readLatestMigration();
14const migrationsToRun = calculateMigrationsToRun(allMigrations, latestMigration);
15if (migrationsToRun.length === 0) {
16 console.log('No migrations to run');
17 process.exit(0);
18}
19printMigrationsNames(latestMigration, migrationsToRun);
20runMigrations(migrationsToRun);
21updateLatestMigration();
22console.log('All migrations run successfully');
23function readLatestMigration() {
24 const angularCli = workspace_1.readWorkspaceConfigPath();
25 return angularCli.project.latestMigration;
26}
27function calculateMigrationsToRun(migrations, latestMigration) {
28 const startingWith = latestMigration
29 ? migrations.findIndex(item => item.name === latestMigration) + 1
30 : 0;
31 return migrations.slice(startingWith);
32}
33function printMigrationsNames(latestMigration, migrations) {
34 console.log(`Nx will run the following migrations (after ${latestMigration}):`);
35 migrations.forEach(m => {
36 console.log(`- ${m.name}`);
37 });
38 console.log('---------------------------------------------');
39}
40function runMigrations(migrations) {
41 migrations.forEach(m => {
42 try {
43 console.log(`Running ${m.name}`);
44 console.log(m.migration.description);
45 m.migration.run();
46 console.log('---------------------------------------------');
47 }
48 catch (e) {
49 console.error(`Migration ${m.name} failed`);
50 console.error(e);
51 console.error(`Please run 'git checkout .'`);
52 process.exit(1);
53 }
54 });
55}
56function updateLatestMigration() {
57 // we must reread .angular-cli.json because some of the migrations could have modified it
58 workspace_1.updateJsonFile('.angular-cli.json', angularCliJson => {
59 angularCliJson.project.latestMigration =
60 migrationsToRun[migrationsToRun.length - 1].name;
61 });
62}