Skip to content

Commit

Permalink
feat(core): log a note after an interactive migration run where packa…
Browse files Browse the repository at this point in the history
…ge updates were opted out
  • Loading branch information
leosvelperez committed May 5, 2023
1 parent 575c6a1 commit 743df6e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
10 changes: 10 additions & 0 deletions packages/nx/src/command-line/migrate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ describe('Migration', () => {
},
'@my-company/lib-4': { version: '2.0.1', addToPackageJson: false },
},
minVersionWithSkippedUpdates: undefined,
});
});

Expand Down Expand Up @@ -503,6 +504,7 @@ describe('Migration', () => {
'@my-company/lib-1': { version: '3.0.0', addToPackageJson: false },
'@my-company/lib-2': { version: '3.0.0', addToPackageJson: false },
},
minVersionWithSkippedUpdates: undefined,
});
});

Expand Down Expand Up @@ -635,6 +637,7 @@ describe('Migration', () => {
child2: { version: '3.0.0', addToPackageJson: false },
child3: { version: '3.0.0', addToPackageJson: false },
},
minVersionWithSkippedUpdates: undefined,
});
expect(enquirer.prompt).toHaveBeenCalledWith(
expect.arrayContaining([
Expand Down Expand Up @@ -693,6 +696,7 @@ describe('Migration', () => {
mypackage: { version: '2.0.0', addToPackageJson: false },
child1: { version: '3.0.0', addToPackageJson: false },
},
minVersionWithSkippedUpdates: '2.0.0',
});
expect(enquirer.prompt).toHaveBeenCalled();
});
Expand Down Expand Up @@ -749,6 +753,7 @@ describe('Migration', () => {
child2: { version: '3.0.0', addToPackageJson: false },
child3: { version: '3.0.0', addToPackageJson: false },
},
minVersionWithSkippedUpdates: undefined,
});
expect(enquirer.prompt).not.toHaveBeenCalled();
});
Expand Down Expand Up @@ -831,6 +836,7 @@ describe('Migration', () => {
child2: { version: '3.0.0', addToPackageJson: false },
child3: { version: '3.0.0', addToPackageJson: false },
},
minVersionWithSkippedUpdates: undefined,
});
});

Expand Down Expand Up @@ -913,6 +919,7 @@ describe('Migration', () => {
pkg1: { version: '2.0.0', addToPackageJson: false },
pkg2: { version: '2.0.0', addToPackageJson: false },
},
minVersionWithSkippedUpdates: undefined,
});
});

Expand Down Expand Up @@ -961,6 +968,7 @@ describe('Migration', () => {
mypackage: { version: '2.0.0', addToPackageJson: false },
child1: { version: '3.0.0', addToPackageJson: false },
},
minVersionWithSkippedUpdates: undefined,
});
expect(enquirer.prompt).toHaveBeenCalledWith(
expect.arrayContaining([
Expand Down Expand Up @@ -1013,6 +1021,7 @@ describe('Migration', () => {
packageUpdates: {
mypackage: { version: '2.0.0', addToPackageJson: false },
},
minVersionWithSkippedUpdates: undefined,
});
expect(enquirer.prompt).not.toHaveBeenCalled();
});
Expand Down Expand Up @@ -1257,6 +1266,7 @@ describe('Migration', () => {
packageUpdates: {
parent: { version: '2.0.0', addToPackageJson: false },
},
minVersionWithSkippedUpdates: '2.0.0',
});
expect(enquirer.prompt).toHaveBeenCalled();
});
Expand Down
35 changes: 30 additions & 5 deletions packages/nx/src/command-line/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export class Migrator {
private readonly collectedVersions: Record<string, string> = {};
private readonly promptAnswers: Record<string, boolean> = {};
private readonly nxInstallation: NxJsonConfiguration['installation'] | null;
private minVersionWithSkippedUpdates: string | undefined;

constructor(opts: MigratorOptions) {
this.packageJson = opts.packageJson;
Expand All @@ -156,7 +157,11 @@ export class Migrator {
});

const migrations = await this.createMigrateJson();
return { packageUpdates: this.packageUpdates, migrations };
return {
packageUpdates: this.packageUpdates,
migrations,
minVersionWithSkippedUpdates: this.minVersionWithSkippedUpdates,
};
}

private async createMigrateJson() {
Expand Down Expand Up @@ -567,6 +572,15 @@ export class Migrator {
},
]).then(({ shouldApply }: { shouldApply: boolean }) => {
this.promptAnswers[promptKey] = shouldApply;

if (
!shouldApply &&
(!this.minVersionWithSkippedUpdates ||
lt(packageUpdate.version, this.minVersionWithSkippedUpdates))
) {
this.minVersionWithSkippedUpdates = packageUpdate.version;
}

return shouldApply;
});
}
Expand Down Expand Up @@ -1232,10 +1246,8 @@ async function generateMigrationsJsonAndUpdatePackageJson(
excludeAppliedMigrations: opts.excludeAppliedMigrations,
});

const { migrations, packageUpdates } = await migrator.migrate(
opts.targetPackage,
opts.targetVersion
);
const { migrations, packageUpdates, minVersionWithSkippedUpdates } =
await migrator.migrate(opts.targetPackage, opts.targetVersion);

updatePackageJson(root, packageUpdates);
await updateInstallationDetails(root, packageUpdates);
Expand Down Expand Up @@ -1275,6 +1287,19 @@ async function generateMigrationsJsonAndUpdatePackageJson(
: []),
],
});

if (opts.interactive && minVersionWithSkippedUpdates) {
output.note({
title: 'Opted out package updates',
bodyLines: [
'Based on this migration run, to later apply the package updates you opted out you can run:',
`nx migrate ${opts.targetPackage}@${opts.targetVersion} --from ${minVersionWithSkippedUpdates} --exclude-applied-migrations`,
`The version passed in the '--from' flag above is the version you'd need to use to get those package updates.`,
`Please keep track of that version so you don't miss the package updates you opted out of during this migration run and can apply them later.`,
`If you haven't applied other package updates you opted out of in previous migrations, use the oldest version you're keeping track of.`,
],
});
}
} catch (e) {
output.error({
title: `The migrate command failed.`,
Expand Down

0 comments on commit 743df6e

Please sign in to comment.