Skip to content

Commit 6ac7d83

Browse files
committed
chore(graph): refactor migration type checks
1 parent dc93bea commit 6ac7d83

File tree

6 files changed

+70
-117
lines changed

6 files changed

+70
-117
lines changed

graph/migrate/src/lib/components/automatic-migration.tsx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import type { MigrationsJsonMetadata } from 'nx/src/command-line/migrate/migrate
66
import { useSelector } from '@xstate/react';
77
import {
88
currentMigrationHasChanges,
9-
currentMigrationHasFailed,
10-
currentMigrationHasSucceeded,
11-
currentMigrationIsStopped,
9+
getCurrentMigrationType,
1210
} from '../state/automatic/selectors';
1311
import { MigrationTimeline } from './migration-timeline';
1412
import { Interpreter } from 'xstate';
@@ -46,15 +44,18 @@ export function AutomaticMigration(props: {
4644
(migration) => migration.id === currentMigration?.id
4745
);
4846

49-
const currentMigrationFailed = useSelector(props.actor, (state) =>
50-
currentMigrationHasFailed(state.context)
47+
const currentMigrationFailed = useSelector(
48+
props.actor,
49+
(state) => getCurrentMigrationType(state.context) === 'failed'
5150
);
5251

53-
const isCurrentMigrationStopped = useSelector(props.actor, (state) =>
54-
currentMigrationIsStopped(state.context)
52+
const isCurrentMigrationStopped = useSelector(
53+
props.actor,
54+
(state) => getCurrentMigrationType(state.context) === 'stopped'
5555
);
56-
const currentMigrationSuccess = useSelector(props.actor, (state) =>
57-
currentMigrationHasSucceeded(state.context)
56+
const currentMigrationSuccess = useSelector(
57+
props.actor,
58+
(state) => getCurrentMigrationType(state.context) === 'successful'
5859
);
5960

6061
const currentMigrationChanges = useSelector(props.actor, (state) =>

graph/migrate/src/lib/components/migration-card.tsx

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,8 @@ import type {
2828
import { useSelector } from '@xstate/react';
2929
import {
3030
currentMigrationHasChanges,
31-
isMigrationFailed,
31+
getMigrationType,
3232
isMigrationRunning,
33-
isMigrationSkipped,
34-
isMigrationStopped,
35-
isMigrationSuccessful,
3633
} from '../state/automatic/selectors';
3734

3835
export interface MigrationCardHandle {
@@ -127,17 +124,21 @@ export const MigrationCard = forwardRef<
127124
const nextSteps =
128125
migrationResult?.type === 'successful' ? migrationResult.nextSteps : [];
129126

130-
const isSucceeded = useSelector(actor, (state) =>
131-
isMigrationSuccessful(state.context, migration.id)
127+
const isSucceeded = useSelector(
128+
actor,
129+
(state) => getMigrationType(state.context, migration.id) === 'successful'
132130
);
133-
const isFailed = useSelector(actor, (state) =>
134-
isMigrationFailed(state.context, migration.id)
131+
const isFailed = useSelector(
132+
actor,
133+
(state) => getMigrationType(state.context, migration.id) === 'failed'
135134
);
136-
const isSkipped = useSelector(actor, (state) =>
137-
isMigrationSkipped(state.context, migration.id)
135+
const isSkipped = useSelector(
136+
actor,
137+
(state) => getMigrationType(state.context, migration.id) === 'skipped'
138138
);
139-
const isStopped = useSelector(actor, (state) =>
140-
isMigrationStopped(state.context, migration.id)
139+
const isStopped = useSelector(
140+
actor,
141+
(state) => getMigrationType(state.context, migration.id) === 'stopped'
141142
);
142143
const hasChanges = useSelector(actor, (state) =>
143144
currentMigrationHasChanges(state.context)

graph/migrate/src/lib/components/migration-timeline.tsx

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ import type {
2727
} from '../state/automatic/types';
2828
import { useSelector } from '@xstate/react';
2929
import {
30-
isMigrationFailed,
31-
isMigrationSkipped,
32-
isMigrationStopped,
33-
isMigrationSuccessful,
30+
getMigrationType,
3431
isMigrationRunning,
3532
} from '../state/automatic/selectors';
3633

@@ -599,21 +596,25 @@ function MigrationStateCircle({
599596
let textColor = '';
600597
let Icon = null;
601598

602-
const isSkipped = useSelector(actor, (state) =>
603-
isMigrationSkipped(state.context, migration.id)
599+
const isSkipped = useSelector(
600+
actor,
601+
(state) => getMigrationType(state.context, migration.id) === 'skipped'
604602
);
605603

606604
const isRunning = useSelector(actor, (state) =>
607605
isMigrationRunning(state.context, migration.id)
608606
);
609-
const isStopped = useSelector(actor, (state) =>
610-
isMigrationStopped(state.context, migration.id)
607+
const isStopped = useSelector(
608+
actor,
609+
(state) => getMigrationType(state.context, migration.id) === 'stopped'
611610
);
612-
const isFailed = useSelector(actor, (state) =>
613-
isMigrationFailed(state.context, migration.id)
611+
const isFailed = useSelector(
612+
actor,
613+
(state) => getMigrationType(state.context, migration.id) === 'failed'
614614
);
615-
const isSuccess = useSelector(actor, (state) =>
616-
isMigrationSuccessful(state.context, migration.id)
615+
const isSuccess = useSelector(
616+
actor,
617+
(state) => getMigrationType(state.context, migration.id) === 'successful'
617618
);
618619
// Check if this migration is in the completed migrations
619620

graph/migrate/src/lib/migrate.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ import {
1717
MigrationSettingsPanel,
1818
AutomaticMigration,
1919
} from './components';
20-
import {
21-
currentMigrationIsStopped,
22-
currentMigrationHasFailed,
23-
} from './state/automatic/selectors';
20+
import { getCurrentMigrationType } from './state/automatic/selectors';
2421

2522
export interface MigrateUIProps {
2623
migrations: MigrationDetailsWithId[];
@@ -86,11 +83,13 @@ export function MigrateUI(props: MigrateUIProps) {
8683
const isDone = useSelector(actor, (state) => state.matches('done'));
8784
const isInit = useSelector(actor, (state) => state.matches('init'));
8885
const isRunning = useSelector(actor, (state) => state.matches('running'));
89-
const isCurrentMigrationStopped = useSelector(actor, (state) =>
90-
currentMigrationIsStopped(state.context)
86+
const isCurrentMigrationStopped = useSelector(
87+
actor,
88+
(state) => getCurrentMigrationType(state.context) === 'stopped'
9189
);
92-
const isCurrentMigrationFailed = useSelector(actor, (state) =>
93-
currentMigrationHasFailed(state.context)
90+
const isCurrentMigrationFailed = useSelector(
91+
actor,
92+
(state) => getCurrentMigrationType(state.context) === 'failed'
9493
);
9594

9695
const isNeedReview = useSelector(actor, (state) =>

graph/migrate/src/lib/state/automatic/guards.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
11
import { AutomaticMigrationState } from './types';
22
import {
3+
getCurrentMigrationType,
34
currentMigrationCanLeaveReview,
45
currentMigrationHasChanges,
5-
currentMigrationHasFailed,
6-
currentMigrationHasSucceeded,
7-
currentMigrationIsSkipped,
86
currentMigrationIsRunning,
97
} from './selectors';
108

119
export const guards = {
1210
canStartRunningCurrentMigration: (ctx: AutomaticMigrationState) => {
11+
const type = getCurrentMigrationType(ctx);
1312
return (
1413
!!ctx.currentMigration &&
1514
!currentMigrationIsRunning(ctx) &&
16-
!currentMigrationIsSkipped(ctx) &&
15+
!(type === 'skipped') &&
1716
// Allow running if migration is not completed
1817
// stopped migrations can be restarted via the UI, Nx-console will handle the state change
19-
!currentMigrationHasFailed(ctx) &&
18+
!(type === 'failed') &&
2019
!currentMigrationHasChanges(ctx)
2120
);
2221
},
2322

2423
currentMigrationIsDone: (ctx: AutomaticMigrationState) => {
24+
const type = getCurrentMigrationType(ctx);
2525
return (
2626
!!ctx.currentMigration &&
2727
!currentMigrationIsRunning(ctx) &&
28-
((currentMigrationHasSucceeded(ctx) &&
29-
currentMigrationCanLeaveReview(ctx)) ||
30-
currentMigrationIsSkipped(ctx))
28+
((type === 'successful' && currentMigrationCanLeaveReview(ctx)) ||
29+
type === 'skipped')
3130
);
3231
},
3332

graph/migrate/src/lib/state/automatic/selectors.ts

Lines changed: 20 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -16,83 +16,36 @@ export function findFirstIncompleteMigration(
1616
);
1717
}
1818

19-
export function currentMigrationHasSucceeded(ctx: AutomaticMigrationState) {
20-
if (!ctx.currentMigration) {
21-
return false;
22-
}
23-
return isMigrationSuccessful(ctx, ctx.currentMigration.id);
24-
}
25-
26-
export function currentMigrationHasChanges(ctx: AutomaticMigrationState) {
27-
if (!ctx.currentMigration) {
28-
return false;
29-
}
30-
const completed =
31-
ctx.nxConsoleMetadata?.completedMigrations?.[ctx.currentMigration?.id];
32-
return (
33-
completed?.type === 'successful' &&
34-
(completed.changedFiles?.length ?? 0) > 0
35-
);
36-
}
37-
38-
export function isMigrationSuccessful(
39-
ctx: AutomaticMigrationState,
40-
migrationId: string
41-
) {
42-
return (
43-
ctx.nxConsoleMetadata?.completedMigrations?.[migrationId]?.type ===
44-
'successful'
45-
);
46-
}
47-
48-
export function isMigrationFailed(
19+
export function getMigrationType(
4920
ctx: AutomaticMigrationState,
5021
migrationId: string
5122
) {
52-
return (
53-
ctx.nxConsoleMetadata?.completedMigrations?.[migrationId]?.type === 'failed'
54-
);
23+
return ctx.nxConsoleMetadata?.completedMigrations?.[migrationId]?.type;
5524
}
5625

57-
export function isMigrationSkipped(
58-
ctx: AutomaticMigrationState,
59-
migrationId: string
60-
) {
61-
return (
62-
ctx.nxConsoleMetadata?.completedMigrations?.[migrationId]?.type ===
63-
'skipped'
64-
);
26+
export function getCurrentMigrationType(ctx: AutomaticMigrationState) {
27+
if (!ctx.currentMigration) return undefined;
28+
return getMigrationType(ctx, ctx.currentMigration.id);
6529
}
6630

67-
export function isMigrationStopped(
31+
export function getMigrationCompletedData(
6832
ctx: AutomaticMigrationState,
6933
migrationId: string
7034
) {
71-
return (
72-
ctx.nxConsoleMetadata?.completedMigrations?.[migrationId]?.type ===
73-
'stopped'
74-
);
75-
}
76-
77-
export function currentMigrationIsSkipped(ctx: AutomaticMigrationState) {
78-
if (!ctx.currentMigration) {
79-
return false;
80-
}
81-
return isMigrationSkipped(ctx, ctx.currentMigration.id);
35+
return ctx.nxConsoleMetadata?.completedMigrations?.[migrationId];
8236
}
8337

84-
export function currentMigrationHasFailed(ctx: AutomaticMigrationState) {
85-
if (!ctx.currentMigration) {
86-
return false;
87-
}
88-
return isMigrationFailed(ctx, ctx.currentMigration.id);
38+
export function getCurrentMigrationCompletedData(ctx: AutomaticMigrationState) {
39+
if (!ctx.currentMigration) return undefined;
40+
return getMigrationCompletedData(ctx, ctx.currentMigration.id);
8941
}
9042

91-
export function currentMigrationIsStopped(ctx: AutomaticMigrationState) {
92-
if (!ctx.currentMigration) {
93-
return false;
94-
}
95-
return isMigrationStopped(ctx, ctx.currentMigration.id);
43+
export function currentMigrationHasChanges(ctx: AutomaticMigrationState) {
44+
const completed = getCurrentMigrationCompletedData(ctx);
45+
return (
46+
completed?.type === 'successful' &&
47+
(completed.changedFiles?.length ?? 0) > 0
48+
);
9649
}
9750

9851
export function currentMigrationIsReviewed(ctx: AutomaticMigrationState) {
@@ -103,10 +56,11 @@ export function currentMigrationIsReviewed(ctx: AutomaticMigrationState) {
10356
}
10457

10558
export function currentMigrationCanLeaveReview(ctx: AutomaticMigrationState) {
59+
const type = getCurrentMigrationType(ctx);
10660
return (
10761
currentMigrationIsReviewed(ctx) ||
108-
currentMigrationIsSkipped(ctx) ||
109-
(currentMigrationHasSucceeded(ctx) && !currentMigrationHasChanges(ctx))
62+
type === 'skipped' ||
63+
(type === 'successful' && !currentMigrationHasChanges(ctx))
11064
);
11165
}
11266

@@ -120,8 +74,6 @@ export function isMigrationRunning(
12074
}
12175

12276
export function currentMigrationIsRunning(ctx: AutomaticMigrationState) {
123-
if (!ctx.currentMigration) {
124-
return false;
125-
}
77+
if (!ctx.currentMigration) return false;
12678
return isMigrationRunning(ctx, ctx.currentMigration.id);
12779
}

0 commit comments

Comments
 (0)